1
votes

I am trying run an simple script on Azure VM (Windows) via Invoke-AzureRmVMRunCommand and also via Invoke-RestMethod cmdlet. Just trying to get the status of DHCP service.

  1. Tried after restarting the VMs
  2. Created a New VM to run the same script but same error
  3. Tried running the invoke command from a different machine

Script stored in a test.ps1 file

Get-Service DHCP

Command:

Invoke-AzureRmVMRunCommand -ResourceGroupName $Resource_Group -VMName $Resource_Name -CommandId RunPowerShellScript -ScriptPath 'C:\Vincent\Azure\AzureVM\Test.ps1'

I even tried using Azure API but same error: Command:

$Body = @"
{
  "commandId": "RunPowerShellScript",
  "script": [
    "Get-Service DHCP"
  ]
}
"@

$AppID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$Key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$TenantId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$SubscriptionID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

$GetToken = "https://login.microsoftonline.com/$TenantId/oauth2/token?tenant_id=$TenantId"
$Access_Token = Invoke-RestMethod -Method Post -Uri $GetToken -Body "grant_type=client_credentials&client_id=$AppID&client_secret=$Key&resource=$resource"
$Token = $Access_Token.access_token


$API = "https://management.azure.com/subscriptions/$SubscriptionID/resourceGroups/$Resource_Group/providers/Microsoft.Compute/virtualMachines/$VMName/runCommand?api-version=2017-03-30"
Invoke-RestMethod -Method Post -Uri $API -Headers @{Authorization = "Bearer $Token"} -Body $Body -ContentType 'application/json' -OutVariable Result

Error: Invoke-AzureRmVMRunCommand:

Invoke-AzureRmVMRunCommand : The Resource 'Microsoft.Compute/virtualMachines/XXXXXXXX' under resource group 'XXXXXX' was not found. ErrorCode: ResourceNotFound ErrorMessage: The Resource 'Microsoft.Compute/virtualMachines/XXXXXXXX' under resource group 'XXXXXX' was not found. StatusCode: 404 ReasonPhrase: Not Found OperationID : d5a9e664-92e2-45d6-b5e8-b3d5bd65814c At line:1 char:1 + Invoke-AzureRmVMRunCommand -ResourceGroupName $Resource_Group -VMName ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Invoke-AzureRmVMRunCommand], ComputeCloudException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.InvokeAzureRmVMRunCommand

Error API:

Invoke-RestMethod : { "error": { "code": "Conflict", "message": "Run command extension execution is in progress. Please wait for completion before invoking a run command." } } At line:23 char:1 + Invoke-RestMethod -Method Post -Uri $API -Headers @{Authorization = " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

3
According to the error, it may caused by the value of $Resource_Name. This value should be the Name of a VM,such as MyVM, not a resourceIDWayne Yang
I am passing name of the vm not id. $Resource_Name = 'Server201201'Vincent K
Sorry for that. Have you checked if this VM has been in that resource group?Wayne Yang
Yes. it is the same resource group. The same code was working yesterday.Vincent K
Um... That's weird. Did you do anything to change the VM? If you have any process on it, please update here.Wayne Yang

3 Answers

0
votes

Invoke-AzureRmVMRunCommand Run command on the VM.

You will receive the error message when you have given wrong “ResourceGroupName”.

You may check the screenshot below: I have provided wrong resource group and changed to correct resource group gives the successful.

enter image description here

In case, if it shows Run command extension execution is in progress, you can check the operation from Azure Portal => Activity log:

enter image description here

0
votes

You're getting a "resource not found" error:

Error: Invoke-AzureRmVMRunCommand:

Invoke-AzureRmVMRunCommand : The Resource 'Microsoft.Compute/virtualMachines/XXXXXXXX' under resource group 'XXXXXX' was not found. ErrorCode: ResourceNotFound ErrorMessage: The Resource 'Microsoft.Compute/virtualMachines/XXXXXXXX' under resource group 'XXXXXX' was not found. StatusCode: 404 ReasonPhrase: Not Found OperationID : d5a9e664-92e2-45d6-b5e8-b3d5bd65814c At line:1 char:1 + Invoke-AzureRmVMRunCommand -ResourceGroupName $Resource_Group -VMName ... +

+ CategoryInfo : CloseError: (:) [Invoke-AzureRmVMRunCommand], ComputeCloudException + FullyQualifiedErrorId :
Microsoft.Azure.Commands.Compute.Automation.InvokeAzureRmVMRunCommand

And then you're getting an error saying "You're running invoke-azurermvmruncommand", wait till that's completed:

Error API:

Invoke-RestMethod : { "error": { "code": "Conflict", "message": "Run command extension execution is in progress. Please wait for completion before invoking a run command." } } At line:23 char:1 + Invoke-RestMethod -Method Post -Uri $API -Headers @{Authorization = " ... +

+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod],
WebException + FullyQualifiedErrorId :
WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

run this to check if the VM is where it's supposed to be, and then your script:

get-azurermvm -resourcegroupname $Resource_Group -Name $Resource_Name
$file = "C:\Vincent\Azure\AzureVM\Test.ps1"
Invoke-AzureRmVMRunCommand -ResourceGroupName $Resource_Group -VMName $Resource_Name -CommandId RunPowerShellScript -ScriptPath $file

If that still doesn't work, please share with us $Resource_Name.GetType() and $Resource_Group.GetType()

0
votes

You need to call Set-AzureRmContext before calling Invoke-AzureRmVMRunCommand, by setting the -SubscriptionName from the Virtual Machine's Subscription.