akshay karambale
Posts: 49
Joined: Thu May 28, 2020 8:04 am

How to retrieve value of APPDIR in "Run Powershell Inline Script Custom "action

Hello,
We are created one Bootstrapper package in which we have using "Run PowerShell Inline Script" Custom action to retrieve value of APPDIR property (i.e. APPLICATION FOLDER). Please suggest command to retrieve this using this custom action.
we have tried below command,
$APPValue = [Environment]::GetEnvironmentVariable('APPDIR','Machine ')

But this command not returning the actual value.

So please help us to achieve this,



Thanks
Akshay
Catalin
Posts: 6608
Joined: Wed Jun 13, 2018 7:49 am

Re: How to retrieve value of APPDIR in "Run Powershell Inline Script Custom "action

Hello Akshay,

Here's an article that explains what you want to achieve:

How to set an installer property using custom actions

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
akshay karambale
Posts: 49
Joined: Thu May 28, 2020 8:04 am

Re: How to retrieve value of APPDIR in "Run Powershell Inline Script Custom "action

Hello Catlin,

Thanks for the reply,

I have gone through the article that you have suggested but it is not helpful to me because its not solved my issue. I have attached the screenshot of command and output I am getting with this post, also I have describing what I am actually want as output please go through it and give us the way to resolve my problem.

1. As you know there is default APPDIR (i.e. "Application folder" directory) property in advanced installer project under location and default value for it is "C:\Program Files".
2. I have using Run PowerShell script inline custom action action to assign some of the install parameter in run time.
3. So for some of the parameter I need the value of APPDIR parameter which default parameter of advanced installer.
4. For this I have already tried the "Get and set a property in Windows PowerShell Script custom action" but it is not fulfilling my need.
5. So please suggest the way to assign the APPDIR parameter value to custom parameter.

Also please go through the screenshot of "output and used command" attached with this post.

Please suggest the solution as soon as possible. We are waiting for your solution.

Best regards,
Akshay
Attachments
Error.JPG
Error.JPG (71.18 KiB) Viewed 2922351 times
Catalin
Posts: 6608
Joined: Wed Jun 13, 2018 7:49 am

Re: How to retrieve value of APPDIR in "Run Powershell Inline Script Custom "action

Hello Akshay,

The way in which you have retrieved the property is correct.

However, please note that this is not working using the "Test" button because in order for the property to be resolved, we need to run the installer.

Additionally, if you want to retrieve the value of APPDIR during a "deferred" custom action, you will need to pass the value of APPDIR like this:
Screenshot_137.png
Screenshot_137.png (5.13 KiB) Viewed 2922345 times
Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Radhika
Posts: 1
Joined: Thu Dec 07, 2023 1:36 pm

Re: How to retrieve value of APPDIR in "Run Powershell Inline Script Custom "action

Hi team,

I am trying to create a DB and execute the .sql files while installing the application .exe, we used below powershell Inscript under custom actions to perform this, Could create new database(with static values given in script not with user prompt value), but not able to execute .sql files which we kept inside "Files and Folders"(pls refer the attached image), my question's is
1.how to refer APPDIR value in powershell script ( we used like this $sqlScriptFolder = "APPDIR\scripts\database\tables" but this doesn't work)
2. And for creating new database, we created a 'SQLServerConnectionDlg' dialog box and used Property name to refer (pls see below), but no luck this also not working

$serverName = "[SERVER_PROPERTY]"
$databaseName = "[DATABASE_PROPERTY]"
$username = "[USERNAME_PROPERTY]"
$password = "[PASSWORD_PROPERTY]"


Powershell Inline script:-
$serverName = "ICH-L-RSHAN\SQLEXPRESS"
$databaseName = "Everi-JackpotXpress"
$username = "JXReports"
$password = "JXReports"

$connectionString = "Server=$serverName;Database=master;User ID=$username;Password=$password;"

$sqlCreateDB = "IF NOT EXISTS (SELECT 1 FROM sys.databases WHERE name = '$databaseName')
BEGIN
CREATE DATABASE [$databaseName]
END;"

try {
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$command = $connection.CreateCommand()
$command.CommandText = $sqlCreateDB

$connection.Open()
$command.ExecuteNonQuery()

Write-Host "Database '$databaseName' created successfully."

[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.MessageBox]::Show($databaseName)

# Define the folder path containing SQL script files in Advanced Installer
$sqlScriptFolder = APPDIR\scripts\database\tables" # Use the correct path

#$sqlScriptFolder = "HK_UM\Software\[Manufacturer]\[ProductName]\JackpotXpressAPI\scripts\database\tables" # Use the correct path

#$scriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
#$sqlScriptFolder = Join-Path -Path $scriptDirectory -ChildPath "JackpotXpressAPI\scripts\database\tables"


Write-Host "Folder Path: $sqlScriptFolder"


# Get all SQL script files from the specified folder
$sqlFiles = Get-ChildItem -Path $sqlScriptFolder -Filter "*.sql"

foreach ($file in $sqlFiles) {
$scriptContent = Get-Content -Path $file.FullName -Raw

$scriptContent.Split('GO') | ForEach-Object {
$command.CommandText = $_
$command.ExecuteNonQuery()
}

Write-Host "Executed SQL script: $($file.Name)"
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.MessageBox]::Show($file.Name)
}
} catch {
Write-Host "Error: $($_.Exception.Message)"
} finally {
if ($connection.State -ne 'Closed') {
$connection.Close()
}
}
Attachments
Screenshot 2023-12-13 221350.png
Screenshot 2023-12-13 221350.png (242.58 KiB) Viewed 2917003 times
Liviu
Posts: 1048
Joined: Tue Jul 13, 2021 11:29 am
Contact: Website

Re: How to retrieve value of APPDIR in "Run Powershell Inline Script Custom "action

Hello and welcome to our forums,
1.how to refer APPDIR value in powershell script ( we used like this $sqlScriptFolder = "APPDIR\scripts\database\tables" but this doesn't work)
To pass a property into your script, it depends on whether your script is scheduled to run in deferred or immediate execution time.

Please take a look at our "A custom action written in PowerShell can access Windows Installer properties in three ways" article.
And for creating new database, we created a 'SQLServerConnectionDlg' dialog box and used Property name to refer (pls see below), but no luck this also not working
You can try using our predefined support for this: Using SQL Databases tutorials

Other useful articles:

Create sql Database

Configure MS-SQL Server Connection


If you can't get this to work, please send us the .AIP (setup project) file by email to support at advancedinstaller dot com so we can further test and investigate this.

Best regards,
Liviu
________________________________________
Liviu Sandu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”