Created
August 16, 2020 19:02
-
-
Save moddingg33k/972fc87ec296d2768726c81463c3611c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Param( | |
[switch] $RebootAfterInstall | |
) | |
# Default Variables | |
# ----------------- | |
$PSDefaultParameterValues = @{ | |
"Out-File:Encoding" = 'utf8'; | |
}; | |
$sVar = @{ | |
ClientApplicationID = 'UpdateOrchestrator'; | |
}; | |
$ServerSelection = '1'; | |
$ServiceID = @( | |
'00000000-0000-0000-0000-000000000000', | |
'3da21691-e39d-4da6-8a4b-b43877bcb1b7', | |
'9482f4b4-e343-43b6-b170-9a65bc822c77', | |
'7971f918-a847-4430-9279-4a52d1efe18d' | |
); | |
$WUAServiceSelected = @( | |
'Auto', | |
'WSUS', | |
'Windows Update', | |
'Microsoft Update' | |
); | |
$NumUpdatesDownloaded = 0; | |
$NumUpdatesFound = 0; | |
$NumUpdatesInstalled = 0; | |
$RebootRequired = $False; | |
$RebootPending = (New-Object -ComObject 'Microsoft.Update.SystemInfo').RebootRequired; | |
# Start logging | |
# ------------- | |
$timestamp = Get-Date -UFormat '+%Y%m%d%H%M%S'; | |
$logfile = "$($env:SystemRoot)\Temp\PoSh-WindowsUpdates_$($timestamp).log"; | |
Get-Date | Out-File -LiteralPath $logfile -Force; | |
<#------------------------------------------------------------------------------------------------------------------- | |
## WUA.ServiceManager | |
$UpdateServiceManager = New-Object -ComObject 'Microsoft.Update.ServiceManager'; | |
$UpdateServiceManager.ClientApplicationID = $sVar['ClientApplicationID']; | |
$UpdateServiceManager.SetOption('AllowWarningUI', $false); | |
$smMicrosoftUpdate = ''; | |
$smMicrosoftUpdate = ( $UpdateServiceManager.Services | Where-Object { $_.ServiceID -eq $ServiceID[3] } ); | |
if ( [object]::Equals($null, $smMicrosoftUpdate) ) | |
{ | |
$UpdateServiceManager.AddService2($ServiceID[3], 7, '') | Out-Null; | |
$UpdateServiceManager.RegisterServiceWithAU($ServiceID[3]) | Out-Null; | |
} | |
elseif ( $smMicrosoftUpdate.IsRegisteredWithAU -ne $True ) | |
{ | |
$UpdateServiceManager.RegisterServiceWithAU($ServiceID[3]) | Out-Null; | |
}; | |
-------------------------------------------------------------------------------------------------------------------#> | |
## WUA.Session | |
$UpdateSession = New-Object -ComObject 'Microsoft.Update.Session'; | |
$UpdateSession.ClientApplicationID = $sVar['ClientApplicationID']; | |
# Search for Updates | |
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher(); | |
$UpdateSearcher.ClientApplicationID = $sVar['ClientApplicationID']; | |
$UpdateSearcher.ServerSelection = $ServerSelection; | |
$UpdateSearcher.ServiceID = $ServiceID[$UpdateSearcher.ServerSelection]; | |
$SearchCriteria = "IsInstalled=0 and IsHidden=0 and AutoSelectOnWebSites=1 and Type='Software' or "; | |
$SearchCriteria += "IsInstalled=0 and IsHidden=0 and IsAssigned=1 and Type='Software' or "; | |
$SearchCriteria += "IsInstalled=0 and IsHidden=0 and DeploymentAction='Installation' or "; | |
$SearchCriteria += "IsInstalled=0 and IsHidden=0 and DeploymentAction='OptionalInstallation' or "; | |
$SearchCriteria += "IsInstalled=1 and IsHidden=0 and DeploymentAction='Installation' and RebootRequired=1"; | |
Try { | |
$UpdatesToDownload = $UpdateSearcher.Search($SearchCriteria); | |
} | |
Catch { | |
$_ | Out-File -LiteralPath $logfile -Append; | |
$UpdateSearcher.ServerSelection = '0'; | |
$UpdateSearcher.ServiceID = $ServiceID[$UpdateSearcher.ServerSelection]; | |
$SearchCriteria = "IsInstalled=0 and IsHidden=0 and AutoSelectOnWebSites=1 and Type='Software' or "; | |
$SearchCriteria += "IsInstalled=0 and IsHidden=0 and IsAssigned=1 and Type='Software'"; | |
$UpdatesToDownload = $UpdateSearcher.Search($SearchCriteria); | |
}; | |
$NumUpdatesFound = ($UpdatesToDownload.Updates | Measure-Object).Count; | |
If ( $NumUpdatesFound -gt 0 ){ | |
$UpdatesToDownload = $UpdatesToDownload.Updates; | |
$UpdatesToDownload | ForEach-Object { "[ Found ] $($_.Title)" | Out-File -LiteralPath $logfile -Append; }; | |
$UpdatesToInstall = New-Object -ComObject 'Microsoft.Update.UpdateColl'; | |
# Download Updates | |
foreach ( $Update in $UpdatesToDownload ) | |
{ | |
If ( $Update.InstallationBehavior.CanRequestUserInput ) { continue }; | |
$Update.AcceptEula(); | |
$objCollectionTmp = New-Object -ComObject 'Microsoft.Update.UpdateColl'; | |
$objCollectionTmp.Add($Update) | Out-Null; | |
$Downloader = $UpdateSession.CreateUpdateDownloader(); | |
$Downloader.Updates = $objCollectionTmp; | |
Try { | |
"[ Download ] $($Update.Title) -> " | Out-File -LiteralPath $logfile -Append -NoNewline; | |
$DownloadResult = $Downloader.Download(); | |
} | |
Catch { | |
"ERROR." | Out-File -LiteralPath $logfile -Append; | |
continue | |
}; | |
If ( ($DownloadResult.ResultCode -eq 2) -or ($Update.IsDownloaded) ) { | |
"success." | Out-File -LiteralPath $logfile -Append; | |
$UpdatesToInstall.Add($Update) | Out-Null; | |
$NumUpdatesDownloaded++; | |
} | |
Else { | |
"failure." | Out-File -LiteralPath $logfile -Append; | |
}; | |
}; | |
# Install Updates | |
foreach ( $Update in $UpdatesToInstall ) | |
{ | |
$objCollectionTmp = New-Object -ComObject 'Microsoft.Update.UpdateColl'; | |
$objCollectionTmp.Add($Update) | Out-Null; | |
$Installer = $UpdateSession.CreateUpdateInstaller(); | |
$Installer.Updates = $objCollectionTmp; | |
Try { | |
"[ Install ] $($Update.Title) -> " | Out-File -LiteralPath $logfile -Append -NoNewline; | |
$InstallResult = $Installer.Install(); | |
} | |
Catch { | |
"failure." | Out-File -LiteralPath $logfile -Append; | |
continue | |
}; | |
If ( $InstallResult.ResultCode -eq 2 ){ | |
"success." | Out-File -LiteralPath $logfile -Append; | |
$NumUpdatesInstalled++; | |
} | |
Else { | |
"failure." | Out-File -LiteralPath $logfile -Append; | |
}; | |
If ( $RebootRequired -ne $True ){ | |
$RebootRequired = $InstallResult.RebootRequired; | |
}; | |
}; | |
}; | |
"Update Service selected: $($WUAServiceSelected[$($UpdateSearcher.ServerSelection)])" | Out-File -LiteralPath $logfile -Append; | |
"Updates found: $NumUpdatesFound" | Out-File -LiteralPath $logfile -Append; | |
"Updates downloaded: $NumUpdatesDownloaded" | Out-File -LiteralPath $logfile -Append; | |
"Updates installed: $NumUpdatesInstalled" | Out-File -LiteralPath $logfile -Append; | |
"Reboot pending: $RebootPending" | Out-File -LiteralPath $logfile -Append; | |
"Reboot required: $RebootRequired" | Out-File -LiteralPath $logfile -Append; | |
Get-Date | Out-File -LiteralPath $logfile -Append; | |
if ($RebootAfterInstall -and ($RebootRequired -or $RebootPending)) { Restart-Computer -Force } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment