Reputation: 1346
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.
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
Upvotes: 1
Views: 5683
Reputation: 1
You need to call Set-AzureRmContext
before calling Invoke-AzureRmVMRunCommand
, by setting the -SubscriptionName
from the Virtual Machine's Subscription.
Upvotes: 0
Reputation: 116
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()
Upvotes: 0
Reputation: 12788
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.
In case, if it shows Run command extension execution is in progress, you can check the operation from Azure Portal => Activity log:
Upvotes: 0