az vm run-command

Commands

az vm run-command invoke Execute a specific run command on a vm.
az vm run-command list Lists all available run commands for a subscription in a location.
az vm run-command show Gets specific run command for a subscription in a location.

az vm run-command invoke

Execute a specific run command on a vm.

az vm run-command invoke --command-id
[--ids]
[--name]
[--parameters]
[--resource-group]
[--scripts]
[--subscription]

Examples

Install nginx on a linux VM.

az vm run-command invoke -g MyResourceGroup -n MyVm --command-id RunShellScript --scripts "sudo apt-get update && sudo apt-get install -y nginx"

Run shell command on a linux VM with parameters.

az vm run-command invoke -g MyResourceGroup -n MyVm --command-id RunShellScript --scripts 'echo $1 $2' --parameters hello world

Run powershell script on a windows VM with parameters. Script supplied inline. Be wary of single-quoting in CMD.exe.

az vm run-command invoke  --command-id RunPowerShellScript --name win-vm -g my-resource-group  \
    --scripts 'param([string]$arg1,[string]$arg2)' \
    'Write-Host This is a sample script with parameters $arg1 and $arg2' \
    --parameters 'arg1=somefoo' 'arg2=somebar'

Run powershell script on a windows VM with parameters. Script supplied from file.

# script.ps1
#   param(
#       [string]$arg1,
#       [string]$arg2
#   )
#   Write-Host This is a sample script with parameters $arg1 and $arg2

az vm run-command invoke  --command-id RunPowerShellScript --name win-vm -g my-resource-group \
    --scripts @script.ps1 --parameters "arg1=somefoo" "arg2=somebar"

Required Parameters

--command-id

The command id.

value from: az vm run-command list

Optional Parameters

--ids

One or more resource IDs (space-delimited). It should be a complete resource ID containing all information of 'Resource Id' arguments. If provided, no other 'Resource Id' arguments should be specified.

--name -n

The name of the Virtual Machine. You can configure the default using az configure --defaults vm=<name>.

--parameters

Space-separated parameters in the format of '[name=]value'.

--resource-group -g

Name of resource group. You can configure the default group using az configure --defaults group=<name>.

--scripts

Space-separated script lines. Use @{file} to load script from a file.

--subscription

Name or ID of subscription. You can configure the default subscription using az account set -s NAME_OR_ID.

az vm run-command list

Lists all available run commands for a subscription in a location.

az vm run-command list --location
[--subscription]

Required Parameters

--location -l

Location. Values from: az account list-locations. You can configure the default location using az configure --defaults location=<location>.

Optional Parameters

--subscription

Name or ID of subscription. You can configure the default subscription using az account set -s NAME_OR_ID.

az vm run-command show

Gets specific run command for a subscription in a location.

az vm run-command show --command-id
--location
[--subscription]

Examples

vm run-command show (autogenerated)

az vm run-command show --command-id RunShellScript --location westus2

Required Parameters

--command-id

The command id.

value from: az vm run-command list
--location -l

Location. Values from: az account list-locations. You can configure the default location using az configure --defaults location=<location>.

Optional Parameters

--subscription

Name or ID of subscription. You can configure the default subscription using az account set -s NAME_OR_ID.