I am making an Azure CLI Run command call to a Windows VM, AZ VM run-command but cannot get the parameters to pass properly?
Command to call from Azure CLI:
az vm run-command invoke
--resource-group 'resgrpname'
--name 'hostname'
--command-id 'RunPowerShellScript'
--scripts "C:ScriptsStart_script.ps1"
--parameters "A=B"
When the PowerShell script runs, it executes correctly, appending to a file, but doesn’t accept the parameter. Code in the PS1 script:
param(
[string]$SID
)
# Use the $param1 and $param2 variables in your script
date | Out-File -FilePath C:scriptsPowerShellLog.txt -Append
$A | Out-File -FilePath C:scriptsPowerShellLog.txt -Append
" " | Out-File -FilePath C:scriptsPowerShellLog.txt -Append
The output file shows the date correctly appended, but is otherwise blank, not capturing a value for the $A??
Has anyone seen this before? Anything obvious I can try?
I have tried just passing the commands and parameters inline as text, rather than calling a PS1 script, but invoke doesn’t handle that very well, and it would soon get unwieldy in a multi-command PowerShell script
2
Answers
For your script to work, the parameter you pass in
az vm run-command invoke
has to match the name of the parameter in your Powershell script.Therefore, you either need to rename the parameter to
$A
or pass the parameter asSID = B
You’re trying to pass a parameter
$A
to a powershell script using theaz vm run-command
command. However, it seems that the parameter is not being captured correctly in the script.To fix this issue, remove the
--parameters
argument in theaz vm run-command
command, and also change the parameter from $A to $SID as shown belowHere is the update script.
Start_script
Command to call from Azure CLI:
Output:
Once the above command is executed, the value of the
SID
parameter will be stored in the log file.