Summary
Example on passing arguments to powershell script
Synopsis
This article will show you different ways you can pass arguments to a powershell custom action.
Discussion
If you are using InstallShield 2015 or newer, you can make sure of cmdlets, which will let you interact(get and set properties) with the running powershell custom action.
However if you are using an earlier version of InstallShield (...2014 or earlier), there is no built in cmdlet functionality and you will have to come up with a different way to pass arguments to your powershell script. This knowledge base article will show you one way you can approach this.
For InstallShield 2015 or newer, you can take a look at the online help document Calling a PowerShell Custom Action, which will give you more information on how you can use cmdlets in your powershell script to get and set properties that your powershell script can use.
For InstallShield 2014 or older, since there is no direct way to pass arguments to the powershell custom action, one way to approach this is to use an InstallScript custom action. This script will still call your powershell script, but you will now have the ability to pass arguments.
Now in your project, go to the 'Behavior and Logic->InstallScript' view and under the 'InstallScript->Files' node, right click and add a new 'setup.rul' file (...assuming no file is present already, if so, then skip this).
Once you have the 'setup.rul' file in your project, then there should be a default function added to the script called MyFunction(). You can keep this function name or change it (...remember to update the prototype as well) and you can use the following code...
//------------------------------------
SdShowMsg("Executing Script...", TRUE);
// Setting szVar to argument value that will be send to powershell script
szVar="Default Test Text";
LongPathToQuote(szVar, TRUE);
// (Optional) Setting a path to a log file to capture any results from the execution of this script
szLog=INSTALLDIR^"MyLogFile.log";
LongPathToQuote(szLog, TRUE);
// Setup command line to run ps1 file
szProgram=WINSYSDIR64^"WindowsPowerShell\\v1.0\\powershell.exe";
szPathPs=INSTALLDIR^"MyPowershellScript.ps1";
LongPathToQuote(szPathPs, TRUE);
// Setup actual command line to be passed
szCmdLine="-file "+szPathPs+" "+szVar+" >> "+szLog;
// Run the powershell script from command line using the command line above.
// This function will hide the command window and wait until the powershell script is completed.
LaunchAppAndWait(szProgram, szCmdLine, LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN);
SdShowMsg("Executing Script...", FALSE);
//------------------------------------
So in the above script, the LaunchAppAndWait() function is used to execute powershell.exe and the path to your .ps1 file, along with any command line arguments are used as well. So in your powershell script file, you can use something like this to get the
NOTE: Inside your powershell script, you can use...
#-------------------------------------------
param( [string]$replaceme)
#-------------------------------------------
...where $replaceme will contain the custom string passed to the ps1 file.
Related Articles
Launch InstallAnywhere Without a UAC prompt 3Number of Views "Unknown Publisher" on UAC Prompt when Uninstalling from Add/Remove Programs with a Signed Installation Package 5Number of Views Can I Launch a Custom Action from SUPPORTDIR in Express? 5Number of Views HOW TO: Stop a Windows Service using a Custom Action 8Number of Views Launch a Custom Executable 16Number of Views
Hi, I am Reva - Ask me anything.
No new updates
Thanks for the feedback!
Your feedback has been saved.Rate this response:
Add Additional feedback ( Optional )
Are you sure you want to cancel
the case creation?
Are you sure you want to cancel the case creation?
Are you sure you want to close this case
| Products | Region | Phone Numbers |
|---|---|---|
| FlexNet Operations FlexNet Embedded FlexNet Publisher FlexNet Connect FlexNet Code Insight InstallAnywhere InstallShield |
North America * |
+1 630-332-2513 (toll) +1 877-279-2853 (toll-free in North America) |
| Europe * |
+44 1925 944367 (toll) +44 800 047 8642 (toll-free in Europe) |
|
| Japan * | +81 3-4540-5335 (select option 2) | |
| Australia * |
+61 3 9895 2177 +61 1800 560 603 (toll-free in Australia) |
|
|
Usage Intelligence (formerly
Revulytics) Compliance Intelligence |
Please use the Case Portal to submit your support ticket or reach out to your Revenera contact. | |
Case id: 00001065
Activity: Status change: 2 hours ago