Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Scripts/System/Invoke-WindowsUpdateMaintenance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

.NOTES
File Name : Invoke-WindowsUpdateMaintenance.ps1
Author : IT Operations Team
Author : Leonardo Klein Rezende
Prerequisite : Administrator privileges, PowerShell 5.1+, PSWindowsUpdate module
Creation Date : 2025-10-03

Expand Down Expand Up @@ -188,18 +188,16 @@ function Invoke-UpdateCheck {
}

$updates = Invoke-Command -ComputerName $server -ScriptBlock {
param($Timeout)

Import-Module PSWindowsUpdate -ErrorAction Stop

$updateList = Get-WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot -Verbose:$false

return @{
UpdateCount = $updateList.Count
Updates = $updateList | Select-Object Title, Size, Description
RequiresReboot = $updateList | Where-Object {$_.RebootRequired -eq $true}
}
} -ArgumentList $TimeoutMinutes -ErrorAction Stop
} -ErrorAction Stop

$results[$server] = @{
Status = "Success"
Expand Down Expand Up @@ -465,7 +463,7 @@ try {

"Recheck" {
Write-MaintenanceLog "Starting recheck phase" -Level "INFO" -LogFilePath $logFilePath
$results = Invoke-UpdateCheck -ServerNames $connectivity.Available -LogFilePath $logFilePath -TimeoutMinutes $WSUSTimeoutMinutes -SkipSQLCheck $false
$results = Invoke-UpdateCheck -ServerNames $connectivity.Available -LogFilePath $logFilePath -TimeoutMinutes $WSUSTimeoutMinutes -SkipSQLCheck $SkipSQLServiceCheck.IsPresent
}

"Finalize" {
Expand All @@ -481,7 +479,9 @@ try {
}
catch {
$errorMessage = "Critical error during $Stage stage: $($_.Exception.Message)"
Write-MaintenanceLog $errorMessage -Level "ERROR" -LogFilePath $logFilePath
if ($logFilePath) {
Write-MaintenanceLog $errorMessage -Level "ERROR" -LogFilePath $logFilePath
}
Write-Error $errorMessage
exit 1
}
Expand Down