【问题标题】:Azure Powershell - find the NIC based on the private IPAzure Powershell - 根据私有 IP 查找 NIC
【发布时间】:2022-07-30 00:42:30
【问题描述】:

我对 Azure 和 Powershell 非常陌生。我需要根据私有 IP 地址找到我的网络接口。我找到了“Get-AzNetworkInterface”cmdlet,我希望它只返回包含 IP 的条目。我注意到 IP 仅存在于“IpConfigurationsText”中,但不存在于仅包含对象名称的“IpConfigurations”中。我不知道这是否正常。返回的“IpConfigurationText”是一个列表(基于我有限的 python 经验),其中包含像这样的字典键值对

[
 {
   "Name": "xxxx",
   "Id": "xxxx",
   "PrivateIpAddress": "10.1.2.3",
   ...
 }
]

我想我想根据其内容进行过滤。我尝试了以下但没有成功...

Get-AzNetworkInterface | Where-Object { $_.IpConfigurationsText["PrivateIpAddress"] -contains "10.1.2.3" }
Get-AzNetworkInterface | Where-Object { $_.IpConfigurationsText[0]["PrivateIpAddress"] -eq "10.1.2.3" }

我也尝试在输出中只显示 IP 而不是字典键值对也没有成功

Get-AzNetworkInterface | select Name,IpConfigurationsText["PrivateIpAddress"]

让我知道我错过了什么。

顺便说一句,我还发现我可以使用“Out-GridView”来查看和过滤结果,但是当它很大时它不会显示整个输出。它被截断了。我似乎也无法对其进行复制/粘贴...对此也有任何建议吗?

谢谢! 地番

【问题讨论】:

  • 根据docs 中的示例,IpConfigurations 属性也应该包含PrivateIpAddress.. 这个属性是JSON,那么您是否尝试过ConvertFrom-Json?否则,你可以试试IpConfigurationsText(也是Json),比如Get-AzNetworkInterface | Select-Object IpConfigurationsText | Where-Object { ($_ | ConvertFrom-Json)["PrivateIpAddress"] -eq "10.1.2.3" }
  • @Theo 感谢您的宝贵意见!我想我对 shell 和 cmdlet 有一些基本的误解。在尝试您的建议时,我注意到如果我使用out-gripview 选项,我的IpConfigurations 字段只是{ipconfig1}IpConfigurationsText 将包含所有详细信息。如果我不使用out-gripview,在shell 控制台中我会看到IpConfigurations 中打印的所有内容,而我看不到IpConfigurationsText 字段。更奇怪的是,如果我使用Select-Object IpConfigurations 选项,我将再次只看到{ipconfig1} 的值...

标签: azure powershell powershell-7.2


【解决方案1】:

试试这个:

$IP = (Get-AzureRmNetworkInterface -Name $VMName -ResourceGroupName $RGName).IpConfigurations.PrivateIpAddress

如果您没有需要安装 i 的模块:

Login-AzureRmAccount
Install-Module AzureRm

more info

【讨论】:

  • 感谢@Tal Folkman。我应该提到我不知道我的 VMName。我来自网络团队,我只有一个私有 IP,我需要找出它是哪个 VM。我也无法运行此命令...我收到此错误Get-AzureRmNetworkInterface: The term 'Get-AzureRmNetworkInterface' is not recognized as a name of a cmdlet, function, script file, or executable program.。我应该安装最新的 power shell 和 azure cmdlet...
  • 看编辑@DifanZhao
【解决方案2】:
$IP = [IP you want to search)
$VMID = (Get-AzNetworkInterface | where {$_.IpConfigurations.PrivateIpAddress -eq $IP}).VirtualMachine.Id
If (!VMID) {
Write-Host "$IP Not Found"
}ELSE {
$VMName = $VMID.Substring($VMID.LastIndexOf('virtualMachines/')+16)
}

$VMName 将保存具有该私有 IP 的 VM 的纯文本名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多