dvSwitch scripting – Part 12 – Find free ports

On the PowerCLI Community there was an interesting question about dvSwitches, portgroups and connecting VMs. Turns out you will need to provide a free port to connect a VM’s NIC to a portgroup on dvSwitch.
Since the solution is a nice follow up on my previous, somewhat lengthy post, called Variations on a port, I decided to create a short post on the subject in my dvSwitch series.

Update 3th March 2016: added test to capture portgroups with no VM connected to it

To connect a VM’s NIC to a dvSwitch portgroup you can use the Set-NetworkAdapter cmdlet like this.

But how do we get that PortKey number ?

Since PowerCLI is all about automation, we are not going to use the vSphere client to get the number. Hence the following function.

The script


Line 27-28: All the possible NIC types

Line 29: To collect the ports, the script uses a hash table.

Line 32: The script adds all ports available on the portgroup to the hash table

Line 34: Skip the following code block when there are no VMs connected to this portgroup

Line 35: For all the VMs connected to the portgroup, the script will find the NICs connected to this portgroup.

Line 39: For the NICs that are connected to the portgroup, the script will remove the corresponding portkey from the hash table

Line 43-45: If the caller asked more ports than there are free on the portgroup, the function will return all the free ports.

Line 46: The script returns the free ports in ascending order

Sample usage

The use of the function is quite simple. Just pass the name of the portgroup and the number of ports you want returned.

The result is an ordered list of 5 free ports, which you can use on the PortKey parameter of the Set-NetworkAdapter cmdlet.

Enjoy !




    Thanks so much for this awesome script. I am having some mixed results on the amount of time it takes to find a free port. For example using the function above for one VM it might take 18 secs to find a free port and then the next VM (same vCenter, different PG) it might take 41 secs. Any reason why it would take double the time? Anything I can change to make it go faster? I know I was brief but can provide more info if needed.



    Thanks for this script. It does appear to still generate an accurate number PortKey, but I get the following error when it runs.

    Get-View : Cannot validate argument on parameter ‘VIObject’. The argument is nu
    ll, empty, or an element of the argument collection contains a null value. Supp
    ly a collection that does not contain any null values and then try the command
    At C:\script\Get-dvPgFreePort.ps1:34 char:11
    + Get-View <<<< $pg.ExtensionData.Vm | %{
    + CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingVal
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

    My environment utilizes the Cisco Nexus 1000V, so I wonder if that has something to do with it.

Leave a Reply

Your email address will not be published.


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