Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Perform steps to upgrade all ESXi hosts in a cluster.
- .DESCRIPTION
- Use this script to upgrade all ESXi hosts in a cluster based on an upgrade baseline.
- This script assumes availability of the following modules:
- - VMware-PowerCli
- .PARAMETER
- - Cluster: name of the cluster to migrate.
- - Baseline: name of the upgrade baseline.
- .EXAMPLE
- .\Upgrade-Cluster.ps1 -Cluster <clustername>
- .NOTES
- Script name: Upgrade-Cluster.ps1
- Author: Jeroen Buren
- DateCreated: 07-03-2020
- #>
- [CmdletBinding(SupportsShouldProcess=$true)]
- Param(
- [parameter(valuefrompipeline = $true, mandatory = $true,
- HelpMessage = "Enter the name of the cluster to upgrade.")]
- [PSObject]$ClusterName,
- [parameter(mandatory = $true,
- HelpMessage = "Enter the name of the Host Upgrade Baseline to apply.")]
- [String]$Baseline
- )
- ####################################################################################
- # #
- # Functions #
- # #
- ####################################################################################
- function Get-ScriptDirectory {
- $Invocation = (Get-Variable MyInvocation -Scope 1).Value
- Split-Path $Invocation.MyCommand.Path
- }
- function Log {
- param(
- [Parameter(Mandatory=$true)][String]$Message,
- [Parameter(Mandatory=$true)][ValidateSet("Info","Debug","Warn","Error")][String]$Type,
- [Parameter(Mandatory=$true)][ValidateSet("Console","LogFile","Both")][String]$OutputMode
- )
- $dateTimeString = Get-Date -Format "yyyy-MM-dd HH:mm:sszz"
- $output = ($dateTimeString + " " + $type.ToUpper() + " " + $message)
- if ($outputMode -eq "Console" -OR $outputMode -eq "Both") {
- Write-Host $output
- }
- if ($outputMode -eq "LogFile" -OR $outputMode -eq "Both") {
- try {
- Add-Content $logFile -Value $output -ErrorAction Stop
- }
- catch {
- Log ("Failed to write to log file: """ + $logFile + """.") -OutputMode Console -Type Error
- Log ("[" + $_.Exception.GetType().FullName + "] " + $_.Exception.Message) -OutputMode Console -Type Error
- }
- }
- }
- ####################################################################################
- # #
- # Variables #
- # #
- ####################################################################################
- # Logfile
- Set-Variable logFile ((Get-ScriptDirectory) + "\Upgrade-Cluster.log") -Option Constant -ErrorAction SilentlyContinue
- $vCenter = "vCenter"
- $vib = "cisco-vem-v320-esx"
- ####################################################################################
- # #
- # Main Code #
- # #
- ####################################################################################
- Log -Message "Script started" -Type Info -OutputMode LogFile
- # Connect vCenter Server
- Connect-VIserver -Server $vCenter | Out-Null
- Log -Message "Connected to vCenter Server" -Type Info -OutputMode Both
- # Get cluster object
- $cluster = Get-Cluster $clusterName
- # Disable HA
- Set-Cluster -Cluster $cluster -HAEnabled:$false -Confirm:$false | Out-Null
- # Disconnect all ISOs that may be attached to VMs and set the CD Drive state to "disconnected"
- Log -Message "Disconnecting CD drives" -Type Info -OutputMode Both
- $cluster | Get-VM | Get-CDDrive | Where-Object {($_.IsoPath -ne $null) -or ($_.ConnectionState.Connected -eq "true")} | Set-CDDrive -NoMedia -Connected:$false -Confirm:$false
- # Apply baseline to cluster
- Add-EntityBaseline -Entity $cluster -Baseline (Get-Baseline -Name $Baseline -BaselineType Upgrade)
- Add-EntityBaseline -Entity $cluster -Baseline (Get-PatchBaseline -Name "Critical Host Patches (6.7)")
- # Test baseline compliance on the cluster
- Test-Compliance -Entity $cluster
- # Check for host compliance to the applied baseline and select all hosts that are not compliant
- $hostNotComp = $cluster | Get-VMHost | Get-Compliance | Where-Object {$_.Status -ne "Compliant"}
- # Upgrade hosts
- Log -Message "Beginning baseline installation" -Type Info -OutputMode Both
- foreach ($h in $hostNotComp) {
- # Enter maintenance mode
- Log -Message "Host $($h.Entity) is entering Maintenance Mode" -Type Info -OutputMode Both
- Set-VMHost -VMHost $h.Entity -State Maintenance | Out-Null
- # Wait for the host to enter Maintenance Mode
- do {Start-Sleep -Seconds 5} while ((Get-VMHost -Name $h.Entity).ConnectionState -ne 'Maintenance')
- # Start removing the VIB
- Log -Message "Removing VIB from host" -Type Info -OutputMode Both
- $esxcli = Get-EsxCli -VMHost $h.Entity -V2
- $param = $esxcli.software.vib.remove.CreateArgs()
- $param.dryrun = $false
- $param.vibname = $vib
- $param.force = $false
- $esxcli.software.vib.remove.Invoke($param) | Out-Null
- # Update the host
- Log -Message "Installing baseline $Baseline on host $($h.Entity)" -Type Info -OutputMode Both
- Update-Entity -Entity $h.Entity -Baseline (Get-Baseline -Name $Baseline -BaselineType Upgrade) -Confirm:$false | Out-Null
- # Now check compliancy against patches baseline
- Test-Compliance -Entity $h.Entity -UpdateType HostPatch
- # Patch the host
- Log -Message "Installing critical host patches on host $($h.Entity)" -Type Info -OutputMode Both
- Update-Entity -Entity $h.Entity -Baseline (Get-Baseline -BaselineType Patch -Name "Critical Host Patches (6.7)") -Confirm:$false | Out-Null
- # Exit maintenance mode
- Log -Message "Host $($h.Entity) is exiting Maintenance Mode" -Type Info -OutputMode Both
- Set-VMHost -VMHost $h.Entity -State Connected | Out-Null
- # Wait for the host to leave Maintenance Mode
- do {Start-Sleep -Seconds 5} while ((Get-VMHost -Name $h.Entity).ConnectionState -ne 'Connected')
- # Next host...
- }
- # Enable HA
- Set-Cluster -Cluster $cluster -HAEnabled:$true -Confirm:$false | Out-Null
- # Disconnect from vCenter Servers
- Disconnect-VIServer -Server $vCenter -Confirm:$false
- Log -Message "Script Ended" -Type Info -OutputMode LogFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement