PowerCLI: Adding New PortGroups

I'm sure that someone somewhere has written a script exactly like this in the past but I wanted to write my own for a number of reasons. While I'll probably never be a PowerCLI hero, it really doesn't hurt to keep in practice and hone your skills.

Let's start with what I want to accomplish. I'm working with an ESX 3.5 web hosting environment and there's a new project in the pipelines. A brace of new servers are required and they'll be on a new VLAN. The VLAN has been created and configured on the various switches that the ESX hosts connect to but now of course a corresponding PortGroup is required. (Actually two are needed – complex project.) Now we're not talking about a huge number of hosts here. It would probably only take 10 minutes to do it by hand using the VI client. It'll take me longer to write this post! However, it is something that happens relatively often in this environment so it's worth taking the time to write a script.

Let's say, for the sake of simplicity, that I have 5 or 6 hosts with near identical networking configurations. The same number of NICs in each host, the same NICs attached to the same vSwitches in each host.

The command that we'll need to use to create the PortGroup will be New-VirtualPortGroup. We'll need to give it a name for the PortGroup and a VLAN ID too. Crucially it will need to know which vSwitch we want the new PortGroup to be placed on. Many environments will have more that one vSwitch and PowerCLI won't be able to guess for you. The VirtualSwitch parameter of the New-VirtualPortGroup cmdlet needs to be an object so we have to use the Get-VirtualSwitch cmdlet with sufficient detail to identify the vSwitch that we want uniquely. Using the vSwitch name and the VMHost gives a unique combination.

Finally, to add the PortGroup to all hosts in a cluster we have to call the New-VirtualPortGroup cmdlet once per VMHost. This can be done using the PowerShell pipeline.

So, now that I've confused you with a few parapgraphs of waffle, adding a new PortGroup is actually pretty simple. In fact it could be a one liner once you are connected to vCenter. Something like this:

[ps]Get-Cluster -Name $cluster | Get-VMHost | foreach { New-VirtualPortGroup -VirtualSwitch ( Get-VirtualSwitch -Name $vswitch -VMHost $_ ) -Name $PG -VLanId $vlan }[/ps]

Of course you'd need to insert your own values for $cluster, $vswitch, $PG and $vlan as follows:

$cluster = The name of the cluster in vCenter that your hosts are in

$vswitch = The name of the vSwitch that you want to add the PortGroup to

$PG = The name of the PortGroup that you want to add

$vlan = The VLAN ID that the PortGroup should use (0 can be used to indictae no VLAN ID)

It should guarantee that if you make any spelling mistakes / typos that they are at least consistent across your infrastructure 😉 You could tack -whatif onto the end of the New-VirtualPortGroup command to test it before making any changes. As always, be careful when you make scripted changes. If possible test them in a development environment first.

To take this further, you could convert the whole thing into a script that takes input from the command prompt or as parameters.