We use the following code in a runbook and it is returning an incorrect value for the Resource Group Name.

$AzureVMs = Get-AzVM "SH-COMPANY-AD-0"

$AzureVMs | ForEach-Object {
    $AzureVM =  $_
    $job += Invoke-AzVmRunCommand `
    -ResourceGroupName $AzureVM.ResourceGroupName `
    -VMName $AzureVM.Name `
    -CommandId "RunPowerShellScript" `
    -ScriptPath "$env:TEMP\CleanerScript.ps1"
}

After investigating the error message we get the following error: Get-AzVM : Resource group 'SH-COMPANY-AD-0' could not be found. ErrorCode: ResourceGroupNotFound ErrorMessage: Resource group 'SH-COMPANY-AD-0' could not be found. ErrorTarget: StatusCode: 404 ReasonPhrase: Not Found OperationID : cea02a85-8524-4552-a243-a362c69f6d98 At line:16 char:13 + $AzureVMs = Get-AzVM "SH-COMPANY-AD-0" + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzVM], ComputeCloudException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.GetAzureVMCommand

This should be returning the value of "COMPANY-INTERNAL-CLOUD" as that is the resource group it is housed in.

1

There are 1 best solutions below

0
Scott Richards On

Add -Name in your command like so: $AzureVMs = Get-AzVM -Name "SH-COMPANY-AD-0"

When using Get-AzVM without specifying the argument, it will assume you are asking for all Virtual Machines in the SH-COMPANY-AD-0 resource group. It is good practice to always specify your PowerShell arguments.

See Get-AzVM in MS docs for more details.