【问题标题】:Retrieve the NICs and the associated VMs in Azure在 Azure 中检索 NIC 和关联的 VM
【发布时间】:2018-05-27 20:55:37
【问题描述】:

我需要在 Azure 中检索 NIC 和关联的 VM 我已经运行了以下 Cmdlet:

Get-AzureRmNetworkInterface | Select Name, VirtualMachine

但是,它只生成网卡的名称,但是到了虚拟机时却显示了Microsoft.Azure.Commands.Network.Models.PSResourceId,如下图所示。

请告知如何检索虚拟机的实际名称。

【问题讨论】:

    标签: azure azure-virtual-machine azure-powershell azure-virtual-network


    【解决方案1】:

    请告知如何检索虚拟机的实际名称。

    我们可以使用select-object 来做到这一点。它对我来说可以正常工作。

    Get-AzureRmNetworkInterface |  Select-Object -Property Name, @{Name="VMName";Expression = {$_.VirtualMachine.Id.tostring().substring($_.VirtualMachine.Id.tostring().lastindexof('/')+1)}}
    

    更新:根据评论

    如果我们想获取虚拟网络名称,我们可以使用命令

    Get-AzureRmVirtualNetwork
    

    如何将结果导出到同一个 csv 文件中。我们可以使用out-file

    Get-AzureRmVirtualNetwork | select Name |out-file $filePath -Append
    Get-AzureRmNetworkInterface |  Select-Object -Property Name, @{Name="VMName";Expression = {$_.VirtualMachine.Id.tostring().substring($_.VirtualMachine.Id.tostring().lastindexof('/')+1)}} |out-file $filePath -Append
    

    【讨论】:

    • 谢谢汤姆。子网名称呢,有没有办法在同一个 cmdlet 中检索虚拟网络名称?
    • 如果要获取虚拟网络名称,可以使用 cmdlet Get-AzureRmVirtualNetwork
    • 我在相同的结果中需要它们。我可以在同一个 csv 中导出所有这些信息吗?
    • 是的。我们可以将它们全部导出到同一个 csv 中。我会更新答案。
    • 但我相信这会匹配正确的虚拟网络和正确的 NIC,对吧?
    猜你喜欢
    • 2021-09-20
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多