-
This commit is contained in:
parent
c2fa0c585c
commit
21ac65f282
1 changed files with 43 additions and 24 deletions
65
cleanup.ps1
65
cleanup.ps1
|
|
@ -445,41 +445,60 @@ if ($ScriptResult.windows_updates.pswindowsupdate_installed) {
|
||||||
# Check for and install Windows updates
|
# Check for and install Windows updates
|
||||||
if ($ScriptResult.windows_updates.pswindowsupdate_installed) {
|
if ($ScriptResult.windows_updates.pswindowsupdate_installed) {
|
||||||
try {
|
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
|
# Run Windows update check and install in one command
|
||||||
$AvailableUpdates = Get-WindowsUpdate -AcceptAll -ListOnly -ErrorAction Stop
|
$UpdateOutput = Get-WindowsUpdate -AcceptAll -Install -IgnoreUser -IgnoreReboot -ErrorAction Stop 2>&1 | Out-String
|
||||||
$ScriptResult.windows_updates.updates_available = $AvailableUpdates.Count
|
$UpdateOutput | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
Write-Log "Found $($AvailableUpdates.Count) Windows update(s)." -Level "INFO"
|
|
||||||
|
|
||||||
# Install updates
|
# Parse the output to extract KB numbers and titles
|
||||||
Write-Log "Installing Windows updates..." -Level "INFO"
|
$UpdateLines = $UpdateOutput -split "`n"
|
||||||
$InstallOutput = Get-WindowsUpdate -AcceptAll -Install -ErrorAction Stop 2>&1 | Out-String
|
$UpdatesFound = $false
|
||||||
$InstallOutput | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
$UpdatesInstalled = @()
|
||||||
|
|
||||||
# Parse installed updates
|
foreach ($line in $UpdateLines) {
|
||||||
foreach ($Update in $AvailableUpdates) {
|
# 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 = @{
|
$UpdateInfo = @{
|
||||||
"kb" = $Update.KB
|
"kb" = $kb
|
||||||
"title" = $Update.Title
|
"title" = $title
|
||||||
"description" = $Update.Description
|
"description" = ""
|
||||||
"size_mb" = [math]::Round($Update.Size / 1MB, 2)
|
"size_mb" = 0
|
||||||
"is_downloaded" = $Update.IsDownloaded
|
"is_downloaded" = $true
|
||||||
"reboot_required" = $Update.RebootRequired
|
"reboot_required" = $false
|
||||||
|
}
|
||||||
|
$UpdatesInstalled += $UpdateInfo
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$ScriptResult.windows_updates.updates_installed += $UpdateInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if reboot is required
|
$ScriptResult.windows_updates.updates_installed = $UpdatesInstalled
|
||||||
if ($InstallOutput -match "reboot|Reboot|restart") {
|
$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
|
$ScriptResult.windows_updates.reboot_required = $true
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Log "Windows updates installation completed." -Level "INFO"
|
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 {
|
} 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) {
|
if ($ScriptResult.windows_updates.reboot_required) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue