by Roger Lund

Today I found my self looking for a way to add portgroups to a set of hosts in a cluster that have Standard switches.

I found Chris Nakagaki’s post http://tech.zsoldier.com/2013/07/add-portgroupsvlans-to-vmware-standard.html that covered this completely.

“Summary:
Wrote a simple little script to insert a portgroup into a targeted vSwitch of all VM hosts in a targeted cluster.  This is not an issue if you use distributed vSwitches.

Script:
$vCenterName = Read-Host -Prompt “Please provide name of vCenter server”
$vCenter = Connect-VIServer $vCenterName

If ($? -ne $true){
Write-Host “$($vCenterName) appears to be invalid, please rerun script w/ correct vCenter name.”
Exit
}

$ClusterName = Read-Host -Prompt “Provide name of cluster you’d like to add a new portgroup to”
$Cluster = Get-Cluster $ClusterName

If ($? -ne $true){
Write-Host “$($ClusterName) appears to be invalid, please rerun script w/ correct cluster name.”
Exit
}

$VLAN = Read-Host -Prompt “Please provide VLAN ID: (0-4094 are valid values)”
$PGName = Read-Host -Prompt “Please provide name of port group: (i.e. 10.64.120.x)”
$vSwitchName = Read-Host -Prompt “Please provide vSwitch ID you’d like to add the portgroup to: (i.e. vSwitch2)”

$TargetHosts = $Cluster | Get-VMHost
    Foreach ($VMHost in $TargetHosts)
    {
    $VMHost | Get-VirtualSwitch -Name $vSwitchName | New-VirtualPortGroup -Name $PGName -VLanId $VLAN
    }

Disconnect-VIServer $vCenter -Confirm:$false

All credit to Chris and his blog http://tech.zsoldier.com/2013/07/add-portgroupsvlans-to-vmware-standard.html

You may also like