och
Posts: 9
Joined: Wed Jan 07, 2015 12:41 pm

Pass Property with spaces to Powershell script

Hi,

how can I pass Windows Installer Property which contains spaces to Powershell script Custom Action? I want to read APPDIR in script but string is terminated after first space.
In Script Parameters I have

Code: Select all

-appDir [APPDIR]
and in script I am checking it

Code: Select all

param($appDir)
echo $appDir >> "C:\install.log"
...
but in C:\install.log is only "C:\Program" instead of whole path like "C:\Program Files\Manufacturer\Product Name".

I've tried enclose variable name within quotes and curly braces

Code: Select all

echo "$appDir" >> "C:\install.log"
echo "${appDir}" >> "C:\install.log"
but result is the same.
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Pass Property with spaces to Powershell script

Hello,

In order to achieve what you need you should enclose the [APPDIR] formatted reference between quotes in the "Script Parameters" field of your custom action. So, can you please set the "Script Parameters" field like this:

Code: Select all

-appDir "[APPDIR]"
and see if this helps you?

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
och
Posts: 9
Joined: Wed Jan 07, 2015 12:41 pm

Re: Pass Property with spaces to Powershell script

It somehow works.

The path is full now but there are trailing quotes at the and so it looks like this C:\Program Files\Manufacturer\Product Name"
I remove it with this line

Code: Select all

$appDir = $appDir -replace '"',''
but I don't understand why it is happening.
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Pass Property with spaces to Powershell script

Hello,

I've tested the scenario and I replicated the behavior. This happens because, when resolved, the [APPDIR] property formatted reference ends with a trailing backslash (e.g. C:\Program Files\Manufacturer\Product Name\). Our "Run Windows PowerShell Script" custom action uses the Windows command interpreter tool to run the script file and it passes the script arguments as command line parameters. So the script parameters last characters (i.e. \") will be escaped to ". This is the normal behavior of the Windows command line tool.

In order to avoid this behavior you should escape the trailing backslash by duplicating it (e.g. \\) so that cmd.exe interprets this as a single \ literal. Therefore, in order to achieve what you need you should set the "Script Parameters" field like this:

Code: Select all

-appDir "[APPDIR]\"
If you have any questions let us know.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”