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


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.

$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

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 ?

$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

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

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

  1. 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

    • 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

  2. 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

  3. 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

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

    • 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

  5. 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?

    • 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.

      • 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 🙂

  6. 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 🙂

    • 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.

  7. 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 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s