Created
December 11, 2019 14:53
-
-
Save vMarkusK/28bf0ee01d67a89bd183f1ba863f43c9 to your computer and use it in GitHub Desktop.
Site Aware Patching with VMware Update Manager
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
function Start-ClusterPatch { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True, ValueFromPipeline=$True, HelpMessage="vSphere Cluster to Patch")] | |
[ValidateNotNullorEmpty()] | |
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.ComputeResourceImpl] $Cluster, | |
[Parameter(Mandatory=$True, ValueFromPipeline=$false, HelpMessage="Site to Process")] | |
[ValidateNotNullorEmpty()] | |
[ValidateSet("DC1","DC2")] | |
[String] $SiteToProcess, | |
[Parameter(Mandatory=$True, ValueFromPipeline=$false, HelpMessage="BaselineName to Process")] | |
[ValidateNotNullorEmpty()] | |
[String] $BaselineName, | |
[Parameter(Mandatory=$false, ValueFromPipeline=$false, HelpMessage="Location Tag Category Name")] | |
[ValidateNotNullorEmpty()] | |
[String] $LocationTagCategoryName = "DC_Location" | |
) | |
begin { | |
#region: get vCenter Name from CLuster | |
$ServerName = $Cluster.Uid.Substring($Cluster.Uid.IndexOf('@')+1).Split(":")[0] | |
#endregion | |
#region: Update PowerCLI Configuration | |
$Trash = Set-PowerCLIConfiguration -InvalidCertificateAction "ignore" -Confirm:$false | |
#endregion | |
#region: Get Tag | |
try { | |
$LocationTagCategory = Get-TagCategory -Name $LocationTagCategoryName | |
$LocationTag = Get-Tag -Category $LocationTagCategory -Name $SiteToProcess -Server $ServerName | |
} | |
catch { | |
Throw "Get Tag Failed" | |
} | |
#endregion | |
#region: Get Basline | |
try { | |
$Baseline = Get-Baseline -Name $BaselineName -Server $ServerName | |
} | |
catch { | |
Throw "Get Basline Failed" | |
} | |
} | |
process { | |
#region: Disable HA Admission Control | |
"Disable HA Admission Control for $($Cluster.Name)..." | |
$Trash = $Cluster | Set-Cluster -HAAdmissionControlEnabled:$false -Confirm:$False | |
#endregion | |
#region: Disable DRS Rules | |
"Disable DRS Rules for $($Cluster.Name)..." | |
$Trash = $Cluster | Get-DrsRule | Set-DrsRule -Enabled:$false -ErrorAction SilentlyContinue | |
$Trash = $Cluster | Get-DrsVMHostRule | Set-DrsVMHostRule -Enabled:$false -ErrorAction SilentlyContinue | |
#endregion | |
#region: Get Hosts in Site | |
"Get Hosts in $SiteToProcess for $($Cluster.Name)..." | |
$HostsToProcess = Get-VMHost -Tag $LocationTag | Where-Object {$_.Parent -eq $Cluster} | |
#endregino | |
#region: Enter Maintenance Mode | |
"Enter Maintenance Mode in $SiteToProcess for $($Cluster.Name)..." | |
$Trash = $HostsToProcess | Set-VMHost -State Maintenance -Confirm:$false | |
"Sleep for 10s..." | |
Start-Sleep -Seconds 10 | |
#endregion | |
#region: Get Compliance | |
"Check Compliance in $SiteToProcess for $($Cluster.Name)..." | |
$TestComplianceTask = $HostsToProcess | Test-Compliance -RunAsync | |
$i = 0 | |
while(Get-Task -Id $TestComplianceTask.Id | Where-Object {$_.State -eq "Running"}){ | |
$i++ | |
Start-Sleep 1 | |
Write-Progress -Activity "Check Compliance" -Status "Wait for Compliance Task..." | |
} | |
$ComplianceState = $HostsToProcess | Get-Compliance | |
$ComplianceState | Format-Table -AutoSize | |
#endregion | |
#region: Patch Hosts | |
"Install Patches in $SiteToProcess for $($Cluster.Name)..." | |
#$UpdateEntityTask = ($ComplianceState | Where-Object {$_.Status -eq "NotCompliant"}).Entity | Update-Entity -Baseline $Baseline -RunAsync -Confirm:$False | |
$UpdateEntityTask = $HostsToProcess | Update-Entity -Baseline $Baseline -RunAsync -Confirm:$False | |
$i = 0 | |
while(Get-Task -Id $UpdateEntityTask.Id | Where-Object {$_.State -eq "Running"}){ | |
$i++ | |
Start-Sleep 5 | |
Write-Progress -Activity "Run Update" -Status "Wait for Update Task..." | |
} | |
#endregion | |
#region: Get Compliance | |
"Re-Check Compliance in $SiteToProcess for $($Cluster.Name)..." | |
$TestComplianceTask = $HostsToProcess | Test-Compliance -RunAsync | |
$i = 0 | |
while(Get-Task -Id $TestComplianceTask.Id | Where-Object {$_.State -eq "Running"}){ | |
$i++ | |
Start-Sleep 1 | |
Write-Progress -Activity "Check Compliance" -Status "Wait for Compliance Task..." | |
} | |
$ComplianceState = $HostsToProcess | Get-Compliance | |
$ComplianceState | Format-Table -AutoSize | |
#endregion | |
if ($ComplianceState.Status -eq "Compliant") { | |
"All Hosts in $SiteToProcess for $($Cluster.Name) Compliant. Re-Enabling Site $SiteToProcess" | |
#region: Exit Maintenance Mode | |
"Exit Maintenance Mode in $SiteToProcess for $($Cluster.Name)..." | |
$Trash = $HostsToProcess | Set-VMHost -State Connected -Confirm:$false | |
"Sleep for 10s..." | |
Start-Sleep -Seconds 10 | |
#endregion | |
#region: Enable HA Admission Control | |
"Enable HA Admission Control for $($Cluster.Name)..." | |
$Trash = $Cluster | Set-Cluster -HAAdmissionControlEnabled:$true -Confirm:$False | |
#endregion | |
#region: Enable DRS Rules | |
"Enable DRS Rules for $($Cluster.Name)..." | |
$Trash = $Cluster | Get-DrsRule | Set-DrsRule -Enabled:$true -ErrorAction SilentlyContinue | |
$Trash = $Cluster | Get-DrsVMHostRule | Set-DrsVMHostRule -Enabled:$true -ErrorAction SilentlyContinue | |
#endregion | |
} | |
else { | |
Write-Error "In-Compliant Hosts in $SiteToProcess for $($Cluster.Name)! Please check Status." | |
} | |
} | |
end { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment