Using az vm run-command from within a .ps1 script and with .ps1 script file

1.9k Views Asked by At

I have created a VM in Azure, and I can run the following command from my local Windows 10 Powershell window:

az vm run-command invoke --command-id RunPowerShellScript --resource-group $ResourceGroup --name $AD1Name --scripts "@provision.ps1"

But when I put this line in a Powershell script file, CreateVMAndProvision.ps1, and run my CreateVMAndProvision.ps1 that also run the az vm create... I get the following error:

  "value": [
    {
      "code": "ComponentStatus/StdOut/succeeded",
      "displayStatus": "Provisioning succeeded",
      "level": "Info",
      "message": "",
      "time": null
    },
    {
      "code": "ComponentStatus/StdErr/succeeded",
      "displayStatus": "Provisioning succeeded",
      "level": "Info",
      "message": "At C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\1.1.5\\Downloads\\script6.ps1:1 char:1
+ @provision.ps1
+ ~~~~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@provision'
can be used only as an argument to a command. To reference variables in an expression use
'$provision'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : SplattingNotPermitted
 ",
      "time": null
    }
  ]
}

The syntax of specifying my local provision.ps1 file to the az cli command as "@provision.ps1" seem to be the problem, but I can not figure out how to solve this. I have tried to use double quotes, combination of double and single quotes, here string (@"..."@) and (@'...'@) without any success.

Anyone have an idea on how to solve this?

Thanks! :: Petter

1

There are 1 best solutions below

0
On

Yes, this is one of the Quoting issues documented in the Azure CLI docs:

When running Azure CLI commands in PowerShell, parsing errors will occur when the arguments contain special characters of PowerShell, such as at @. You can solve this problem by adding a backtick (`) before the special character to escape it, or by enclosing the argument with single or double quotes '/".

For example, az group deployment create --parameters @parameters.json doesn't work in PowerShell because @ is parsed as a splatting symbol. To fix this, you may change the argument to `@parameters.json or '@parameters.json'.

Since what you have is a PS script already, the easiest way out would be to use the PowerShell equivalent of az vm run-command, which is Invoke-AzVMRunCommand.

Here is how you can use it:

Invoke-AzVMRunCommand -ResourceGroupName '<myResourceGroup>' -Name '<myVMName>' -CommandId 'RunPowerShellScript' -ScriptPath '<pathToScript>' -Parameter @{"arg1" = "var1";"arg2" = "var2"}

Other ways to run PowerShell scripts in your Windows VM by using Run Command are documented here.

But if you do have specific reasons to use Azure CLI commands within your PS script, you could also try using PowerShell's stop-parsing symbol --% in your command. The stop-parsing symbol (--%), introduced in PowerShell 3.0, directs PowerShell to refrain from interpreting input as PowerShell commands or expressions.

So this should do the trick:

az --% vm run-command invoke --command-id RunPowerShellScript --resource-group '<myResourceGroup>' --name '<myVMName>' --scripts @script.ps1