SuperDev
Posts: 10
Joined: Thu Oct 13, 2022 5:38 pm

PowerShell Inline Script

Hello,

I am reaching out because I need help running an Invoke-Sqlcmd in the Powershell Inline Script to remove a DB when program is uninstalled. I want to pass the sql connection properties into this PS Script to directly reference those values after the user inputs them into a dialog box. I referred to the powershell documentation in Advanced Installer but still need more clarity on the structure needed to facilitate this. I have attached my project file, please let me know if anymore information/clarity is needed on my end thank you and have a great day!
Attachments
SQL DB Test.aip
(21.48 KiB) Downloaded 429 times
Liviu
Posts: 1325
Joined: Tue Jul 13, 2021 11:29 am
Contact: Website

Re: PowerShell Inline Script

Hello,

You've set the Execution Time for your custom action to "Immediately". Please note that the parameters value field is used for deferred custom actions.

The main purpose of the "Parameters" field from the "Run PowerShell inline script" custom action is to access properties from a deferred custom action (when the "Run PowerShell inline script" custom action has its execution time of "When the system is being modfied (deferred)").

For more details, please take a look at our Run Inline PowerShell Script article.

To get a property during an immediate action, you need to include a line with the following syntax in your script:

Code: Select all

$propValue = AI_GetMsiProperty YOUR_PROP
You can adjust your script based on the immediate action, or you can change its execution to "deferred".

In the article above you have an example of accessing properties for deferred custom actions - "Getting a property value into a script (for deferred custom actions)" section.

Hope this helps! If you have any other questions, please don’t hesitate to contact us.

Best regards,
Liviu
________________________________________
Liviu Sandu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
SuperDev
Posts: 10
Joined: Thu Oct 13, 2022 5:38 pm

Re: PowerShell Inline Script

Awesome, Thank you! I have adjust the syntax as recommended, but still receiving an error when trying to test out the script. I am certain I am doing this step the wrong way and will upload my project file. I truly appreciate your help!
Attachments
SQL DB Test.aip
(21.71 KiB) Downloaded 405 times
Catalin
Posts: 7513
Joined: Wed Jun 13, 2018 7:49 am

Re: PowerShell Inline Script

Hello,

Most likely, in your case, the properties are not available during the uninstall.

If you want the value that your user inputs at install time to be available at uninstall time, you can try to set the properties as "persistent".

How do I make the properties in the package keep their values during Maintenance or Upgrade?

Additionally, since your custom action modifies the system, you may want to schedule it as "deferred" (its "Execution Time")

If your custom action requires elevation, you can also check the "Run under the LocalSystem account with full privileges (no impersonation)" option.

Here is how your custom action can look like:
Screenshot_87.png
Screenshot_87.png (81.13 KiB) Viewed 16800 times

Hope this helps!

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
GonzalezA159
Posts: 1
Joined: Wed Nov 20, 2024 9:06 pm

Re: PowerShell Inline Script

@catalin
I have been struggling with something similar to this in my Advanced Installer version 22.2.

My inline script does NOT seem to be working and it is configured exactly as you have in the screenshot provided.
Catalin
Posts: 7513
Joined: Wed Jun 13, 2018 7:49 am

Re: PowerShell Inline Script

Hello and welcome to our forums,

If possible, could you please give me some more details about your script not working?

What exactly does that mean, is your script failing at install time? Or perhaps it is not working as you expect it?

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
nainrohan
Posts: 3
Joined: Tue Jan 21, 2025 10:55 am

Re: PowerShell Inline Script

Hello there,
I want to access a searched file in my shell script, I ran search test and it was able to locate the file in the target computer. Property name is "FULL_CHAIN_CERT" and file type is .pem, how can I access the file in custom action's Run Powershell inline script.

Regards,
Rohan
Catalin
Posts: 7513
Joined: Wed Jun 13, 2018 7:49 am

Re: PowerShell Inline Script

Hello Rohan and welcome to our forums,

Depending on your custom action execution, we can retrieve the property in different ways:

1. if the custom action is immediate (Execution Time), we can do it like this:

$cert = AI_GetMsiProperty FULL_CHAIN_CERT

Note: if this does not retrieve the full name, you can append the ".pem" using something like the "Join-String" cmdlet.

2. if the custom action is set on deferred, we can proceed like this:
Screenshot_220.png
Screenshot_220.png (6.18 KiB) Viewed 7121 times

If the name contains spaces, we have to enclose the property between double quotes, e.g.:

Code: Select all

-cert "[FULL_CHAIN_CERT]"
Hope this helps!

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
nainrohan
Posts: 3
Joined: Tue Jan 21, 2025 10:55 am

Re: PowerShell Inline Script

Hello team,
Thanks for the quick response,
I tried both the approaches, I am getting error in script output saying
"ERROR: Missing an argument for parameter "file". Specify a parameter of type 'System.Object' and try again..."

Also, in "Application Search" in custom behaviour, when I am testing the file search operation "test search" it is able to find the path of the file correctly but when I run the installer, I see an error in the logs saying,
"Error while attempting to search in/below folder. FOLDER_NAME."
For every folder it is searching for in.

Also, can you please review my approach or suggest some alternative approach on how to access a file in target computer with the fixed given file name and use it in our custom script (powershell script preferably) to perform further actions on the file. Please give suggestions within professional version which as I have professional version access constraint with me.

Regards,
Rohan
Attachments
search4.PNG
search4.PNG (141.97 KiB) Viewed 7109 times
search3.PNG
search3.PNG (143.07 KiB) Viewed 7109 times
Catalin
Posts: 7513
Joined: Wed Jun 13, 2018 7:49 am

Re: PowerShell Inline Script

Hello Rohan,

Please note that your powershell script relies on a search, which is not executed when you use the "Test Script" button.

Instead, you should be testing the entire installation package by running it - preferably on a Virtual Machine or in Windows Sandbox.

This way, the search will be executed first and then the PowerShell script based on that.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”