Report for all vms regarding uuid,network card, macaddress, ipaddress, network card type e1000/vmxnet in a csv ;)

by Grzegorz Kulikowski

Right,
i wanted to see all vms, with their current ips, macaddresses, names, networkcard types.
This report should also have all network cards in separate columns in csv. So i needed to dynamically adjust number of those ‘columns’.
Below script will output only ipv4 addresses without ipv6.
[sourcecode language=”powershell”]
$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 | ?{$_.DeviceConfigId -ne -1}
$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
[/sourcecode]

Ok, and what if somebody doesn’t want those multiple columns, but wants to have only 1 column that will describe all network cards for particular vm ?
[sourcecode language=”powershell”]
$vms=get-view -viewtype virtualmachine
$(
foreach($vm in $vms){
$networkcards=$vm.guest.net | ?{$_.DeviceConfigId -ne -1}
""|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 -join ‘;’),$_.Macaddress))}) )}}
}
)|export-csv c:\myreport.csv
[/sourcecode]
I know 😉 a lot of joining.
@Update: Luc has noticed my error there that i was not sorting $vms array. We have to sort it by the vm with most network cards so that export-csv will work properly.
@Update: Thanks Steve for the feedback. I have updated the code in both examples.
27/02/2014@Update : Fixed issue with reporting multiple ip addresses in second example.

Advertisements

You may also like

24 comments

Clifton Duanway November 14, 2013 - 10:13 pm

Can it also show the Linkspeed, MTU, TCP Offloading and the TCP Chimney partial offload?

Reply
guest December 18, 2013 - 6:52 pm

Nicely done:) Thank you.

Reply
Steve Pickering February 26, 2014 - 6:07 pm

I gave this a go, however the results were not what I expected. My CSV File had info in it, but not about network addresses

Reply
psvmware February 26, 2014 - 8:24 pm

Hi Steve, thanks for feedback. Can you please tell from which example have you tried the code ? was is the first example or the second. The first example should work, i have tested it today. The second one, i have ran it towards few vms and i spot an issue. In case you were running second example can you please change the line:
$networkcards=$vm.guest.net
to:
$networkcards=$vm.guest.net| ?{$_.DeviceConfigId -ne -1}
Maybe we hit the same issue. Please let me know if that helped

Reply
bijolianabhi August 21, 2014 - 5:59 am

Hi,
I run first example and I am getting following error :-

You cannot call a method on a null-valued expression.
At line:11 char:180
+ Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name “networkcard${i}.Device” -Value $(($vm.config.hardware.device|?{$
_.key -eq $($ntwkcard.DeviceConfigId)}).gettype <<<< ().name)
+ CategoryInfo : InvalidOperation: (gettype:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

I am new to Powercli.
Thanks

Reply
steven September 4, 2014 - 11:36 pm

is it possible to make it so that it only pulls back vm with 2 or more network adapters?

Reply
Anonymous January 8, 2015 - 5:20 am

Hi Guys,
I was looking for this scripts but I never run it before, can someone show a screenshot how to run this script, I try to run it on Vcenter but it show an error message “get-view” is not recognized I try to run both scripts but was not able to find please help.
Thank you

Reply
psvmware January 8, 2015 - 10:20 am

Are you running it within powercli ? or directly in powershell ?

Reply
Anonymous January 8, 2015 - 6:23 pm

I ran in the Vcenter server and used powershell, I’m new to this please provide little more info how to run.
Thank you

Reply
Anonymous January 8, 2015 - 6:02 pm

I ran in the Vcenter server and used powershell, I’m new to this please provide little more info how to run.
Thank you

Reply
psvmware January 8, 2015 - 8:03 pm

You have to start from the basics i would say. You have to run those scripts within PowerCLI , that is connected to your VC.
Best place to start is:
vmware.com/go/powercli

Reply
Magnus January 26, 2015 - 4:21 pm

Nice work Grzegorz! Much appreciated. Almost spot on for what i need. I am trying to export a list like yours, but with powered on VMs (only), DNS name of guest OS, every MAC and IP adresses currently assigned (as in above script). However my CLI skill are not at that level yet. Do you think you can point me in the right direction?

Reply
psvmware January 27, 2015 - 2:00 pm

It can be done in various ways actually. Hostname is kept inside .Extensiondata.Guest.Hostname , some time you have to combine it from :
$a.extensiondata.guest.ipStack.DNsConfig where
$a.extensiondata.guest.ipStack.DNsConfig.Hostname keeps the name
and
$a.extensiondata.guest.ipStack.DNsConfig.DomainName keeps the domain name

So basically you can try this like that : This is just by adding 3 lines to the script mentioned in the post:

After line 7 add following lines:
Add-Member -InputObject $reportedvm -MemberType noteProperty -name HostName -value $($vm.Guest.IpStack.DnsConfig.Hostname)
Add-Member -InputObject $reportedvm -MemberType noteProperty -name Domain -value $($vm.Guest.IpStack.DnsConfig.DomainName)
Add-Member -InputObject $reportedvm -MemberType noteProperty -name FullName -value $($vm.Guest.IpStack.DnsConfig.Hostname+’.’+$vm.Guest.IpStack.DnsConfig.DomainName)

Then report should look like :
Guest : MyVM
HostName : MyVM
Domain : myhome.net
FullName : MyVM.myhome.net
UUID : xyz
networkcard0.Network : vlanName
networkcard0.MacAddress : 00:50:56:xx:xx:xx
networkcard0.IpAddress : 127.0.0.1
networkcard0.Device : VirtualVmxnet3

I have tested it now, works ok.

If you want to get this report only for powered on vms modify line number 2 as :

$vms=get-view -viewtype virtualmachine -Filter @{‘Runtime.PowerState’=’poweredOn’}|Sort-Object -Property { $_.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualEthernetCard]} | Measure-Object | select -ExpandProperty Count} -Descending

This will grab only those which are ON.

Reply
Magnus January 27, 2015 - 3:35 pm

Thank you for your time and assist. Much appreciated. 🙂

I have modified the ps1 file and removed the lines for Guest, UUID etc so that it contains -name Guest, MacAdress and IpAdress for Powered.On machines.

However (and i must confess that i am out on deep waters here), after adjusting with your latest code snippets, i get an error which looks like this (hoping cut´n paste works):

Missing ‘=’ operator after key in hash literal.
At C:\tools\export.ps1:2 char:90
+ $vms=get-view -viewtype virtualmachine -Filter @{â?~Runtime.PowerStateâ?T=â?TpoweredOnâ?T <<<< } | Sort-Object -Property { $_.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualEthernetCard]} | Measure-Object | select -ExpandProperty Count} -Descending
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : MissingEqualsInHashLiteral

Currently the modified ps1 looks like this:

$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
$networkcards=$vm.guest.net | ?{$_.DeviceConfigId -ne -1}
$i=0
foreach($ntwkcard in $networkcards){
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 "*.*"})
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}

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

I´m trying to find the error at character 90 and any missing ´=´, perhaps i have polluted the code when using copy & paste so that the formating is wrong?

Best regards
Magnus 🙂

Reply
psvmware January 27, 2015 - 3:57 pm

Please use pastebin for example for your entire code, from top to bottom.

Reply
Magnus January 27, 2015 - 5:08 pm

Ahh that was a new site for me thanks!

ok here goes.

Reply
Magnus January 27, 2015 - 5:11 pm

uhm, ok it did not accept the script tags. Trying again I have the code at http://pastebin.com/bD74p1CB

//Magnus

Reply
psvmware January 27, 2015 - 6:29 pm

Hi there, i think this is some bonus formatting from wordpress. Have a look here:
http://pastebin.com/fFM3SEgb

Reply
Magnus January 28, 2015 - 11:21 am

Thank you, now the script is almost working, some (but not all) rows contains “System.Object[]”,,,,,, and some machines (locked Linux appliances with no vmware tools) do not return anything except the VM container name.

I need this list to populate an Asset Database so that is why i am only interested in getting a nice and clean csv list over Powered On VMs only that contains

Servername, IP, MAC,

Nothing more, nothing less. 🙂 So i have removed some values i dont need ie (UUID, name of the vNIC etc).

The modified script i am using looks like this: http://pastebin.com/bD74p1CB

But i dont wanna take up more of your time and i am very grateful for your help this far. Perhaps there are other ways of getting the above information from vcenter. I have tried rvtools, but the results (that i need) are exported to different tabs which need manual sorting/merging.

Cheers 🙂

Reply
psvmware January 29, 2015 - 12:09 pm

Hi there, no worries. There is no way as far as i know you can pull the ip of a VM instance which is not running vmware tools. No vmware tools = no ip info, If you really need badly to pool that 😉 i mean, if you really want, i guess you can try to play with arp tables to check mac to ip, it’s complex, but i suppose 100% doable.
For the [object] , i will send you an update shortly, it’s because that is an array, so in CV you see that object thing. But in pwoershell if you will type : $reportedvms after you created report, you will see the whole thing. Will reply with the proper version probably tomorrow.

Reply
psvmware January 29, 2015 - 4:40 pm

Hello, here it is. It should report all ips now,.(not the ones without vm tools)
http://pastebin.com/FH7sfVtB

Reply
Magnus January 29, 2015 - 6:01 pm

Fantastic!!! Thank you so much. Now it looks very nice :). I can handle the linux appliances manually. One small question. Is it possible to get rid of the symbols: ,,””,,,,,,,, that are written last on each row in the output file? And perhaps also remove the divider symbol -> ” <- between each result and only keep the comma , symbol as a divider?

Thank you 🙂

Reply
psvmware January 30, 2015 - 10:37 am

Hey Magnus, can you just join #powercli on irc.freenode.net ? I guess it will be easier 😉 If you don’;t know how : https://psvmware.wordpress.com/powercli-live-chat/

Reply
Anonymous August 12, 2015 - 2:04 pm

Thank you

Reply

Leave a Reply

Chinese (Simplified)DutchEnglishFrenchGermanHindiPolishRussianSpanish