Created
October 29, 2013 14:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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