Add Portgroups/VLANs to vmware standard switches via PowerCLI

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

Comments

Anonymous said…
Works perfectly, thanks!
Ravi Shankar said…
superb works great..thanks a lot..you are really genius..
Unknown said…
Genius u just save me 2 hours of repetitive work
Thanks

Popular posts from this blog

NSX-T: Release associated invalid node ID from certificate

NSX-T: vCenter and NSX-T Inventory out of Sync (Hosts in vSphere not showing up in NSX-T)

MacOS: AnyConnect VPN client was unable to successfully verify the IP forwarding table modifications.