Scripts to add or remove a PortGroup from an ESX/ESXi Cluster

29 04 2010

For one of our clusters we have ponied up the $$$ for vSphere Enterprise Plus licensing. However, this isn’t a reasonable assumption for most customers, and for most of our clusters Enterprise is as high as we go. Without the support of distributed virtual switches in the environment, keeping all of the hosts consistent can be a time-consuming process. After getting tired of manually adding/removing portgroups (vlans) from our hosts, I created some simple scripts that do this for me.  Each one takes the vlanid and a cluster name as parameters to do the work on.

AddPortGroupToCluster.ps1:

param($cluster, $vlanid)

#—————————–
#—————————–

if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}

$myVIServer = “MYVISERVER”
Connect-VIServer $myVIServer -credential (Get-Credential)

#—————————–
#—————————–

$myCluster = Get-Cluster -Name $cluster

$myVMHosts = Get-VMhost -Location $myCluster

foreach ($myhost in $myVMHosts) {

$myvSwitches = Get-VirtualSwitch -VMHost $myhost

foreach ($vSwitch in $myvSwitches) {

Write-Host “Adding VLAN $vlanid from $myhost…”
New-VirtualPortGroup -VirtualSwitch $vSwitch -Name “VLAN $vlanid” -vlanid $vlanid

}

}

and RemovePortGroupFromCluster.ps1:

param($cluster, $vlanid)

#—————————–
#—————————–

if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}

$myVIServer = “MYVISERVER”
Connect-VIServer $myVIServer -credential (Get-Credential)

#—————————–
#—————————–

$myCluster = Get-Cluster -Name $cluster

$myVMHosts = Get-VMhost -Location $myCluster

foreach ($myhost in $myVMHosts) {

$myvSwitches = Get-VirtualSwitch -VMHost $myhost

foreach ($vSwitch in $myvSwitches) {

$myvPortGroups = Get-VirtualPortGroup -VirtualSwitch $vSwitch

foreach ($portgroup in $myvPortGroups) {

if ($portgroup.VLanId -eq $vlanid) {

Write-Host “Removing VLAN $vlanid from $myhost…”
Remove-VirtualPortGroup -VirtualPortGroup $portgroup -Confirm:$False

}

}

}

}

Advertisements

Actions

Information

One response

25 08 2015
CountryMan

Thank you!, works great!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: