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
Yes, this is one of the Quoting issues documented in the Azure CLI docs:
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:
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: