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
# 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
}
}
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
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
Thanks!
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
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!!