Azure Powershell 命令无法在我的资源组下找到我的 VM?

Azure Powershell command cannot find my VM under my resource group?

我正在尝试更改我在 Azure 中的 Ubuntu VM 上的 TCP 端口超时,并且正在按照 this guide 进行操作,但是我似乎卡在了第 8 步输入以下命令:

Get-AzureRmVM -Name "digitron" -ResourceGroup "DIGITRON-RG" | Get-AzureRmPublicIpAddress

它返回以下错误的地方:

Get-AzureRmPublicIpAddress : The Resource 'Microsoft.Network/publicIPAddresses/digitron' under resource group 'DIGITRON-RG' was not found. StatusCode: 404 ReasonPhrase: Not Found OperationID : '5031da35-262c-4e1a-a85b-885a6a0fd36c' At line:1 char:63 + ... "digitron" -ResourceGroup "DIGITRON-RG" | Get-AzureRmPublicIpAddress + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzureRmPublicIpAddress], NetworkCloudException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Network.GetAzurePublicIpAddressCommand

奇怪的是,如果我 运行 命令 Get-AzureRmVm,powershell 会返回:

ResourceGroupName Name Location VmSize                OsType NIC ProvisioningState
DIGITRON-RG           digitron eastus Standard_DS2_v2 Linux digitron727 Succeeded

现在阅读错误让我认为 VM 本身没有 public IP 地址,但我已经在 Azure 门户中设置了它,如图所示(其中显示 40.71.98.172 等) :

为什么 Powershell 给我这个错误?

The Resource 'Microsoft.Network/publicIPAddresses/digitron' under resource group 'DIGITRON-RG' was not found.

因为Get-AzureRmPublicIpAddress无法获取正确的参数,这个错误意味着在DIGITRON-RG中找不到资源。对于测试,我们可以使用Get-AzureRmResource来测试资源是否存在:

PS > Get-AzureRmResource |  ?{$_.name = "digitron"}

对了,命令Get-AzureRmPublicIpAddress需要参数:-Name(public IP地址的名称), -ResourceGroupName(资源组名称)

PS > Get-AzureRmPublicIpAddress -Name "jason-ip" -ResourceGroupName "jason"


Name                     : jason-ip
ResourceGroupName        : jason
Location                 : eastus
Id                       : /subscriptions/5xxxxabb-222b-49c3-9488-0361e29a7b15/resourceGroups/jason/providers/Microsoft.Network/publicIPAddresses/jason-ip
Etag                     : W/"5a7200b2-7c2b-4c7a-be27-0bbb7c8f4665"
ResourceGuid             : 32xxxf-750a-46a4-abda-c25xxx2b64
ProvisioningState        : Succeeded
Tags                     :
PublicIpAllocationMethod : Dynamic
IpAddress                : 13.92.255.103
PublicIpAddressVersion   : IPv4
IdleTimeoutInMinutes     : 30
IpConfiguration          : {
                             "Id": "/subscriptions/5xxxxb-222b-49c3-9xx8-0361e29a7b15/resourceGroups/jason/providers/Microsoft.Network/networkInterfaces/jason647/ipConfigurations/ipconfig1"
                           }
DnsSettings              : null

我们可以使用此命令直接获取 public IP,并将 timrout 增加到 30 分钟。

下面是我用来获取 Azure ARM VM 的私有和 Public IP 的脚本:

$rg = Get-AzureRmResourceGroup -Name "MyResourceGroup01"
$vm = Get-AzureRmVM -ResourceGroupName $rg.ResourceGroupName -Name "MyVM01"
$nic = Get-AzureRmNetworkInterface -ResourceGroupName $rg.ResourceGroupName -Name $(Split-Path -Leaf $VM.NetworkProfile.NetworkInterfaces[0].Id)
$nic | Get-AzureRmNetworkInterfaceIpConfig | Select-Object Name,PrivateIpAddress,@{'label'='PublicIpAddress';Expression={Set-Variable -name pip -scope Global -value $(Split-Path -leaf $_.PublicIpAddress.Id);$pip}}
(Get-AzureRmPublicIpAddress -ResourceGroupName $rg.ResourceGroupName -Name $pip).IpAddress

#Output:    
Name      PrivateIpAddress PublicIpAddress
----      ---------------- ---------------
ipconfig1 10.0.0.10        MyVM01-pip

40.80.217.1