Adding/Removing Virtual Machines’ PortGroups for Standard Switch at cluster level with PowerCLI

Starting with vSphere 4, a nice network enhancement has been brought:

Virtual Distributed Switchs (vDS)

Except for the specifics features bound to the vDS, it is nice to set only one time a new VMs’ PortGroup for an entire group of hosts. Of course you have pro and cons between vDS and Standard Switchs. For me, one big disadvantage of the vDS is that you must have an Enterprise Plus or a vSphere for Desktop licence to use it with your hosts.

But I will only stick to the centralized “add and remove” of VMs PortGroups.

It is true that when I must add or remove a PortGroup in a cluster with 20 hosts might take some time if you don’t use a vDS. And I have several clusters like that. No money for Enterprise Plus 🙁

So I asked some help from my pal –> PowerCLI

I made a handy script to add a new VMs’ PortGroup in Standard Switch to every host in a cluster. And of course I made one to remove PortGroup from hosts in a cluster.

This script has been successfully tests against vSphere versions: 4.x and 5.x

To add a PortGroup:

“Add_PortGroup_at_Cluster_Level” -> Download-Script

In my lab I have a Cluster with only 1 PortGroup configured for Virtual Machines.

In this script you have to change the credential used to connect to the vCenter server, set the correct vCenter address and set the DataCenter name. By default it will add the PortGroup to the default Swith: “vSwitch0”. You can change it if you need. The only settings you can specify during the addition are the PortGroup’s Name and its VLAN. The other settings are by default.

Once the script launched and authenticated, you will have a list box containing every cluster present in the Data Center:

Select the cluster in which you want to add the PortGroup

Enter a name for your new PortGroup

The script will check if a PortGroup with the same name already exists and exit if so.

Then you specify a VLAN number between 0 and 4095.

The PortGroup will be added to the hosts:

This will be reflected at the cluster level:

And at the host level

To remove a PortGroup:

“Remove_PortGroup_at_Cluster_Level” -> Download-Script

In my lab I have a Cluster with 2 PortGroups configured for Virtual Machines.

Like the other script you have to change the credential used to connect to the vCenter server, set the correct vCenter address and set the DataCenter name.

The only setting you can specify during the removal is the PortGroup’s Name.

Once the script launched and authenticated, you will have a list box containing every cluster present in the Data Center:

Select the cluster in which you want to remove the PortGroup

Enter a name of the PortGroup you want to remove

At this point, the script checks if no VMs are connected to the PortGroup. Otherwise it warns you and stops:

If no VMs are connected, the PortGroup is removed from the hosts:

This will be reflected at the cluster level:

Leave a Comment