How to pass variable into powershell script through CFExecute arguments?

1k views Asked by At

I'm trying to pass a user supplied value into PowerShell script using the following code, but am having no luck:

<cfset MyVar="Woila!">

<cfoutput>
<cfexecute name="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" 

arguments="$MyVar = #MyVar# C:\Users\raimonds\Desktop\create_website_IIS_aws_uat_1.ps1" 
/>
</cfoutput>

The argument writes in the PowerShell command line, but it is not passing the variable into the .ps1 script with this syntax $MyVar.

2

There are 2 answers

1
Esperento57 On BEST ANSWER

try it

<cfexecute name="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" arguments="-file C:\Users\raimonds\Desktop\create_website_IIS_aws_uat_1.ps1 ""youvalue"""/>
0
BKBK On

Your were almost there. Just change the order, as follows:

<cfset MyVar="Woila!">

<cfexecute name="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" 

arguments="C:\Users\raimonds\Desktop\create_website_IIS_aws_uat_1.ps1 
 MyVar '#MyVar#'" />

Alternatively, define the arguments attribute as

arguments="& 'C:\Users\raimonds\Desktop\create_website_IIS_aws_uat_1.ps1' 
     MyVar '#MyVar#'"

This assumes your script defines the parameter MyVar in the usual way, for example:

 param([string]$MyVar)

See this Stackoverflow page for additional Powershell options you might want to use.