VMware Cloud Community
VM_
Contributor
Contributor
Jump to solution

Script to list VM Network

Hi,

I'm having trouble putting together a script that essentially seems simple enough but has me stupmed as I need to get the data from multiple locations.

I need to gather all VM's and their network config. I have seen a lot of similar scripts but none seem to aligh the IP and Mac per network card and include the vlan and device type for the network card. Also none seem to deal with vm's that have multiple nics.

I need this for an internal CMDB. They have requested:

VM Name

UUID

Network Name

Adapter Type

Mac Address

IP address

Output needs to be one line per vm in a csv format

Guest UUID Vlan0 IPAddress0 Mac0 AdapterType0 Vlan1 IPAddress1 Mac1 AdapterType1 Vlan2 IPAddress2 Mac2 AdapterType2

Any help appreciated.

1 Solution

Accepted Solutions
Grzesiekk
Expert
Expert
Jump to solution

Yeah.. it's my bad here i had bad example on my test env, will find vm with ipv6 address as well and will write updated version shortly.

ok here it is :

$reportedvms=New-Object System.Collections.ArrayList
$vms=get-view -viewtype virtualmachine

foreach($vm in $vms){
  $reportedvm = New-Object PSObject
  Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
  Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress 
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_ -like "*.*"})
Add-Member  -InputObject $reportedvm -MemberType NoteProperty -Name  "networkcard${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key  -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}

$reportedvms|Export-Csv c:\myreport.csv

--- @blog https://grzegorzkulikowski.info

View solution in original post

0 Kudos
15 Replies
sparrowangelste
Virtuoso
Virtuoso
Jump to solution

to get you started here is the UUID

Get-VM <vm_name> | %{(Get-View $_.Id).config.uuid}
--------------------- Sparrowangelstechnology : Vmware lover http://sparrowangelstechnology.blogspot.com
0 Kudos
Grzesiekk
Expert
Expert
Jump to solution

Hi there,

i think i did not understand all requirements, or they were not described correctly but what i understood is:

data needed: uuid, vm name,network name, mac, ip

what do you mean by adapter type , do want to have info about e1000,vmxnet etc ??

--

$vms=get-view -viewtype virtualmachine -filter @{"name"="myvm1"}
foreach($vm in $vms){

$networkcards=$vm.guest.net
""|select @{n="VM name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net info";e={[string]::join(',', $($networkcards|%{[string]::join(',',$($_.network,$_.ipaddress,$_.Macaddress))}) )}}

}

--

try this first out for 1 vm where : "myvm1" should be change to some existing vm in your infra. If all is good , run this

$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){

$networkcards=$vm.guest.net
""|select @{n="VM  name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net  info";e={[string]::join(',',  $($networkcards|%{[string]::join(',',$($_.network,$_.ipaddress,$_.Macaddress))})  )}}

}

Greg

If you want that type card e1000,vmxnet etc.. you can try it like this

$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){

$networkcards=$vm.guest.net
""|select @{n="VM name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net info";e={[string]::join(',', $($networkcards|%{$devid=$_.DeviceConfigId;[string]::join(',',$(($vm.config.hardware.device|?{$_.key -eq $devid}).gettype().name,$_.network,$_.ipaddress,$_.Macaddress))}) )}}

}

i forgot about the csv at the end so:

$(

foreach($vm in $vms){

$networkcards=$vm.guest.net
""|select  @{n="VM name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net  info";e={[string]::join(',',  $($networkcards|%{$devid=$_.DeviceConfigId;[string]::join(',',$(($vm.config.hardware.device|?{$_.key  -eq $devid}).gettype().name,$_.network,$_.ipaddress,$_.Macaddress))})  )}}

}

)|export-csv c:\rep321.csv

--- @blog https://grzegorzkulikowski.info
VM_
Contributor
Contributor
Jump to solution

Thanks for the replys so far:

This script I found online (All credit to : http://www.blkmtn.org/Get_VMWare_guest_Network_and_IP_Address) is a good start.

To get the IP address's associated with each nic adapter I have added in :

  $GuestView = Get-View -ViewType VirtualMachine -Property Name,Guest.Net -Filter @{"Name"="$vm$"}
  $Network = $GuestView.Guest.Net
And
  $obj | Add-Member -MemberType noteProperty -name IPAddress$i -value $Network[$i].IPAddress

Without this I was only getting the first IP reported against ever Nic.

I also need to get the Nic type like VMXnet3 or e1000 but don't know how to.

It's a work in progress and when finished it will should be useful to others.

# Get all powered on VM guests
$cluster = Get-Cluster $name
$VMs = $cluster | Get-VM
# Loop through each system
foreach ($vm in $VMs ) {
  # Get the virtual machine details
  $GuestView = Get-View -ViewType VirtualMachine -Property Name,Guest.Net -Filter @{"Name"="$vm$"}
  $Network = $GuestView.Guest.Net
 
  $obj = New-Object -typename System.Object
  $obj | Add-Member -MemberType noteProperty -name Guest -value $vm.Name
  $obj | Add-Member -MemberType noteProperty -name UUID -value $($vm | Get-View).Config.Uuid
  # Loop through each NIC assigned to a vm and report the details
  $i=0    # Set to zero
  foreach ($nic in $vm.Guest.Nics) {
  
    # As additional nics are descovered the label should be updated to be Network 1, 2, 3 etc..
    $obj | Add-Member -MemberType noteProperty -name Network$i -value $nic.NetworkName
    $obj | Add-Member -MemberType noteProperty -name IPAddress$i -value $Network[$i].IPAddress
    $obj | Add-Member -MemberType noteProperty -name Mac$i -Value $nic.MacAddress
    $obj | Add-Member -MemberType noteProperty -name Vlan$i -Value $nic.NetworkName
  
    # I also need to get the Nic type like VMXnet3 or e1000 but don't know how to.
    $i++  # Increment Nic object count
  }
  $report += $obj
  $obj = $null
}
$report | Export-Csv F:\Scripts\Reports\vm_nic_report.csv -NoTypeInformation -UseCulture
# Clean up
$report = $null
0 Kudos
VM_
Contributor
Contributor
Jump to solution

Adapter Type = device type of the network card - e1000, vmxnet

0 Kudos
VM_
Contributor
Contributor
Jump to solution

Hi Grzesiekk

Thanks for the scripts they are very good and the last one where the output to csv is 90% of what I need.

The output needs to be broken up into individual cells in excel(csv) in the same way you have VM name and uuid.

Output needs to be one line per vm and needs to deal with vm's that have multiple Network cards in a csv format

VM Name, UUID, NetworkAdapterType0, Vlan0, IPAddress0, Mac0, NetworkAdapterType1, Vlan1, IPAddress1, Mac1, NetworkAdapterType2, Vlan2, IPAddress2, Mac2

Thanks for you help, really appreciated.

0 Kudos
Grzesiekk
Expert
Expert
Jump to solution

Hi Smiley Wink

i guess that this was the most confusing in what you were trying to achieve. Some people wants exactly to have 1 column with all this info , so i thought you wanted the same. In this case it makes things even easier Smiley Wink

$reportedvms=New-Object System.Collections.ArrayList
$vms=get-view -viewtype virtualmachine

foreach($vm in $vms){
  $reportedvm = New-Object PSObject
  Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
  Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress 
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $ntwkcard.IpAddress
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}

$reportedvms|Export-Csv c:\myreport.csv

Let me know if this is 100% accurate Smiley Wink

--- @blog https://grzegorzkulikowski.info
0 Kudos
VM_
Contributor
Contributor
Jump to solution

Hey, Thanks again for your help.

It's 99% there.

The IP address is outputting as System.String[] for all vm's.

So I changed the following to $($ntwkcard.IpAddress).ToString(). This works for some vm's but not all.

The ones that do not output the IP are listed as System.Object[]

Also some of my Network names are being output as : 62 f7 03 50 29 ff 5d e0-4c 38 90 b4 fb 64 bd 09

Thanks,

Niall.

0 Kudos
Grzesiekk
Expert
Expert
Jump to solution

Yeah.. it's my bad here i had bad example on my test env, will find vm with ipv6 address as well and will write updated version shortly.

ok here it is :

$reportedvms=New-Object System.Collections.ArrayList
$vms=get-view -viewtype virtualmachine

foreach($vm in $vms){
  $reportedvm = New-Object PSObject
  Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
  Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress 
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_ -like "*.*"})
Add-Member  -InputObject $reportedvm -MemberType NoteProperty -Name  "networkcard${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key  -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}

$reportedvms|Export-Csv c:\myreport.csv

--- @blog https://grzegorzkulikowski.info
0 Kudos
VM_
Contributor
Contributor
Jump to solution

Hi Grzesiekk,

Just saw the following :

http://psvmware.wordpress.com/2012/10/24/report-for-all-vms-regarding-network-card-macaddress-ipaddr...

Really should share with the forum if using the forum for motivation to write the script.

However I have tested it and it is working but one change I made was to put the following in quotes:

$($ntwkcard.IpAddress|?{$_ -like "*.*"})

without the quotes some of the output is like this if there are multiple address per adapter:

System.Object[]

Thanks for all your help. Here is final script:

$reportedvms=New-Object System.Collections.ArrayList
$vms = get-view -viewtype virtualmachine  |Sort-Object -Property {  $_.Config.Hardware.Device |  where {$_ -is [VMware.Vim.VirtualEthernetCard]} |  Measure-Object | select -ExpandProperty Count} -Descending
foreach($vm in $vms){
  $reportedvm = New-Object PSObject
  Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
  Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress 
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_ -like "*.*"})
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}
$reportedvms | Export-Csv C:\vm_nic_report.csv
0 Kudos
Grzesiekk
Expert
Expert
Jump to solution

I am glad that this worked for you.

One thing is that i did put the "  " Smiley Wink


Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name  "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_ -like  "*.*"})

it has the quoatation marks "*.*" i am bit confused now if that was something wrong while you have been copying text to your clipboard or did i make some mistake Smiley Wink

Before i pasted to you i was testing source code Smiley Wink It had to work, i guess Smiley Wink

--- @blog https://grzegorzkulikowski.info
0 Kudos
TdisalvoOrinoco
Enthusiast
Enthusiast
Jump to solution

VM

Thanks for the update on this script.

When I run it I do get an error.

You cannot call a method on a null-valued expression.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\custom\Virtual
MachineNetworking.ps1:16 char:180
+ Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkca
rd${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.De
viceConfigId)}).gettype <<<< ().name)
    + CategoryInfo          : InvalidOperation: (gettype:String) [], RuntimeEx
   ception
    + FullyQualifiedErrorId : InvokeMethodOnNull

This reoccurs several time.

The output is created, however, I would love to know what is going on with this error.

0 Kudos
TdisalvoOrinoco
Enthusiast
Enthusiast
Jump to solution

After reviewing my output, it looks like if networkcard0 was not on the Virtual machine it causes a problem with the script.

Not sure if there is a way to address this.

0 Kudos
kumawat_29
Contributor
Contributor
Jump to solution

Hi,

Can you please add VLAN id and Port Group in same script and let me know if this is feasible to have it ?

Regards,

Pradeep

AlbertWT
Virtuoso
Virtuoso
Jump to solution

Yes please, that'd be greatly appreciated.

/* Please feel free to provide any comments or input you may have. */
0 Kudos
kumawat_29
Contributor
Contributor
Jump to solution

Hi,

Can you please provide script which can generate file in below format ?

VM1, UUID, VNIC TYPE1, IP Address, MAC Address, Port Group, VLANid

VM1, UUID, VNIC TYPE2, IP Address, MAC Address, Port Group, VLANid

VM1, UUID, VNIC TYPE3, IP Address, MAC Address, Port Group, VLANid

Regards,
Pradeep