#PowerCLI: Distributed switch portgroup usage. Good vs. bad vs. Max Power approach

From now on there are three ways of doing things: the right way, the wrong way, and the Max Power way.

Isn’t that the wrong way?

Yes! But faster!

This quote from Homer Simpsons came directly into my mind when I was doing some PowerCLI scripting during the week.

maxpower

I started with the wrong / Max Power way and suddenly came to a much more smarter solution – the right way.

The task was to gather the usage on the virtual distributed switch portgroups within a world-wide environment with around 30 vCenter. (Final script on the bottom of this blog)

Once again I realized there are many roads to Rome and even with PowerCLI you can either go there by crawling or using a plane.

My first approach was to get each portgroup and have a look through each port if it has a connection to the virtual network adapter of a VM (each VM only has 1 Network adapter).

$ports = Get-VDSwitch | Get-VDPort -VDPortgroup $pg
$portsconnected = $ports | Where {$_.ConnectedEntity.Name -eq 'Network adapter 1'}

That approach was incredibly slow (> 12 hours) since it took a while to get all port objects of the distributed switch (more than 5000 per vDS).

Thanks to Brian Graf‘s great blog article we know how to access vSphere objects extension data in a much more elegant way.

$networks = Get-View -viewtype Network
Foreach ($network in $networks){
    $pgname = $network.Name
    $connectedports = ($network.VM).count 
}

Doing it that way it took 15 minutes instead of 12++ hours.

It really makes a huge difference if you code something right or wrong. That’s counts for Software, SQL-queries and also for all kind of scripts we use and built in our daily IT-infrastructure world.

The final script gives you an csv output file with the values

Datacenter, PortgroupName, VLANID, NumberOfConnectedPorts

Make sure to use Powershell 3.0++ so you can use the -append option in the export-csv cmdlet.

Enjoy.

Good one

$results = @()

$cluster = get-cluster | Sort-Object -Property Name
$dcName = (Get-Datacenter).Name
$networks = Get-View -viewtype Network

Foreach ($network in $networks){
    $pgname = $network.Name
    $pg = Get-VDPortgroup -Name $pgname
    $vlanid = $pg.vlanConfiguration.VlanID
    $connectedports = ($network.VM).count

    $details = @{
        PortgroupName = $pgname
        VLANID = $vlanId
        NumberOfConnectedPorts = $connectedports
        Datacenter = $dcName
    }

    $results += New-Object PSObject -Property $details
}

$results | export-csv -Append -Path c:\temp\newvDSnetworkConnected.csv -NoTypeInformation

 Bad one

$results = @()

$dcName = (Get-Datacenter).Name
$pgs = Get-VDSwitch | Get-VDPortgroup | Where {$_.IsUplink -ne 'True'}

foreach ($pg in $pgs){

    $ports = Get-VDSwitch | Get-VDPort -VDPortgroup $pg
    $portsconnected = $ports | Where {$_.ConnectedEntity.Name -eq 'Network adapter 1'}

    $pgname = $pg.name
    $vlanId = $pg.VlanConfiguration.VlanId

    $connectedports = $portsconnected.count
    $details = @{
        PortgroupName = $pgname
        VLANID = $vlanId
        NumberOfConnectedPorts = $connectedports
        Datacenter = $dcName
    }
    $results += New-Object PSObject -Property $details 
} 

$results | export-csv -Append -Path c:\temp\vDSnetworkConnected.csv -NoTypeInformation

5 Comments

  1. Great work!

    I ran into an issue testing this… Lets say a VM has multiple nics on the same VDPortgroup.

  2. $VDPGs = get-vdportgroup | ?{$_.IsUplink -eq $false}

    ForEach($VDPG in $VDPGs){
    $reqports = (Get-VDPort -ConnectedOnly -VDPortgroup $VDPG).count

    $VDPG | Set-VDPortgroup -NumPorts $reqports
    }

  3. Fabian Lenz

    July 29, 2015 at 1:37 pm

    I haven’t tested it in this scenario. Let me ask why you have multiple virtual network adapter on the same portgroup in 1 VM?

    IMO the ‘good one’ approach should work independant of the number of vNICs per VM, but I will try to validate it.

  4. Hi,

    Big thanks!

    But why are you querying the “cluster” first.
    Is there anyway to have this output format : Datacenter, Cluster, PortgroupName

    Best regards,

  5. hi, I’m running the same scenario where the windows VMs have 3 NICS
    1st nic – for RDP (vlan 22/23)
    2 and 3 – software request (primary and secondary network). (vlan 500 or higher)

    can you help me with this ?

Leave a Reply

Your email address will not be published.

*

CAPTCHA

*

© 2017 v(e)Xpertise

Theme by Anders NorenUp ↑