rmoc
Posts: 64
Joined: Tue Mar 14, 2017 8:27 pm

Escaped Powershell parameter

I have a PowerShell script that receives several properties. I have enclosed these properties in quotes as required.
PoweShellCmd.png
PoweShellCmd.png (3.82 KiB) Viewed 1612 times
However, the properties themselves contain a double quote ("), which causes the parameters to be passed incorrectly.
BrokeCmdLine.png
BrokeCmdLine.png (122.17 KiB) Viewed 1612 times
I have already read in the forum that there can be issues when a backslash appears at the end. The only solution I currently see is to escape the properties within the script itself, store the escaped values in a second property, and then pass that property to my script. It would be desirable if Advanced Installer offered better support for this scenario, so that this issue wouldn’t need to be handled manually in every project and for every potentially affected property.

Example Project:
EscapePowerShellParameter.aip
(16.87 KiB) Downloaded 32 times
Catalin
Posts: 7606
Joined: Wed Jun 13, 2018 7:49 am

Re: Escaped Powershell parameter

Hello Rene,

Do you need to retrieve such properties during the deferred stage?

If not, then instead of using the "Parameter" field, we can use the predefined cmdlet - AI_GetMsiProperty.

It can look something as it follows:
Screenshot_92.png
Screenshot_92.png (18.03 KiB) Viewed 1608 times
At install time, the properties values will be correctly passed:
Screenshot_91.png
Screenshot_91.png (240.79 KiB) Viewed 1608 times

Hope this helps!

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
rmoc
Posts: 64
Joined: Tue Mar 14, 2017 8:27 pm

Re: Escaped Powershell parameter

Hello Catalin

Yes, I’m familiar with the AI_Get/SetMsiProperty functions, but unfortunately I need the values in a script that is executed during the deferred stage. Since there is probably no simple way to achieve this, perhaps this could be supported in future versions of AI.

Regards,
René
OneScripter
Posts: 3
Joined: Thu May 16, 2024 6:00 pm

Re: Escaped Powershell parameter

In the parameter values box, try enclosing your parameters in single quotes instead of double quotes.
rmoc
Posts: 64
Joined: Tue Mar 14, 2017 8:27 pm

Re: Escaped Powershell parameter

Hello OneScripter,

thank you very much for your comment. I think this looks good.
itWorks.png
itWorks.png (135.92 KiB) Viewed 1579 times
@Catalin Is this an officially supported feature? If so, shouldn’t it be used as the standard way of passing parameters?

Regards,
René
Catalin
Posts: 7606
Joined: Wed Jun 13, 2018 7:49 am

Re: Escaped Powershell parameter

Hello René,

In case of double quotes, that is indeed a smart way of escaping them.
@Catalin Is this an officially supported feature? If so, shouldn’t it be used as the standard way of passing parameters?
This is an officially supported feature in the sense that this is exactly how quotes work in PowerShell outside of Advanced Installer as well.

Regarding the standard way, not quite, as double double quotes parameters are quite rare, so simply enclosing the parameter (if it has a space, for example) works just fine with single double quotes.

@OneScripter,

Thank you for your contribution to our forum. :D

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
rmoc
Posts: 64
Joined: Tue Mar 14, 2017 8:27 pm

Re: Escaped Powershell parameter

Hello Catalin,

Unfortunately, I have to come back to this ticket. I changed the Powershell parameters to '[MY_VALUE]'. Unfortunately, there is now another character (#) that causes my setup to crash. Presumably, the (#) is masked by AdvancedInstaller with a (`) because otherwise it would be interpreted as a comment in Powershell. Unfortunately, this means that my setup cannot be executed as desired.
broke.png
broke.png (81.27 KiB) Viewed 707 times
broke2.png
broke2.png (6.74 KiB) Viewed 707 times
Kind regards,
René
Catalin
Posts: 7606
Joined: Wed Jun 13, 2018 7:49 am

Re: Escaped Powershell parameter

Hello René,

I think the tilda character comes from trying to escape the # character.

Is the value of the property passed dynamically or statically? I'm thinking if it is passed statically, we can somehow try to escape it.

Could you please provide a small sample that reproduces this so I can run some tests on my end and see if I can manage to make it work with the "#" character?

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
rmoc
Posts: 64
Joined: Tue Mar 14, 2017 8:27 pm

Re: Escaped Powershell parameter

Hello Catalin,

Please find attached the sample project
EscapePowerShellParameter.aip
(16.86 KiB) Downloaded 13 times
.
The property can be entered into the setup in various ways. It can be saved in the setup, as a command line parameter, or entered by the user. I would therefore prefer not to execute a separate action to handle the special characters.

The problem is how AdvancedInstaller passes the command line parameters to PS. I think # is replaced by '# across the board without checking whether it is within a parameter with single quotes.

I have tested the following special characters:

# does not work because it is replaced by '#

' causes the setup to abort

Code: Select all

MSI (c) (E8!18) [07:30:20:110]: PROPERTY CHANGE: Adding POWERSHELL_EXECUTION_LOG property. Its value is 'ERROR: In line:1 character:98
+ ... al\Temp\scr5E82.ps1 ‘This is my “Value” ...’ 'value ‘ broke my setup’
+                                                                         ~
The string does not have a terminating character: ‘.’.
Action ended 07:30:20: CheckMyValue. Return value 3.
Info 2896. Executing action CheckMyValue failed.
` Works

" Works

It's a little frustrating to invest so much time in dealing with command line parameters.

Kind regards
René
Catalin
Posts: 7606
Joined: Wed Jun 13, 2018 7:49 am

Re: Escaped Powershell parameter

Hello René,

Thank you for providing the project file.

I have run a test on my end and noticed the same behavior as you, specifically the single quote added before the "#" character.

However, in my case, the setup does not terminate, it just continues further with the new property value (with the added single quote).

I have run some more tests and the # character is retrieved correctly if:

1. we pass the parameter using double quotes, so something like this:

Code: Select all

"[PARAM2_PS]"
2. we retrieve the value of the property via the AI_GetMsiProperty cmdlet

Now, this depends a lot on where do you want to schedule the custom action.

If you need it to run during the deferred stage and want to retrieve the property there, then the second point does not help.

However, if you only want to run it during the immediate stage, then the second approach fixes this problem.

Regarding the issue we have here, I will open a ticket so that this can be fixed in a future version of Advanced Installer.

I apologize for the inconvenience.

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

Return to “Common Problems”