Well, I recently decided to rename a bunch of my Standard Port Groups, since they did no longer reflect the network they were providing. Since I’m a lazy bastard (well lazy as in click lazy), I wrote this little PowerCLI script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
param(
	[string] $vcenter,
	[string] $cluster,
	[string] $oldpg,
	[string] $newpg,
	[string] $vlan )

# Add the VI-Snapin if it isn't loaded already
if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null )
{
	Add-PSSnapin -Name "VMware.VimAutomation.Core"
}

If ( !($vcenter) -or !($cluster) -or !($oldpg) -or !($newpg) -or !($vlan) )
{
	Write-Host `n "pg-cluster-rename: <vcenter-server> <cluster> <oldpg> <newpg>" `n
	Write-Host "This script renames each port group with the name <oldpg> to <newpg>" `n
	Write-Host "   <vcenter-server>  - DNS name of your vCenter server." `n
	Write-Host "   <cluster>         - Display-Name of the vCenter cluster, on which we are"
	Write-Host "                       gonna create the new portgroup." `n
	Write-Host "   <oldpg>           - Name of the old Port Group that is to be replaced (ie VLAN2)." `n
	Write-Host "   <newpg>           - Name of the new Port Group (ie PG-VLAN2-Produktion)." `n
	Write-Host "   <vlan>            - VLAN-ID for of the new port group." `n
	exit 1
}

Connect-VIServer -Server $vcenter

Get-Cluster $cluster | Get-VMHost | Get-VirtualSwitch -Name $vswitch | `
        New-VirtualPortGroup -Name $pg -VLanId $vlan
Get-Cluster $cluster | Get-VM | Get-NetworkAdapter | Where { $_.NetworkName -eq "$oldpg" } | `
        Set-NetworkAdapter -NetworkName $newpg -Confirm:$false
Get-Cluster $cluster | Get-VMHost | %{Get-View (Get-View $_.ID).configmanager.networkSystem} | %{
	$_.RemovePortGroup($oldpg)
}

Disconnect-VIServer -server $vcenter -Confirm:$false

This script basically takes a vCenter instance and a single cluster, then creates a new Port Group on each host, after which it reconfigures all VMs possessing a virtual NIC with that Port Group and then deletes the old Port Group.