%3CLINGO-SUB%20id%3D%22lingo-sub-292446%22%20slang%3D%22en-US%22%3EHow%20to%20Run%20scripts%20on%20Azure%20Virtual%20Machines%20with%20the%20Run%20Command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292446%22%20slang%3D%22en-US%22%3E%3CP%3EI%20often%20need%20to%20run%20a%20simple%20task%20or%20retrieve%20information%20from%20an%20Azure%20VM.%20Most%20of%20the%20time%20I%20want%20to%20do%20so%20without%20creating%20an%20RDP%20or%20SSH%20session%2C%20exposing%20a%20WinRM%20or%20SSH%20port%2C%20and%20in%20some%20cases%20need%20to%20do%20so%20on%20a%20VM%20that%20is%20isolated%20from%20the%20internet.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20past%20I%20have%20relied%20on%20the%20Azure%20Virtual%20Machine%20Custom%20Script%20Extension%20for%20these%20types%20of%20operations.%20The%20Custom%20Script%20extension%20uses%20the%20Azure%20VM%20Agent%20to%20download%20and%20run%20scripts%20on%20a%20virtual%20machine.%20It%20is%20useful%20when%20needing%20to%20perform%20a%20VM%20operation%20or%20configuration%2C%20however%20is%20somewhat%20heavy%20weight%20and%20has%20one%20in%20particular%20trait%20that%20I%20have%20found%20cumbersome.%20Once%20the%20Custom%20Script%20Extension%20has%20been%20added%20to%20a%20VM%2C%20it%20needs%20to%20then%20be%20removed%20before%20another%20instance%20can%20be%20run.%20This%20is%20not%20a%20difficult%20operation%20but%20when%20running%20multiple%20scripts%20against%20a%20VM%20is%20less%20than%20desirable.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERecently%20an%20alternate%20and%20more%20lightweight%20method%20for%20running%20scripts%20against%20Azure%20VMs%20has%20been%20released%3B%20the%20Azure%20Virtual%20Machine%20Run%20Command.%20Using%20a%20Run%20Command%2C%20PowerShell%20or%20Bash%20scripts%20can%20be%20run%20against%20virtual%20machines%20with%20many%20of%20the%20same%20benefits%20as%20the%20custom%20script%20extension%2C%20however%20multiple%20instances%20can%20be%20run%20without%20the%20need%20of%20any%20type%20of%20clean-up%20action.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20document%20I%20walk%20through%20some%20steps%20for%20using%20the%20Azure%20Virtual%20Machine%20Run%20Command%20action%20and%20also%20provide%20some%20troubleshooting%20information.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20detailed%20information%2C%20see%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-machines%2Fwindows%2Frun-command%3FWT.mc_id%3DITOpsTalk-blog-nepeters%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ERun%20Command%20documentation%20on%20docs.microsoft.com%3C%2FA%3E.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1652956395%22%20id%3D%22toc-hId--1443127402%22%20id%3D%22toc-hId--1443127402%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22azureportal%22%20id%3D%22toc-hId--899200566%22%20id%3D%22toc-hId-1044385431%22%20id%3D%22toc-hId-1044385431%22%3EAzure%20portal%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EScripts%20can%20be%20run%20directly%20from%20a%20virtual%20machine%20in%20the%20Azure%20portal.%20To%20do%20so%2C%20select%20the%20VM%20and%20Run%20command.%20From%20here%20select%20a%20pre-created%20operation%20or%20RunPowerShellScript%20%2F%20RunShellScript.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60926i077DA2A979A6DA68%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221.png%22%20title%3D%221.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnter%20the%20command%20%2F%20script%20that%20you%20would%20like%20to%20run%20on%20the%20VM%20and%20click%20run.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20940px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60927iA4A97FE77CA280EE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%222.png%22%20title%3D%222.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-843609769%22%20id%3D%22toc-hId--763069032%22%20id%3D%22toc-hId--763069032%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22azurepowershellcore%22%20id%3D%22toc-hId--1708547192%22%20id%3D%22toc-hId-1724443801%22%20id%3D%22toc-hId-1724443801%22%3EAzure%20PowerShell%20(Core)%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20run%20command%20can%20be%20triggered%20using%20PowerShell%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EInvoke-AzVMRunCommand%20-ResourceGroupName%20runcmd%20-VMName%20win-runcmd%20-CommandId%20RunPowerShellScript%20-ScriptPath%20.%2Frun-command.ps1%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20output%20will%20look%20similar%20to%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EValue%5B0%5D%20%20%20%20%20%20%20%20%3A%0A%20%20Code%20%20%20%20%20%20%20%20%20%20%3A%20ComponentStatus%2FStdOut%2Fsucceeded%0A%20%20Level%20%20%20%20%20%20%20%20%20%3A%20Info%0A%20%20DisplayStatus%20%3A%20Provisioning%20succeeded%0A%20%20Message%20%20%20%20%20%20%20%3A%20%20%20%20%20Directory%3A%20C%3A%5C%0A%0A%0AMode%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20LastWriteTime%20%20%20%20%20%20%20%20%20Length%20Name%0A----%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-------------%20%20%20%20%20%20%20%20%20------%20----%0A-a----%20%20%20%20%20%20%2011%2F28%2F2018%20%20%209%3A48%20PM%20%20%20%20%20%20%20%20%20%20%20%20%20%200%20test.txt%0A%0A%0AValue%5B1%5D%20%20%20%20%20%20%20%20%3A%0A%20%20Code%20%20%20%20%20%20%20%20%20%20%3A%20ComponentStatus%2FStdErr%2Fsucceeded%0A%20%20Level%20%20%20%20%20%20%20%20%20%3A%20Info%0A%20%20DisplayStatus%20%3A%20Provisioning%20succeeded%0A%20%20Message%20%20%20%20%20%20%20%3A%0AStatus%20%20%20%20%20%20%20%20%20%20%3A%20Succeeded%0ACapacity%20%20%20%20%20%20%20%20%3A%200%0ACount%20%20%20%20%20%20%20%20%20%20%20%3A%200%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId-34263143%22%20id%3D%22toc-hId--83010662%22%20id%3D%22toc-hId--83010662%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22azurecli%22%20id%3D%22toc-hId-1777073478%22%20id%3D%22toc-hId--1890465125%22%20id%3D%22toc-hId--1890465125%22%3EAzure%20CLI%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20run%20command%20can%20also%20be%20triggered%20using%20the%20Azure%20CLI%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Eaz%20vm%20run-command%20invoke%20--resource-group%20runcmd%20--name%20linux-runcmd%20--scripts%20%22sudo%20touch%20%2Ftest.txt%22%20--command-id%20RunShellScript%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20output%20will%20look%20similar%20to%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3E%7B%0A%20%20%22value%22%3A%20%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%22code%22%3A%20%22ProvisioningState%2Fsucceeded%22%2C%0A%20%20%20%20%20%20%22displayStatus%22%3A%20%22Provisioning%20succeeded%22%2C%0A%20%20%20%20%20%20%22level%22%3A%20%22Info%22%2C%0A%20%20%20%20%20%20%22message%22%3A%20%22Enable%20succeeded%3A%20%5Cn%5Bstdout%5D%5Cn%5Cn%5Bstderr%5D%5Cn%22%2C%0A%20%20%20%20%20%20%22time%22%3A%20null%0A%20%20%20%20%7D%0A%20%20%5D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId--775083483%22%20id%3D%22toc-hId-597047708%22%20id%3D%22toc-hId-597047708%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22troubleshooting%22%20id%3D%22toc-hId-967726852%22%20id%3D%22toc-hId--1210406755%22%20id%3D%22toc-hId--1210406755%22%3ETroubleshooting%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20do%20run%20into%20a%20situation%20in%20which%20client-side%20troubleshooting%20is%20required%2C%20here%20are%20a%20few%20spots%20to%20check%20out.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWindows%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOn%20Windows%20the%20downloaded%20script%20can%20be%20located%20in%20the%20following%20directory%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EC%3A%5CPackages%5CPlugins%5CMicrosoft.CPlat.Core.RunCommandWindows%5C1.1.0%5CDownloads%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20the%20script%20execution%20output%20here%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EC%3A%5CPackages%5CPlugins%5CMicrosoft.CPlat.Core.RunCommandWindows%5C1.1.0%5CStatus%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20run%20command%20handler%20logs%20can%20be%20found%20here%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EC%3A%5CWindowsAzure%5CLogs%5CPlugins%5CMicrosoft.CPlat.Core.RunCommandWindows%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ELinux%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20script%2C%20stderr%2C%20and%20stdout%20output%20are%20located%20here%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3E%2Fvar%2Flib%2Fwaagent%2Frun-command%2Fdownload%2F%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20run%20command%20handler%20logs%20can%20be%20found%20here%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3E%2Fvar%2Flog%2Fazure%2Frun-command%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-292446%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EMicrosoft%20recently%20released%20a%20more%20lightweight%20method%20for%20running%20scripts%20against%20Azure%20VMs%3B%20the%20Azure%20Virtual%20Machine%20Run%20Command.%20Using%20a%20Run%20Command%2C%20PowerShell%20or%20Bash%20scripts%20can%20be%20run%20against%20virtual%20machines%20with%20many%20of%20the%20same%20benefits%20as%20the%20custom%20script%20extension%2C%20however%20multiple%20instances%20can%20be%20run%20without%20the%20need%20of%20any%20type%20of%20clean-up%20action.%20%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-292446%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ENeil%20Peterson%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1474031%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20Run%20scripts%20on%20Azure%20Virtual%20Machines%20with%20the%20Run%20Command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1474031%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20testing%20a%20script%20and%20for%20some%20reason%20it%20got%20stuck%2C%20and%20I%20could%20not%20find%20a%20way%20to%20cancel%20or%20kill%20the%20execution.%20Being%20force%20to%20wait%20for%2090%20minutes%20for%20a%20single%20test%20makes%20no%20sense%2C%20you%20should%20provide%20an%20option%20to%20kill%20the%20pending%20executions.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

I often need to run a simple task or retrieve information from an Azure VM. Most of the time I want to do so without creating an RDP or SSH session, exposing a WinRM or SSH port, and in some cases need to do so on a VM that is isolated from the internet.

 

In the past I have relied on the Azure Virtual Machine Custom Script Extension for these types of operations. The Custom Script extension uses the Azure VM Agent to download and run scripts on a virtual machine. It is useful when needing to perform a VM operation or configuration, however is somewhat heavy weight and has one in particular trait that I have found cumbersome. Once the Custom Script Extension has been added to a VM, it needs to then be removed before another instance can be run. This is not a difficult operation but when running multiple scripts against a VM is less than desirable.

 

Recently an alternate and more lightweight method for running scripts against Azure VMs has been released; the Azure Virtual Machine Run Command. Using a Run Command, PowerShell or Bash scripts can be run against virtual machines with many of the same benefits as the custom script extension, however multiple instances can be run without the need of any type of clean-up action.

 

In this document I walk through some steps for using the Azure Virtual Machine Run Command action and also provide some troubleshooting information.

 

For detailed information, see the Run Command documentation on docs.microsoft.com.

 

Azure portal

 

Scripts can be run directly from a virtual machine in the Azure portal. To do so, select the VM and Run command. From here select a pre-created operation or RunPowerShellScript / RunShellScript.

 

1.png

 

Enter the command / script that you would like to run on the VM and click run.

 

2.png

 

Azure PowerShell (Core)

 

A run command can be triggered using PowerShell like this:

 

Invoke-AzVMRunCommand -ResourceGroupName runcmd -VMName win-runcmd -CommandId RunPowerShellScript -ScriptPath ./run-command.ps1

 

The output will look similar to:

 

Value[0]        :
  Code          : ComponentStatus/StdOut/succeeded
  Level         : Info
  DisplayStatus : Provisioning succeeded
  Message       :     Directory: C:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       11/28/2018   9:48 PM              0 test.txt


Value[1]        :
  Code          : ComponentStatus/StdErr/succeeded
  Level         : Info
  DisplayStatus : Provisioning succeeded
  Message       :
Status          : Succeeded
Capacity        : 0
Count           : 0

 

Azure CLI

 

A run command can also be triggered using the Azure CLI like this:

 

az vm run-command invoke --resource-group runcmd --name linux-runcmd --scripts "sudo touch /test.txt" --command-id RunShellScript

 

The output will look similar to:

 

{
  "value": [
    {
      "code": "ProvisioningState/succeeded",
      "displayStatus": "Provisioning succeeded",
      "level": "Info",
      "message": "Enable succeeded: \n[stdout]\n\n[stderr]\n",
      "time": null
    }
  ]
}

 

Troubleshooting

 

If you do run into a situation in which client-side troubleshooting is required, here are a few spots to check out.

 

Windows

 

On Windows the downloaded script can be located in the following directory:

 

C:\Packages\Plugins\Microsoft.CPlat.Core.RunCommandWindows\1.1.0\Downloads

 

With the script execution output here:

 

C:\Packages\Plugins\Microsoft.CPlat.Core.RunCommandWindows\1.1.0\Status

 

The run command handler logs can be found here:

 

C:\WindowsAzure\Logs\Plugins\Microsoft.CPlat.Core.RunCommandWindows

 

Linux

 

The script, stderr, and stdout output are located here:

 

/var/lib/waagent/run-command/download/

 

The run command handler logs can be found here:

 

/var/log/azure/run-command

 

 

1 Comment
Occasional Visitor

I am testing a script and for some reason it got stuck, and I could not find a way to cancel or kill the execution. Being force to wait for 90 minutes for a single test makes no sense, you should provide an option to kill the pending executions.