Apr 23

I recently had to get caught up on VMware upgrades, specifically going from vCenter 6.0 to 6.7… I know it was over due but I have to confess loosing the fat client is going to hurt.

Anyway in doing my pre-checks I came across the requirement of the distributed switch version 6. Well needless to say I had some still at version 5.5, I guess this slipped my mind since the last 5.5 updates. Anyway back in the day I would just do this via the fat client and be done with it but since the fat client is soon to be history and the web client is so much slower to get things done I decided I was going to automate or script it. This also gave me the chance to incorporate the items that sometimes gets missed, case in point the only way to backup the switch is via the web client but updating and DRS functions can be performed in either, did I mention I’m really going to miss the fat client!

Anyway my script first checks the current version just in case you forgot it was already updated, then if an update is needed grabs the DRS state of the cluster, takes a backup of the dvSwitch, sets the cluster to PartiallyAutomated (this part and the backup are often missed) updates the dvSwitch and then sets the cluster back to the DRS state. Pretty cool eh?

So here is the code… I’ve hard coded a few things just because the way powercli reads and sets the dvSwitch version.

# Script to update distributed switches
# Gets current state of DRS for cluster
# Sets cluster to PartiallyAutomated if needed
# dvswitch config backed up to d:\backups directory
# Updates dvswitch to level in $vdversion
# Sets cluster back to previous DRS setting
# added checks for current version dvs
#
#–BS

param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[String]
$vCenter,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[String]
$ClusterName,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[String]
$VDSwitch

    )

import-module VMware.VimAutomation.Vds

Connect-VIServer $vCenter | Out-Null

$VDVersion = “6.0.0”
$Cluster = Get-Cluster $ClusterName
$switchversion = Get-VDSwitch -Name $VDSwitch | select-object version

If ($switchversion.version -lt $VDVersion ) {
Write-Host (get-date) “: The DVSwitch for $clustername can be udated to v$VDVersion” -ForegroundColor “yellow”
}

If ($switchversion.version -gt $VDVersion ) {
Write-Host (get-date) “: The DVSwitch for $clustername appears to be at a higher level than v$VDVersion. No additional action is needed.” -ForegroundColor “green”
break;
exit
}

ElseIf ($switchversion.version -eq $VDVersion) {
Write-Host (get-date) “: The DVSwitch for $clustername appears to be at v$VDVersion already, No additional action is needed.” -ForegroundColor “Green”
break;
exit
}

If ($Cluster.DrsEnabled -like “True”-And $switchversion -notlike $VDVersion ) {
Write-Host (get-date) “: DRS is Enabled, it will be temporarily disabled during upgrade.” -ForegroundColor “Yellow”
$ClusterDRSLevel = $Cluster.DrsAutomationLevel
Write-Host (get-date) “: DRS Cluster is currently set to $ClusterDRSLevel. It will changed back when complete.” -ForegroundColor “yellow”
Get-Cluster $Cluster | Set-Cluster -DrsAutomationLevel “PartiallyAutomated” -Confirm:$false
Get-VDSwitch -Name $VDSwitch | select-object Name,Version
Get-VDSwitch -Name $VDSwitch | Export-VDSwitch -Description “dvs Backup” -Destination “d:\backups\$VDswitch-$((Get-Date).ToString(‘yyyy-MM-dd-hh-mm’)).zip” -Force
Get-VDSwitch -Name $VDSwitch | Set-VDSwitch -Version $VDVersion
Write-Host (get-date) “: Upgrade is complete. Setting Cluster back to $ClusterDRSLevel.” -ForegroundColor “Green”
Get-Cluster $Cluster | Set-Cluster -DrsAutomationLevel $ClusterDRSLevel -Confirm:$false
Get-VDSwitch -Name $VDSwitch | select-object Name,Version

}
ElseIf ($Cluster.DrsEnabled -like “False”) {
Write-Host (get-date) “: DRS is Disabled, No additional action needed.” -ForegroundColor “Green”
Get-VDSwitch -Name $VDSwitch | select-object Name,Version
Get-VDSwitch -Name $VDSwitch | Export-VDSwitch -Description “dvs Backup” -Destination “d:\backups\$VDswitch-$((Get-Date).ToString(‘yyyy-MM-dd-hh-mm’)).zip” -Force
Get-VDSwitch -Name $VDSwitch | Set-VDSwitch -Version $VDVersion
Write-Host (get-date) “: Upgrade is complete.” -ForegroundColor “Green”
Get-VDSwitch -Name $VDSwitch | select-object Name,Version
}

Disconnect-Viserver $vcenter -confirm:$false | out-null

Leave a Reply

preload preload preload