PowerCLI: Host Network Config

Whilst on a recent engagement with a customer we were looking at best practices and trying to quickly see how each PortGroup was configured, did all hosts have the same nics assigned to the same vSwitches, were all Portgroups setup the same to use the correct active and standby adapters ?

Have you ever tried doing this from the GUI ? Believe me it will take about 10 minutes to manually copy the relevant information from each host and if you have multiple PortGroups and multiple vSwitches then you can see how long this will take to check against best practice !

I think you know what is coming….

This came up again when someone asked me how to achieve exactly the same thing so I bumped it up my list of scripts I need to write.

Check out the below PowerCLI script, it lists all the needed information to perform this best practice analysis as you can see from the example output:

image

And the code:

# Change the next line to specify the path for the ouput CSV file
$filename = "C:\Temp\Networkinfo.csv"

# Change the next line to connect to your vCenter Server
Connect-VIServer MYVISERVER

$NetworkInfo = @()
Foreach ($VMHost in (Get-View -ViewType HostSystem | Where {$_.Runtime.ConnectionState -ne "disconnected"})){
    Write $VMHost.Name
    $NetworkSystem = Get-View $VMHost.ConfigManager.NetworkSystem
    Foreach ($PG in $NetworkSystem.NetworkInfo.PortGroup){
        $Details = "" | Select VMHost, vSwitch, PortGroup, ActiveNics, StandbyNics
        $Details.VMHost = $VMHost.Name
        $Details.Portgroup = $PG.Spec.Name
        If ((($PG.ComputedPolicy.NicTeaming.NicOrder.ActiveNic | Select -ExpandProperty $ActiveNic).Length) -gt 1){
            $Details.ActiveNics = [string]::join(';',($PG.ComputedPolicy.NicTeaming.NicOrder.ActiveNic | Select -ExpandProperty $ActiveNic))
        }
        Else {
            $Details.ActiveNics = ($PG.ComputedPolicy.NicTeaming.NicOrder.ActiveNic | Select -ExpandProperty $ActiveNic)
        }
        If ((($PG.ComputedPolicy.NicTeaming.NicOrder.StandbyNic | Select -ExpandProperty $StandbyNic).Length) -gt 1){
            $Details.StandbyNics = [string]::join(';',($PG.ComputedPolicy.NicTeaming.NicOrder.StandbyNic | Select -ExpandProperty $StandbyNic))
        }
        Else{
            $Details.StandbyNics = ($PG.ComputedPolicy.NicTeaming.NicOrder.StandbyNic | Select -ExpandProperty $StandbyNic)
        }
        Foreach ($VS in $NetworkSystem.NetworkInfo.vSwitch){
            If ($VS.Name -eq $PG.Spec.vSwitchName){
                $Details.vSwitch = $VS.Name
                }
        }
        $NetworkInfo += $Details
    }
}
$NetworkInfo | Sort VMHost, VSwitch, PortGroup | Export-Csv $Filename -NoTypeInformation
Invoke-Item $filename

There is also some more advanced network information available via this script.

4 thoughts on “PowerCLI: Host Network Config

  1. jasbo

    Then you can use ITQ Infrastructure Client to copy the correct config from your baseline host to other hosts. I’ve found this free program very helpful and want to give Flores Eken credit & thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.