VMware Cloud Community
kumawat_29
Contributor
Contributor

Script to extract multiple IP Address corresponding to VNIC

Hi,

We are trying to bring some data related to VM. Output needs to be one line per vm in a csv format

Lets say VM1 has 2 VNIC and 3 IP Address, output should come in this format

VM1; UUID1; VNIC1; IP1,IP2; MAC1;PG1

VM1;UUID1;VNIC2;IP3;MAC2;PG2

VM2;UUID2; VNIC2;IP4; MAC3;PG3

Can someone help to fix below script by running on PowerCli 6.5.1 ?

$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 

$IP = $(($ntwkcard.IpAddress|?{$_ -like "*.*"}) -join ',')

Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $IP

$i++

}

$reportedvms.add($reportedvm)|Out-Null

}

$reportedvms| export-csv -delimiter "`t" -path E:\SCRIPTS\CRAMER\OUTPUT\HCS_multipleIP-$(get-date -f dd-MM-yyyy-hh-mm).csv –notypeinformation

9 Replies
LucD
Leadership
Leadership

Try something 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){

  foreach($ntwkcard in $vm.Guest.Net){

    $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)

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name Network -Value $ntwkcard.Network

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name MacAddress -Value $ntwkcard.Macaddress

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name IpAddress -Value ($ntwkcard.IPAddress -join ',')

    $reportedvms.add($reportedvm)|Out-Null

  }

}

$reportedvms| export-csv -delimiter "`t" -path E:\SCRIPTS\CRAMER\OUTPUT\HCS_multipleIP-$(get-date -f dd-MM-yyyy-hh-mm).csv –notypeinformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

kumawat_29
Contributor
Contributor

Thanks LucD, it worked fine but gave both IPV4 and IPV6, we need  IPV4. We also require VNIC Type corresponding to VNIC. So I tried to add VNIC type from internet source but getting error

“You cannot call a method on a null-valued expression” errors on screen

Can you please help in fixing VNIC_TYPE and IPV4 details ?

$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){

  foreach($ntwkcard in $vm.Guest.Net){

    $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)

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name Network -Value $ntwkcard.Network

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name MacAddress -Value $ntwkcard.Macaddress

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name VNIC_TYPE -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.DeviceConfigId)}).gettype().name)

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name IpAddress -Value ($ntwkcard.IPAddress -join ',')

    $reportedvms.add($reportedvm)|Out-Null

  }

}

$reportedvms| export-csv -delimiter "`t" -path E:\SCRIPTS\CRAMER\OUTPUT\HCS_multipleIP-$(get-date -f dd-MM-yyyy-hh-mm).csv –notypeinformation

0 Kudos
LucD
Leadership
Leadership

Try 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){

  foreach($ntwkcard in $vm.Guest.Net){

    $vNnIc = $vm.Config.Hardware.Device | where{$_.Key -eq $ntwkcard.DeviceConfigId}

    if($vNic){

      $VnicType = $vNic.GetType().Name.Replace('Virtual','')

    }

    else{

      $VnicType = ''

    }

    $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)

    Add-Member -InputObject $reportedvm -MemberType noteProperty -name VNicType -value $VnicType

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name Network -Value $ntwkcard.Network

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name MacAddress -Value $ntwkcard.Macaddress

    Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name IpAddress -Value (($ntwkcard.IPAddress | where{$_ -notmatch ':'}) -join ',')

    $reportedvms.add($reportedvm)|Out-Null

  }

}

$reportedvms| export-csv -delimiter "`t" -path E:\SCRIPTS\CRAMER\OUTPUT\HCS_multipleIP-$(get-date -f dd-MM-yyyy-hh-mm).csv notypeinformation 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

kumawat_29
Contributor
Contributor

Thanks LuCD, it worked fine. Is it possible to have VLANid in same output. Internally we had discussion if DV switch is 3rd party like N1K then it would not be possible to get VLANid from N1K, it reside somewhere inside N1K not vCenter but it can be seen in vCenter.

0 Kudos
LucD
Leadership
Leadership

You are correct, the internal structure of the N1K object is not public I'm afraid.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
kumawat_29
Contributor
Contributor

Hi LucD,

Today we got dump from Production, some of VMs reported correct data but some VMs came without IP Address.

Did you experienced such case earlier ? Even though vcenter is showing IPAddress it is not reported .

"DUMMY-VM1" "4204204a-2749-089e-7dc4-ca0a8b11x45x" "E1000" "ABC-02-AC-APPS" "00:50:48:8f:61:2a" ""

"DUMMY-VM1" "4204204a-2749-089e-7dc4-ca0a8b11x45x" "E1000" "777_Trap_Exploder" "00:50:48:8f:22:9a" ""

"DUMMY-VM1" "4204204a-2749-089e-7dc4-ca0a8b11x45x" "E1000" "777_Trap_Exploder" "00:50:48:8f:6b:aa" ""

"DUMMY-VM1" "4204204a-2749-089e-7dc4-ca0a8b11x45x" "E1000" "777_Trap_Exploder" "00:50:48:8f:1d:e1" ""

"DUMMY-VM1" "4204204a-2749-089e-7dc4-ca0a8b11x45x" "E1000" "ABC-02-AC-APPS" "00:50:48:8f:72:38" ""

"DUMMY-VM1" "4204204a-2749-089e-7dc4-ca0a8b11x45x" "E1000" "ABC-02-AC-APPS" "00:50:48:8f:39:51" ""

"DUMMY-VM1" "4204204a-2749-089e-7dc4-ca0a8b11x45x" "E1000" "XYZ-MANAGEMENT" "00:50:48:8f:45:61" ""

"DUMMY-VM1" "4204204a-2749-089e-7dc4-ca0a8b11x45x" "E1000" "XYZ-MANAGEMENT" "00:50:48:8f:1c:98" ""

"XYDDOFHDPGB" "4204204a-77a4-998c-a941-838f715e9181" "E1000" "RAM-020-AD-PRVT" "00:50:48:8f:54:42" "172.30.20.115,172.30.20.116"

"XYDDOFHRGRB" "4204204a-95b3-5cb0-7e91-ef27e6a9412c" "E1000" "RAM-020-AD-APPS" "00:50:48:8f:35:7c" "172.30.20.135,172.30.20.136"

Regards,

Pradeep

0 Kudos
LucD
Leadership
Leadership

Do the VMs with the missing info have the VMware Tools installed?

And are they powered on?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
kumawat_29
Contributor
Contributor

They are powered on but not sure of VMware tool you mentioned. How to get information of VMware tool from network using powercli ?

0 Kudos
LucD
Leadership
Leadership

Try like this

Get-VM |

Select Name,@{N="Tools Status";E={$vm.ExtensionData.Guest.ToolsStatus}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos