How to access parameters in az vm run-command create
See original GitHub issueI am trying to run a command using az vm run-command create (as per https://docs.microsoft.com/en-us/cli/azure/vm/run-command?view=azure-cli-latest#az-vm-run-command-create) . I am running this on a Windows 10 workstation, communicating with a Windows Server VM in Azure, where I wish to run a remote PowerShell script.
I am trying to understand how to access the parameters that I pass in via the az cli command. Usually for the “invoke” equivalent, I would just add the parameters switch and then they would be accessible to the remote VM where I run the command via a param block, i.e.:
az vm run-command invoke:
az vm run-command invoke --command-id RunPowerShellScript `
-n $vm.name `
-g $vm.name `
--parameters "param1=foo" "param2=bar" `
--scripts "@Azure\Scripts\myScript.ps1"
PowerShell script:
param (
[string]
$param1,
[string]
$param2
)
Write-Host "Do I have param1? $param1"
Write-Host "Do I have param2? $param2"
This works just fine, however I want to leverage the additional functionality of the new az vm run-command create commands to access protected params as well as the timeout element. If I run the same script using this format, it does not give me the parameters as I expect:
az vm run-command create:
$dateNow = get-date -Format yyyyMMdd_hhmmss -AsUTC
$commandName = "test_$dateNow"
az vm run-command create --run-command-name $commandName `
--vm-name $vm.name `
--resource-group $vm.resourcegroup `
--async-execution false `
--timeout-in-seconds 60 `
--parameters "param1=foo" `
--protected-parameters "param2=bar" `
--script "@Azure\Scripts\myScript.ps1"
Output returned is:
error : C:\Packages\Plugins\Microsoft.CPlat.Core.RunCommandHandlerWindows\2.0.3\Downloa
ds\Script_test_20220527_035311_0.ps1 : Cannot bind parameter because parameter
'param1' is specified more than once. To provide multiple values to parameters
that can accept multiple values, use the array syntax. For example,
"-parameter value1,value2,value3".
+ CategoryInfo : InvalidArgument: (:) [Script_test_20220527_03531
1_0.ps1], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterAlreadyBound,Script_test_20220527_03531
1_0.ps1
Thinking that this means the az cli command is automatically creating params for me, I have tried removing the param block from the PowerShell script. This does not work (though it does remove the error and completes with a success status), as I do not see any param values:
Output returned is:
output : Do I have param1?
Do I have param2?
Would I be able to ask for some guidance on how I can correctly provide params to a PowerShell script being executed and/or adjust my PowerShell script to access these params? I have reviewed the documentation but the only info on params is “The parameters used by the script”. Thanks.
az version output is as follows for reference:
{
"azure-cli": "2.37.0",
"azure-cli-core": "2.37.0",
"azure-cli-telemetry": "1.0.6",
"extensions": {
"aks-preview": "0.5.63",
"automation": "0.1.1",
"azure-devops": "0.25.0",
"connectedk8s": "1.2.8",
"containerapp": "0.3.1",
"resource-graph": "2.1.0"
}
}
Issue Analytics
- State:
- Created a year ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
@yanzhudd - Yes I can confirm this behaves as expected. I can now access regular and protected parameters as desired. Protected parameters are also not exposed in the settings file on the VM, also as expected. It looks like #23175 will fix the issue correctly. Thank you!
Hello @BCOps, we fix some relevant content. You can use a private package in blow link to check if it meets your expectations.
https://dev.azure.com/azure-sdk/public/_build/results?buildId=1700800&view=artifacts&pathAsName=false&type=publishedArtifacts