Passing Argument To Powershell Custom Action

Passing Argument To Powershell Custom Action

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.

Was this article helpful? Yes No
No ratings
Version history
Revision #:
1 of 1
Last update:
‎Jul 19, 2018 04:36 PM
Updated by: