-
This commit is contained in:
parent
c2fa0c585c
commit
21ac65f282
1 changed files with 43 additions and 24 deletions
67
cleanup.ps1
67
cleanup.ps1
|
|
@ -445,41 +445,60 @@ if ($ScriptResult.windows_updates.pswindowsupdate_installed) {
|
|||
# Check for and install Windows updates
|
||||
if ($ScriptResult.windows_updates.pswindowsupdate_installed) {
|
||||
try {
|
||||
Write-Log "Checking for Windows updates..." -Level "INFO"
|
||||
Write-Log "Checking for and installing Windows updates..." -Level "INFO"
|
||||
|
||||
# Get list of available updates first
|
||||
$AvailableUpdates = Get-WindowsUpdate -AcceptAll -ListOnly -ErrorAction Stop
|
||||
$ScriptResult.windows_updates.updates_available = $AvailableUpdates.Count
|
||||
Write-Log "Found $($AvailableUpdates.Count) Windows update(s)." -Level "INFO"
|
||||
# Run Windows update check and install in one command
|
||||
$UpdateOutput = Get-WindowsUpdate -AcceptAll -Install -IgnoreUser -IgnoreReboot -ErrorAction Stop 2>&1 | Out-String
|
||||
$UpdateOutput | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
|
||||
# Install updates
|
||||
Write-Log "Installing Windows updates..." -Level "INFO"
|
||||
$InstallOutput = Get-WindowsUpdate -AcceptAll -Install -ErrorAction Stop 2>&1 | Out-String
|
||||
$InstallOutput | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
# Parse the output to extract KB numbers and titles
|
||||
$UpdateLines = $UpdateOutput -split "`n"
|
||||
$UpdatesFound = $false
|
||||
$UpdatesInstalled = @()
|
||||
|
||||
# Parse installed updates
|
||||
foreach ($Update in $AvailableUpdates) {
|
||||
$UpdateInfo = @{
|
||||
"kb" = $Update.KB
|
||||
"title" = $Update.Title
|
||||
"description" = $Update.Description
|
||||
"size_mb" = [math]::Round($Update.Size / 1MB, 2)
|
||||
"is_downloaded" = $Update.IsDownloaded
|
||||
"reboot_required" = $Update.RebootRequired
|
||||
foreach ($line in $UpdateLines) {
|
||||
# Look for KB pattern in the output
|
||||
if ($line -match "KB\d+" -or $line -match "Update for|Security Update|Cumulative Update") {
|
||||
$UpdatesFound = $true
|
||||
|
||||
# Extract KB number
|
||||
$kbMatch = [regex]::Match($line, 'KB\d+')
|
||||
$kb = if ($kbMatch.Success) { $kbMatch.Value } else { "Unknown" }
|
||||
|
||||
# Extract title (remove KB number and extra spaces)
|
||||
$title = $line -replace 'KB\d+\s*' -replace '\s+' -replace '^\s*|\s*$' -trim
|
||||
|
||||
if ($title -and $title.Length -gt 3) {
|
||||
$UpdateInfo = @{
|
||||
"kb" = $kb
|
||||
"title" = $title
|
||||
"description" = ""
|
||||
"size_mb" = 0
|
||||
"is_downloaded" = $true
|
||||
"reboot_required" = $false
|
||||
}
|
||||
$UpdatesInstalled += $UpdateInfo
|
||||
}
|
||||
}
|
||||
$ScriptResult.windows_updates.updates_installed += $UpdateInfo
|
||||
}
|
||||
|
||||
# Check if reboot is required
|
||||
if ($InstallOutput -match "reboot|Reboot|restart") {
|
||||
$ScriptResult.windows_updates.updates_installed = $UpdatesInstalled
|
||||
$ScriptResult.windows_updates.updates_available = $UpdatesInstalled.Count
|
||||
|
||||
# Check if reboot is required from output
|
||||
if ($UpdateOutput -match "reboot|Reboot|restart|restart your computer") {
|
||||
$ScriptResult.windows_updates.reboot_required = $true
|
||||
}
|
||||
|
||||
Write-Log "Windows updates installation completed." -Level "INFO"
|
||||
if ($AvailableUpdates.Count -gt 0) {
|
||||
Write-Log "Installed $($AvailableUpdates.Count) update(s)." -Level "INFO"
|
||||
|
||||
if ($UpdatesInstalled.Count -gt 0) {
|
||||
Write-Log "Installed $($UpdatesInstalled.Count) update(s):" -Level "INFO"
|
||||
foreach ($update in $UpdatesInstalled) {
|
||||
Write-Log " - $($update.kb): $($update.title)" -Level "INFO"
|
||||
}
|
||||
} else {
|
||||
Write-Log "No Windows updates were needed." -Level "INFO"
|
||||
Write-Log "No Windows updates were needed or found." -Level "INFO"
|
||||
}
|
||||
|
||||
if ($ScriptResult.windows_updates.reboot_required) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue