%3CLINGO-SUB%20id%3D%22lingo-sub-1362360%22%20slang%3D%22en-US%22%3EHow%20to%20run%20scripts%20in%20your%20Azure%20VM%20by%20using%20Run%20Command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1362360%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20you%20know%2C%20you%20can%20access%20your%20Azure%20virtual%20machine%20(VM)%20in%20multiple%20ways%2C%20like%20SSH%20or%20RDP.%20However%2C%20if%20you%20have%20issues%20with%20the%20RDP%20or%20SSH%20network%20configuration%2C%20the%20Run%20Command%20feature%20is%20another%20option.%20Run%20Command%20can%20run%20a%20PowerShell%20or%20shell%20script%20within%20an%20Azure%20VM%20remotely%20by%20using%20the%20VM%20agent.%20This%20scenario%20is%20especially%20useful%20when%20you%20need%20to%20troubleshoot%20operating%20system%20network%20configurations%20or%20user%20access%20configuration.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20use%20Run%20Command%20for%20Azure%20VMs%20through%20the%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Ffeatures%2Fazure-portal%3FWT.mc_id%3Ditopstalk-blog-thmaure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20portal%3C%2FA%3E%2C%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Frest%2Fapi%2Fcompute%2Fvirtual%2520machines%2520run%2520commands%2Fruncommand%3FWT.mc_id%3Ditopstalk-blog-thmaure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-linktype%3D%22absolute-path%22%3EREST%20API%3C%2FA%3E%3CSPAN%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Fvm%2Frun-command%3FWT.mc_id%3Ditopstalk-blog-thmaure%23az-vm-run-command-invoke%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20CLI%3C%2FA%3E%2C%20or%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fpowershell%2Fmodule%2Faz.compute%2Finvoke-azvmruncommand%3FWT.mc_id%3Ditopstalk-blog-thmaure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-linktype%3D%22external%22%3EPowerShell%3C%2FA%3E.%20Here%20are%20some%20examples%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1353602414%22%20id%3D%22toc-hId--1353602414%22%20id%3D%22toc-hId--1353602414%22%3EAzure%20Portal%3C%2FH2%3E%0A%3CP%3EYou%20can%20run%20commands%20directly%20from%20the%20Azure%20Portal.%20In%20the%20menu%20of%20the%20Azure%20VM%2C%20you%20can%20select%20Run%20command.%20Here%20you%20can%20find%20some%20predefined%20scripts%20to%20troubleshoot%20your%20Azure%20VM.%20In%20the%20case%20of%20a%20Windows%20VM%2C%20you%20will%20find%20scripts%20like%20configuring%20RDP%20port%20or%20enable%20PowerShell%20remoting.%20But%20you%20can%20also%20run%20your%20custom%20PowerShell%20script.%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-center%22%20image-alt%3D%22Azure%20VM%20Run%20Command%20Run%20PowerShell%20Script.jpg%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F189412i60F78B4B3284DF14%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Azure%20VM%20Run%20Command%20Run%20PowerShell%20Script.jpg%22%20alt%3D%22Azure%20VM%20Run%20Command%20Run%20PowerShell%20Script%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20VM%20Run%20Command%20Run%20PowerShell%20Script%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3BFor%20Linux%20VMs%2C%20you%20will%20find%20predefined%20options%20to%20run%20a%20Linux%20shell%20script%20or%20ifconfig%20to%20list%20the%20network%20configuration.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1133910419%22%20id%3D%22toc-hId-1133910419%22%20id%3D%22toc-hId-1133910419%22%3EAzure%20CLI%3C%2FH2%3E%0A%3CP%3EYou%20also%20run%20commands%20directly%20from%20the%20Azure%20CLI.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eaz%20vm%20run-command%20invoke%20-g%20myResourceGroup%20-n%20myVm%20--command-id%20RunShellScript%20--scripts%20%22sudo%20apt-get%20update%20%26amp%3B%26amp%3B%20sudo%20apt-get%20install%20-y%20nginx%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20az%20vm%20run-command%20CLI%20command%20works%20with%20Linux%20and%20Windows%20VMs.%20You%20can%20find%20more%20information%20about%20the%20Azure%20CLI%20option%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcli%2Fazure%2Fvm%2Frun-command%3FWT.mc_id%3Ditopstalk-blog-thmaure%23az-vm-run-command-invoke%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--673544044%22%20id%3D%22toc-hId--673544044%22%20id%3D%22toc-hId--673544044%22%3EAzure%20PowerShell%3C%2FH2%3E%0A%3CP%3EYou%20can%20also%20use%20Azure%20PowerShell%20to%20use%20the%20run%20command%20capabilities%20to%20run%20PowerShell%20scripts%20against%20the%20guest%20agent%20inside%20the%20Azure%20VM.%20For%20that%2C%20you%20can%20simply%20use%20the%20Invoke-AzVMRunCommand%20cmdlet%20from%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fazure%2Fnew-azureps-module-az%3FWT.mc_id%3Ditopstalk-blog-thmaure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAz%20PowerShell%20module%3C%2FA%3E.%20You%20can%20also%20run%20this%20command%20directly%20from%20%3CA%20href%3D%22https%3A%2F%2Fwww.thomasmaurer.ch%2F2019%2F01%2Fazure-cloud-shell%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Cloud%20Shell%3C%2FA%3E%20as%20well.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EInvoke-AzVMRunCommand%20-ResourceGroupName%20'%3CMYRESOURCEGROUP%3E'%20-Name%20'%3CMYVMNAME%3E'%20-CommandId%20'RunPowerShellScript'%20-ScriptPath%20'%3CPATHTOSCRIPT%3E'%20-Parameter%20%40%7B%22arg1%22%20%3D%20%22var1%22%3B%22arg2%22%20%3D%20%22var2%22%7D%3C%2FPATHTOSCRIPT%3E%3C%2FMYVMNAME%3E%3C%2FMYRESOURCEGROUP%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20cmdlet%20expects%20the%20script%20referenced%20in%20the%20-ScriptPath%20parameter%20to%20be%20local%20to%20where%20the%20cmdlet%20is%20being%20run.%20If%20you%20are%20running%20it%20from%20your%20local%20machine%2C%20the%20script%20will%20need%20to%20be%20stored%20on%20your%20machine.%20If%20you%20are%20running%20the%20script%20from%20Cloud%20Shell%2C%20you%20will%20need%20to%20have%20the%20script%20available%20in%20your%20Cloud%20Shell.%20You%20can%20find%20more%20information%20about%20the%20Invoke-AzVMRunCommand%20PowerShell%20cmdlet%20option%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Faz.compute%2Finvoke-azvmruncommand%3FWT.mc_id%3Ditopstalk-blog-thmaure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1813968789%22%20id%3D%22toc-hId-1813968789%22%20id%3D%22toc-hId-1813968789%22%3EConclusion%3C%2FH2%3E%0A%3CP%3EThe%20Run%20Command%20option%20is%20a%20great%20solution%20if%20you%20need%20to%20run%20scripts%20inside%20an%20Azure%20VM%20using%20the%20guest%20agent%20to%20troubleshoot%20network%20or%20access%20configurations.%20You%20can%20learn%20more%20about%20the%20Run%20Command%20here%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-machines%2Fwindows%2Frun-command%3FWT.mc_id%3Ditopstalk-blog-thmaure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAz%20Command%20Windows%20VMs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-machines%2Flinux%2Frun-command%3FWT.mc_id%3Ditopstalk-blog-thmaure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAz%20Command%20Linux%20VMs%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EYou%20can%20also%20find%20more%20information%20about%20the%20Azure%20Virtual%20Machine%20guest%20agent%20here%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-machines%2Fextensions%2Fagent-windows%3FWT.mc_id%3Ditopstalk-blog-thmaure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EWindows%20Agent%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-machines%2Fextensions%2Fagent-linux%3FWT.mc_id%3Ditopstalk-blog-thmaure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ELinux%20Agent%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EI%20hope%20this%20blog%20post%20was%20helpful.%20If%20you%20have%20any%20questions%20or%20comments%2C%20feel%20free%20to%20leave%20a%20comment.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1362360%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EThe%20Run%20Command%20feature%20uses%20the%20virtual%20machine%20(VM)%20agent%20to%20run%20PowerShell%20and%20shell%20scripts%20within%20an%20Azure%20Windows%20and%20Linux%20VMs.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1362360%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EThomas%20Maurer%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1623230%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20run%20scripts%20in%20your%20Azure%20VM%20by%20using%20Run%20Command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1623230%22%20slang%3D%22en-US%22%3E%3CP%3EThat%20is%20really%20cool.%20By%20chance%2C%20is%20there%20a%20way%20to%20call%20scripts%20remotely%20(for%20example%2C%20using%20gist)%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

As you know, you can access your Azure virtual machine (VM) in multiple ways, like SSH or RDP. However, if you have issues with the RDP or SSH network configuration, the Run Command feature is another option. Run Command can run a PowerShell or shell script within an Azure VM remotely by using the VM agent. This scenario is especially useful when you need to troubleshoot operating system network configurations or user access configuration.

 

You use Run Command for Azure VMs through the Azure portalREST API, Azure CLI, or PowerShell. Here are some examples:

 

Azure Portal

You can run commands directly from the Azure Portal. In the menu of the Azure VM, you can select Run command. Here you can find some predefined scripts to troubleshoot your Azure VM. In the case of a Windows VM, you will find scripts like configuring RDP port or enable PowerShell remoting. But you can also run your custom PowerShell script.

 

Azure VM Run Command Run PowerShell ScriptAzure VM Run Command Run PowerShell Script

 For Linux VMs, you will find predefined options to run a Linux shell script or ifconfig to list the network configuration.

 

Azure CLI

You also run commands directly from the Azure CLI. 

 

 

 

 

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

 

 

 

 

The az vm run-command CLI command works with Linux and Windows VMs. You can find more information about the Azure CLI option here.

 

Azure PowerShell

You can also use Azure PowerShell to use the run command capabilities to run PowerShell scripts against the guest agent inside the Azure VM. For that, you can simply use the Invoke-AzVMRunCommand cmdlet from the Az PowerShell module. You can also run this command directly from Azure Cloud Shell as well.

 

 

 

 

Invoke-AzVMRunCommand -ResourceGroupName '<myResourceGroup>' -Name '<myVMName>' -CommandId 'RunPowerShellScript' -ScriptPath '<pathToScript>' -Parameter @{"arg1" = "var1";"arg2" = "var2"}

 

 

 

 

The cmdlet expects the script referenced in the -ScriptPath parameter to be local to where the cmdlet is being run. If you are running it from your local machine, the script will need to be stored on your machine. If you are running the script from Cloud Shell, you will need to have the script available in your Cloud Shell. You can find more information about the Invoke-AzVMRunCommand PowerShell cmdlet option here.

 

Conclusion

The Run Command option is a great solution if you need to run scripts inside an Azure VM using the guest agent to troubleshoot network or access configurations. You can learn more about the Run Command here:

You can also find more information about the Azure Virtual Machine guest agent here:

I hope this blog post was helpful. If you have any questions or comments, feel free to leave a comment.

 

1 Comment
Occasional Contributor

That is really cool. By chance, is there a way to call scripts remotely (for example, using gist)?