skip to Main Content

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


  1. 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 as SID = B

    Login or Signup to reply.
  2. The output file shows the date correctly appended, but is otherwise blank, not capturing a value for the $A??

    You’re trying to pass a parameter $A to a powershell script using the az 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 the az vm run-command command, and also change the parameter from $A to $SID as shown below

       SID='B'"
    

    Here is the update script.

    Start_script

    param(
        [string]$SID
    )
    date | Out-File -FilePath C:scriptsPowerShellLog.txt -Append
    $SID | Out-File -FilePath C:ScriptsPowerShellLog.txt -Append
    " "  | Out-File -FilePath C:ScriptsPowerShellLog.txt -Append```
    

    Command to call from Azure CLI:

     az vm run-command invoke --resource-group Aks-rg-test --name venkat-vm --command-id 'RunPowerShellScript' --scripts "C:ScriptsStart_script.ps1  SID='B'"
    

    Output:

    enter image description here

    Once the above command is executed, the value of the SID parameter will be stored in the log file.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search