在 Azure 中检索 NIC 和关联的 VM

Retrieve the NICs and the associated VMs in Azure

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

Get-AzureRmNetworkInterface | Select Name, VirtualMachine

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

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

Please advise how to retrieve the actual name of the VM.

我们可以使用 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