How to use Azure CLI to run custom command on VM

Kyriacos 21 Reputation points
2022-12-10T01:50:06.093+00:00

I created a custom Azure VM command:

   $ az vm run-command create --vm-name $VM_NAME --resource-group $RESOURCE_GROUP --location $LOCATION --subscription $SUBSCRIPTION_ID --run-command-name "MyCustomCommand" --script "echo Hello"  

I verified it provisioned successfully

   $ az vm run-command list --resource-group $RESOURCE_GROUP --vm-name $VM_NAME --location $LOCATION --subscription $SUBSCRIPTION_ID  
     
   {  
     "asyncExecution": false,  
     "errorBlobUri": null,  
     "id": "/subscriptions/[subscription-id]/resourceGroups/limonrg/providers/Microsoft.Compute/virtualMachines/limonvm/runCommands/MyCustomCommand",  
     "instanceView": null,  
     "location": "SwitzerlandNorth",  
     "name": "MyCustomCommand",  
     "outputBlobUri": null,  
     "parameters": null,  
     "protectedParameters": null,  
     "provisioningState": "Succeeded",  
     "resourceGroup": "limonrg",  
     "runAsPassword": null,  
     "runAsUser": null,  
     "source": {  
       "commandId": null,  
       "script": "echo Hello",  
       "scriptUri": null  
     },  
     "tags": null,  
     "timeoutInSeconds": 0,  
     "type": "Microsoft.Compute/virtualMachines/runCommands"  
   }  

Then I try to run it and get an error:

   $ az vm run-command invoke --command-id "/subscriptions/[subscription_id]/resourceGroups/limonrg/providers/Microsoft.Compute/virtualMachines/limonvm/runCommands/MyCustomCommand" --name $VM_NAME --resource-group $RESOURCE_GROUP --subscription $SUBSCRIPTION_ID  
     
   (NotFound) The entity was not found in this Azure location.  
   Code: NotFound  
   Message: The entity was not found in this Azure location.  

Same thing if I use the command name instead of the entire id:

   $ az vm run-command invoke --command-id MyCustomCommand --name $VM_NAME --resource-group $RESOURCE_GROUP --subscription $SUBSCRIPTION_ID  
     
   NotFound) The entity was not found in this Azure location.  
   Code: NotFound  
   Message: The entity was not found in this Azure location.  

What am I doing wrong? Unfortunately Azure Docs do not provide a sample for custom commands.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
0 comments No comments
{count} votes

Answer accepted by question author
  1. TP 145.2K Reputation points Volunteer Moderator
    2022-12-10T05:57:38.443+00:00

    Hi,

    az vm run-command create sends the script to the VM and executes it. You do not try to invoke it as you did in your post. To see current status, start time, stop time, output, etc., you can run command similar to below:

    az vm run-command show --run-command-name MyCustomCommand --vm-name $VM_NAME --resource-group $RESOURCE_GROUP --instance-view  
    

    See below article for details on using Managed Run commands in Windows VM (link to Linux is on left):

    https://learn.microsoft.com/en-us/azure/virtual-machines/windows/run-command-managed

    Article below explains Action vs Managed run commands:

    https://learn.microsoft.com/en-us/azure/virtual-machines/run-command-overview

    -TP


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.