VMware Cloud Community
Arjantimvdi2011
Enthusiast
Enthusiast
Jump to solution

Need a script to get hosts where observed IP-Range

For a customer I need to filter which Hosts (and VM's) have a connection with the DMZ VLAN's.

Problem is, the physical and virtual network VLAN's over multiple vCenter's aren't cosistent. So it can occur physical VLAN 24 is presented in the virtual network as VLAN 24 in one cluster and VLAN356 in an other.

My idea is to create a script that will search for observed IP-ranges (like 192.168.128.xxx) and show the VLAN Name, VLANID and VMHost information.

I'd like to get a csv file with this information, to be able to sort the outcome.

I really don't see if/how this can be done, and so I ask the help of the GURU's Smiley Wink

Thanks in advance!

Arjan

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you try it like this ?

$report = foreach($esx in Get-VMHost){
    $vms = Get-VM -Location $esx
    foreach($pg in (Get-VirtualPortGroup -VMHost $esx)){         $vms | where {$_.Guest.Nics | where {$_.NetworkName -eq $pg.Name}} |         Select @{N="Host";E={$esx.Name}},             @{N="PG";E={$pg.Name}},             @{N="VLANid";E={$pg.VlanId}},             @{N="IP";E={[string]::Join(',',($_.Guest.Nics | where {$_.NetworkName -eq $pg.Name} | %{$_.IPAddress | %{$_}}))}},             @{N="VM";E={$_.Name}}     } } $report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Could this help.

It will run through all portgroups, determine the VMs that have a connection over that portgroup and list the IP address(es) used by that VM over that portgroup.

foreach($esx in Get-VMHost){
    $vms = Get-VM -Location $esx
   
foreach($pg in (Get-VirtualPortGroup -VMHost $esx)){         $vms | where {$_.Guest.Nics | where {$_.NetworkName -eq $pg.Name}} |         Select @{N="Host";E={$esx.Name}},             @{N="PG";E={$pg.Name}},             @{N="VLANid";E={$pg.VlanId}},             @{N="IP";E={[string]::Join(',',($_.Guest.Nics | where {$_.NetworkName -eq $pg.Name} | %{$_.IPAddress | %{$_}}))}}     } }


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

AlbertWT
Virtuoso
Virtuoso
Jump to solution

Thanks for sharing the script,

how to list the name of the VM here ?

I tried to do the $vms. but the properties & Method doesn't give me anything for displaying VM name at all ?

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can add one more calculated property on the Select-Object cmdlet.

Something like this

foreach($esx in Get-VMHost){
    $vms = Get-VM -Location $esx
    foreach($pg in (Get-VirtualPortGroup -VMHost $esx)){         $vms | where {$_.Guest.Nics | where {$_.NetworkName -eq $pg.Name}} |         Select @{N="Host";E={$esx.Name}},             @{N="PG";E={$pg.Name}},             @{N="VLANid";E={$pg.VlanId}},             @{N="IP";E={[string]::Join(',',($_.Guest.Nics | where {$_.NetworkName -eq $pg.Name} | %{$_.IPAddress | %{$_}}))}},             @{N="VM";E={$_.Name}}     } }


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

Arjantimvdi2011
Enthusiast
Enthusiast
Jump to solution

I really can't get it exported to a csv file 😞 tried Export-Csv on multiple locations, but it doesn't seem to work.

Please help? 😉

Greets, and a million thanks for every thing done already.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you try it like this ?

$report = foreach($esx in Get-VMHost){
    $vms = Get-VM -Location $esx
    foreach($pg in (Get-VirtualPortGroup -VMHost $esx)){         $vms | where {$_.Guest.Nics | where {$_.NetworkName -eq $pg.Name}} |         Select @{N="Host";E={$esx.Name}},             @{N="PG";E={$pg.Name}},             @{N="VLANid";E={$pg.VlanId}},             @{N="IP";E={[string]::Join(',',($_.Guest.Nics | where {$_.NetworkName -eq $pg.Name} | %{$_.IPAddress | %{$_}}))}},             @{N="VM";E={$_.Name}}     } } $report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


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

Reply
0 Kudos
Arjantimvdi2011
Enthusiast
Enthusiast
Jump to solution

Thanks Luc,

The Dutch government is very grateful to you :smileysilly: This really makes their live much easier.

Have a great weekend!

Reply
0 Kudos