question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to access parameters in az vm run-command create

See original GitHub issue

I 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:closed
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
BCOpscommented, Jul 13, 2022

@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!

1reaction
yanzhuddcommented, Jul 12, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

az vm run-command | Microsoft Learn
List run commands from a VM or a location. You can specify "--resource-group" and "--vm-name" to get all run commands of a virtual...
Read more >
Azure Run Command for Dummies - Mandiant
Administrators can execute a PowerShell script by name using the following Azure CLI command. az vm run-command invoke --command-id ...
Read more >
az vm run-command parameters in key=value format ...
1 Answer 1 ... For the command az vm run-command invoke , the result that you posted is right and it just shows...
Read more >
Running PowerShell scripts remotely on Azure virtual machines
The Azure CLI uses az vm run-command invoke to run PowerShell scripts on Azure VMs. To use this command to run a PowerShell...
Read more >
How to run scripts in your virtual machine with the Run ...
Navigate to a virtual machine in the Azure portal and select Run command from the left menu under Activities. You will see a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found