Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BenNeise/7216135 to your computer and use it in GitHub Desktop.
Save BenNeise/7216135 to your computer and use it in GitHub Desktop.
Script to add a hash table full of virtual port groups to vSphere hosts
# Sets up virtual port groups on all hosts connected to a specific vCenter Server
# Name of vCenter Server
$strVCenterServer = "your.vCenter.Server"
# VLANs and associated VPGs
$ArrVLANs = @{
"123" = "vlanA";
"456" = "vlanB";
"789" = "vlanC";
}
# Connect to the vCenter Server
Connect-VIserver -Server $strVCenterServer
# Loop through the VLAN/VPG pairs
ForEach($objVLAN in ($ArrVLANs.Keys | Sort-Object)){
# Loop through the hosts
ForEach ($objHost in (Get-VMHost | Sort-Object)){
# Create the VPG with the VLAN as specified in the array above, on the switch called "VMSwitch" on the current host
# Remove the "-WhatIf" tag from the end of the following line to "arm" the script
New-VirtualPortGroup -Name $strNewVPG -VirtualSwitch (Get-Virtualswitch -VMHost $objHost | Where-Object { $_.Name -match "VMswitch" }) -VLanId $strNewVlanTag
# Write what we've just done to screen
Write-Host ("Adding Virtual Port Group $($ArrVLANs[$objVLAN]) with VLAN Tag $objVLAN to $objHost")
}
}
# Disconnect the session from the host
Disconnect-VIServer -Confirm:$False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment