VMware Cloud Community
jade_
Contributor
Contributor

List all route information in a report?

I'm hoping someone here can help me out with this.  I'm trying to build a report that will list all the route information for a series of hosts in a report.  

What I am trying to do is get a readable report thats simliar to the data thats returned from either "esxcfg-route -l"; or "get-vmhostroute".   The part that I am struggling with is the fact that each host returns multiple lines, and for the life of me I can't figure out a simple way to put all of them into an array.

What I would like to see on the output is something like:

clustername, vmhost, destination, gateway

ie

cluster1,vmhost1.domain.com,0.0.0.0/0,10.10.10.1

,,,10.101.106.0/25,0.0.0.0
(etc, for every 'hit')
Here is the script that I have thus far.  I know that part of the issue is that I need to expand the array; but thats the part I am struggling with 🙂    Odds are I am just over complicating this and there is a simple solution that just escapes me.

# Define all VC instances as variables to call later

$vc1 = "VC-SERVER1"

$vc2 = "VC-SERVER2"

# Set PowerCLI to multiple

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false

connect-VIServer -Server $vc1, $vc2

$report = @()

$clusters = get-cluster |sort name

foreach ($cluster in $clusters) {

foreach ($ESXHost in Get-Cluster $cluster |Get-VMHost |sort name ) {

$Row = "" | Select-Object Cluster,ESXHost,Destination,Gateway

$Row.Cluster = $cluster.name

$Row.ESXHost = $ESXHost.name

$Row.Destination = @($ESXHost |get-vmhostroute)

$Row.gateway = @($ESXHost |get-vmhostroute)

$Report += $Row

}

}

Any help is greatly appreciated!
Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

Similarly to what you did with the clusters and the hosts, you can loop through the routes with a foreach loop.

Something like this

$report = @()
foreach ($cluster in (get-cluster |sort name)) {
    foreach ($ESXHost in (Get-VMHost -Location $cluster |sort name )) {
        foreach($route in (Get-VMHostRoute -VMHost $ESXHost)){
            $Row = "" | Select-Object Cluster,ESXHost,Destination,Gateway
            $Row.Cluster = $cluster.name
            $Row.ESXHost = $ESXHost.name
            $Row.Destination = $route.Destination
            $Row.gateway = $route.Gateway
            $Report += $Row
        }     } }
$report


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

Reply
0 Kudos
jade_
Contributor
Contributor

First; that was lightning fast.   Second; Thats pretty much spot on Luc; I don't know why I didn't consider that.  

One question though...  Is it possible to get all the route info under one entry for the host(s)?  This method creates a line for each of the entires; and while its the data I want, its slightly noisy...

example on current output (single host):

Cluster     : CLUSTER-A

ESXHost     : esxhost1.domain.com
Destination : 0.0.0.0
Gateway     : 10.10.10.1
Cluster     : CLUSTER-A
ESXHost     : esxhost1.domain.com
Destination : 10.10.126.0
Gateway     : 0.0.0.0
Cluster     : CLUSTER-A
ESXHost     : esxhost1.domain.com
Destination : 10.10.127.0
Gateway     : 0.0.0.0
Cluster     : CLUSTER-A
ESXHost     : esxhost1.domain.com
Destination : 10.10.179.0
Gateway     : 0.0.0.0
Cluster     : CLUSTER-A
ESXHost     : esxhost1.domain.com
Destination : 72.163.48.64
Gateway     : 10.101.127.1
Cluster     : CLUSTER-A
ESXHost     : esxhost1.domain.com
Destination : 173.37.174.128
Gateway     : 10.101.174.1

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership

Sure, everything is poshible 😉

Like this all destinations and gateways are added as properties to the same row.

$report = @()
foreach ($cluster in (get-cluster |sort name)) {
    foreach ($ESXHost in (Get-VMHost -Location $cluster |sort name )) {
        $Row = "" | Select-Object Cluster,ESXHost,Destination,Gateway
        $Row.Cluster = $cluster.name
        $Row.ESXHost = $ESXHost.name
        $i = 1
       
foreach($route in (Get-VMHostRoute -VMHost $ESXHost)){             Add-Member -InputObject $Row -Name ("Destination" + $i) -Value $route.Destination -MemberType NoteProperty             Add-Member -InputObject $Row -Name ("Gateway" + $i) -Value $route.Gateway -MemberType NoteProperty             $i++
        }        
$Report += $Row
    } } $report

Note that there could be a problem when you export this to a CSV.

The Export-Csv cmdlet uses the first row to determine how many columns there have to be in the CSV file.

So you will have to order the rows in descending orider on the number of columns.

$report | Sort-Object -Property {($_ | Get-Member).Count} -Descending | `
Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


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

Reply
0 Kudos
jade_
Contributor
Contributor

Luc:

Your skills never cease to amaze me; that was exactly what I was looking for.   The example makes perfect sense, and I was not aware of the nuance of export-csv, but will keep that in mind for the future.

Thanks a ton!!

Reply
0 Kudos