我们在 Runbook 中使用以下代码,它返回的资源组名称值不正确。
$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"
}
调查错误消息后,我们得到以下错误:
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
这应该返回“COMPANY-INTERNAL-CLOUD”的值,因为这是它所在的资源组。
在命令中添加
-Name
,如下所示:$AzureVMs = Get-AzVM -Name "SH-COMPANY-AD-0"
在不指定参数的情况下使用 Get-AzVM 时,它将假定您正在请求
SH-COMPANY-AD-0
资源组中的所有虚拟机。始终指定 PowerShell 参数是一个很好的做法。
有关更多详细信息,请参阅 MS 文档中的 Get-AzVM。