Set-AzVMRunCommand succeeded but Invoke-AzVMRunCommand failed for unknown reason

124 Views Asked by At

I can use Set-AzVMRunCommand to create a command (successfully) on Azure VM. I am wondering how to invoke it (more than once)?

Set-AzVMRunCommand -ResourceGroupName $resourceGroup -VMName $vmName -Location $location -RunCommandName "foobar" -ScriptLocalPath ".\Run-App.ps1" -RunAsUser $vmUser -RunAsPassword $vmPass

I see my command is created successfully:

>> az vm run-command list --resource-group "hvtt1" --vm-name "testvm"

[
  {
    "asyncExecution": false,
    "errorBlobManagedIdentity": null,
    "errorBlobUri": null,
    "id": "/subscriptions/c1097bf0-4f64-4ee0-b922-7dd05a5965fe/resourceGroups/hvtt1/providers/Microsoft.Compute/virtualMachines/testvm/runCommands/foobar",
    "instanceView": null,
    "location": "eastus2",
    "name": "foobar",
    "outputBlobManagedIdentity": null,
    "outputBlobUri": null,
    "parameters": null,
    "protectedParameters": null,
    "provisioningState": "Succeeded",
    "resourceGroup": "hvtt1",
    "runAsPassword": null,
    "runAsUser": "user",
    "source": {
      "commandId": null,
      "script": "Write-Host \"Finished script initialization\";",
      "scriptUri": null,
      "scriptUriManagedIdentity": null
    },
    "tags": null,
    "timeoutInSeconds": 0,
    "treatFailureAsDeploymentFailure": null,
    "type": "Microsoft.Compute/virtualMachines/runCommands"
  }
]

But I can't invoke it where I use ARM ID of the command as $id

Invoke-AzVMRunCommand         `
  -ResourceGroupName $resourceGroup        `
  -VMName $vmName                          `
  -CommandId $id -Verbose

Error I am getting:

VERBOSE: Performing the operation "Invoke" on target "testvm".
Invoke-AzVMRunCommand : The entity was not found in this Azure location.
ErrorCode: NotFound
ErrorMessage: The entity was not found in this Azure location.
ErrorTarget: 
StatusCode: 404
ReasonPhrase: 
OperationID : 6ae3c2f5-7328-4de1-b1d2-a78d85576351
At line:12 char:1
+ Invoke-AzVMRunCommand         `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Invoke-AzVMRunCommand], ComputeCloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.InvokeAzureRmVMRunCommand
1

There are 1 best solutions below

0
Venkat V On

But I can't invoke it where I use ARM ID of the command as $id

ErrorMessage: The entity was not found in this Azure location.

Alternatively, you can also use az vm run-command create to execute a script on the VM with the existing VM resource ID.

 az vm run-command create --resource-group  "myResourceGroup"  --location  "West US"  --command-id

After running the above command, you can list all deployed RunCommand resources on a VM by using the command below.

az vm run-command list --vm-name  "myVM"  --resource-group  "myRG"

Reference: Run scripts in your Windows VM by using managed Run Commands

az vm run-command