Skip to content

Instantly share code, notes, and snippets.

@MrXermon
Last active September 3, 2018 09:42
Show Gist options
  • Save MrXermon/c8c0878c10c9a11fbfb67295ac805a9b to your computer and use it in GitHub Desktop.
Save MrXermon/c8c0878c10c9a11fbfb67295ac805a9b to your computer and use it in GitHub Desktop.
Update the uplink of all portgroups on a DVS to a lag.
# Move vCenter Portgroup Uplinks to DVS LAG
# Jan Gilla / www.jangilla.de
$Settings_vCenter = ''
$Settings_vCenter_User = ''
$Settings_vCenter_Password = ''
$Settings_vCenter_DVS = 'dS-FRA1'
$Settings_vCenter_Uplink = 'lag1'
# Connect to vCenter
$vCenter = Connect-VIServer -Server $Settings_vCenter -User $Settings_vCenter_User -Password $Settings_vCenter_Password
if($vCenter){
Write-Host "Connection to vCenter ($Settings_vCenter) successfull..."
# Search DVS in vCenter
$dVS = Get-VDSwitch -Name $Settings_vCenter_DVS
if($dVS){
Write-Host "Found DVS ($Settings_vCenter_DVS) in vCenter..."
# Find Portgroup in DVS
$portGroups = Get-VDPortgroup -VDSwitch $dVS
if($portGroups -and $portGroups.Count){
Write-Host "Found"$portGroups.Count"portgroups on the DVS..."
# Iterate through the portgroups
foreach($portGroup in $portGroups){
# Don't update VXLAN portgroups and uplink portgroup
if(-not $portGroup.Name.StartsWith("vxw-") -and -not $portGroup.Name.StartsWith($Settings_vCenter_DVS)){
# Fetch portgroup Teaming and Failover policy
$portgroup_tpls = Get-VDUplinkTeamingPolicy -VDPortgroup $portGroup
# Check if uplink is correct
if(-not $portgroup_tpls.ActiveUplinkPort.Contains($Settings_vCenter_Uplink)){
# Remove "Uplink 1" and set 'lag1' as active uplink
if(Set-VDUplinkTeamingPolicy -UnusedUplinkPort 'Uplink 1' -ActiveUplinkPort $Settings_vCenter_Uplink -LoadBalancingPolicy LoadBalanceSrcId -Policy $portgroup_tpls){
Write-Host "Updated portgroup:"$portGroup.Name
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment