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.
- Tried after restarting the VMs
- Created a New VM to run the same script but same error
- 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
$Resource_Name
. This value should be theName of a VM
,such asMyVM
, not aresourceID
– Wayne Yang