burnersk
Posts: 43
Joined: Mon Mar 25, 2019 12:26 pm

PowerShell cannot read/write properties which names come from variables

I have a selection of Advanced Installer PowerShell scripts, that I just have optimized. AI/MSI property names are now not hard-coded, but supplied via parameters.

Take this example:

Code: Select all

# Block for declaring the script parameters.
Param($myProperty)

Add-Type -AssemblyName System.Windows.Forms

$propValue = AI_GetMsiProperty "$myProperty"
[System.Windows.Forms.MessageBox]::Show("The value of '$myProperty' is '$propValue' (within PowerShell before overwrite).", "PowerShellScriptInline")
AI_SetMsiProperty "$myProperty" "$propValue"
$propValue = "Changed Value"
[System.Windows.Forms.MessageBox]::Show("The value of '$myProperty' is '$propValue' (within PowerShell after overwrite).", "PowerShellScriptInline")
The PowerShell parameters in Advanced Installer are configured to be: MY_PROPERTY

So I assume that MY_PROPERTY (already configured with "Install Parameters") is read-out and written-in by the PowerShell script.

However, when I use variables, the actual used (I assume) property name (1st parameter) is an empty string. When I hard-code the property into the 1st parameter it works.
The text output, however, shows that $myProperty is actually MY_PROPERTY.

I have attached the AIP file:
DemoApplication.aip
(17.1 KiB) Downloaded 201 times
Catalin
Posts: 6585
Joined: Wed Jun 13, 2018 7:49 am

Re: PowerShell cannot read/write properties which names come from variables

Hello,

First of all, please keep in mind that the AI_GetMsiProperty and AI_SetMsiProperty are both custom cmdlets created with the only purpose of getting/setting a property value.

With that being said, it is not possible to use the AI_GetMsiProeprty cmdlet to get the value from a variable. This can easily be done, as it follows:

- you firstly get the value of a property in a variable, say var1

- set a second variable, say var2, to the value of var1 variable:

$var2 = $var1
PS.png
PS.png (23.34 KiB) Viewed 3192 times
If the property's value contains spaces, then it's value should be encapsuled between quotes ("") (e.g.: -myProperty "[MY_PROP]")

Hope I was able to shed some light on this.

Please let me know if you have any other questions and I will gladly assist.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
burnersk
Posts: 43
Joined: Mon Mar 25, 2019 12:26 pm

Re: PowerShell cannot read/write properties which names come from variables

Catalin wrote: Fri Jan 10, 2020 3:29 pm First of all, please keep in mind that the AI_GetMsiProperty and AI_SetMsiProperty are both custom cmdlets created with the only purpose of getting/setting a property value.
This is exactly my use-case. I need to get properties to use them in scripts, and need to set properties to use them in subsequent installer processing.

---For the rest of your answer, I can't follow you and the attached image is broken.---
I see you have updated your post. I will try your solution.
burnersk
Posts: 43
Joined: Mon Mar 25, 2019 12:26 pm

Re: PowerShell cannot read/write properties which names come from variables

Had no luck... So I guess I am forced to use hard-coded property names then.
Catalin
Posts: 6585
Joined: Wed Jun 13, 2018 7:49 am

Re: PowerShell cannot read/write properties which names come from variables

Hello,
This is exactly my use-case. I need to get properties to use them in scripts
This is indeed done using the AI_GetMsiProperty. What I was trying to explain is the fact that the parameter received by this cmdlet can not be another variable (e.g. $var, it must be a property, e.g. MY_PROPERTY).
-For the rest of your answer, I can't follow you and the attached image is broken
Could you please give me some more details about this? What do you mean by "the image is broken"? Could you please try to refresh the page and let me know if it helps? If that does not help, you can try a force refresh (shift+F5).

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

Return to “Common Problems”