# Azure Action Run Command

  
Azure Run Command is a feature that allows you to execute script or commands directly on your Azure virtual Machines (VMs) without logging in to them. The command will add an extension to your VM instance an execute the command or script. It simplifies management tasks but you may encounter some issues when using this feature. In this blog post, I will explore the command for Windows VMs.

## **Available commands for Windows VMs**

The table below shows a part of commands available for Windows VMs. You can find the complete list [here](https://learn.microsoft.com/en-us/azure/virtual-machines/windows/run-command#available-commands)

| **Name** | **Description** |
| --- | --- |
| RunPowerShellScript | Runs a PowerShell script |
| EnableRemotePS | Configures the machine to enable remote PowerShell. |
| EnableWindowsUpdate | Enable Windows Update Automatic Updates |

## Azure CLI

To run the command using Azure CLI, use the template below and adapt it to your needs. The script can be a file, please report to the command documentation.

```powershell
az vm run-command invoke  --command-id RunPowerShellScript --name <vmName> -g <rgName> \
--scripts 'param([string]$arg1,[string]$arg2)' 'Write-Host This is a sample script with parameters $arg1 and $arg2' --parameters 'arg1=somefoo' 'arg2=somebar'
```

## Permissions

Running a command requires the `Microsoft.Compute/virtualMachines/runCommands/action` permission. The Virtual Machine Contributor role and higher levels have this permission.

## **Action Run Command Windows troubleshooting**

I noticed a weird behavior from the Run Command that impacted our production. The script executed with the run command fails and that script was executed each time the Azure VM was restarted. The Instance was trying to install the extension on the VM even if it failed on the first run.

### Log location

The run command for Windows log is usually located in the following directory: `C:\WindowsAzure\Logs\Plugins\Microsoft.CPlat.Core.RunCommandWindows\<version>\RunCommandExtension.log`

### Remove the extension

If you need to remove the extension from your instance, use the command below and adapt it your needs.

```powershell
az vm run-command invoke  --command-id RemoveRunCommandWindowsExtension --name <vmName> -g <rgName>
```

## Conclusion

The Run Command feature can help you quickly diagnose VM issues. You can use the feature through the Azure portal, REST API, PowerShell for Windows VMs or Azure CLI
