Mohamed Wali
Mohamed Wali

Reputation: 185

Retrieve the NICs and the associated VMs in Azure

I need to retrieve the NICs and the associated VMs in Azure I've run the following Cmdlet:

Get-AzureRmNetworkInterface | Select Name, VirtualMachine

But, it only generate the names of the NICs but when it come to the Virtual Machine it displays Microsoft.Azure.Commands.Network.Models.PSResourceId, as shown in the following figure.Virtual Machine Output

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

Upvotes: 0

Views: 668

Answers (1)

Tom Sun
Tom Sun

Reputation: 24549

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

We could use the select-object to do that. It works correctly for me.

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

enter image description here

Update : according to the comment

If we want to get the virtual network name we could use the command

Get-AzureRmVirtualNetwork

How to export the result in the same csv file. We could use 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

Upvotes: 2

Related Questions