param( [switch]$Automate, [string]$Criteria = "IsInstalled=0 and Type='Software' and IsHidden=0", [switch]$NoDownload, [switch]$NoInstall, [switch]$RebootToComplete ) $ScriptforSigleUpdatePath = ".\WUA_SearchDownloadInstallS.ps1" function InstallationResultToText($result) { switch ($result) { 2 { "Succeeded" } 3 { "Succeeded with errors" } 4 { "Failed" } 5 { "Cancelled" } default { "Unexpected ($result)" } } } Write-Host "### Command line parameters you specify. ###" Write-Host "-Automate: $Automate" Write-Host "-Criteria: $Criteria" Write-Host "-NoDownload: $NoDownload" Write-Host "-NoInstall: $NoInstall" Write-Host "-RebootToComplete: $RebootToComplete" Write-Host "" Write-Host "### Running Windows Update ###" Write-Host "Searching for updates..." Write-Host "" $updateSession = new-object -com "Microsoft.Update.Session" $updateSearcher = $updateSession.CreateupdateSearcher() $searchResult = $updateSearcher.Search($criteria) Write-Host "List of applicable items on the machine:" if ($searchResult.Updates.Count -eq 0) { Write-Host "There are no applicable updates." exit 0 } else { $downloadReq = $False $i = 0 foreach ($update in $searchResult.Updates){ $i++ if ( $update.IsDownloaded ) { Write-Host $i">" $update.Title "(downloaded)" } else { $downloadReq = $true Write-Host $i">" $update.Title "(not downloaded)" } } } Write-Host "" if (-not $Automate ) { $response = Read-Host "Next step: download all updates(Y) or download/install single update(S). May I continue? (Y/N/S)" if ($response -match '^[Yy]$') { Write-Host "Proceed to the next step: download all updates." } elseif ($response -match '^[Ss]$') { Write-Host "Proceed to the next step: download/install single update." If (Test-Path $ScriptforSigleUpdatePath) { . $ScriptforSigleUpdatePath exit $LASTEXITCODE } else { Write-Host "$ScriptforSigleUpdatePath not found." exit 0 } } else { Write-Host "Aborted." exit 0 } } else { Write-Host "Proceed to the net step: download updates. (-Automate)" } if ( $NoDownload ) { Write-Host "Skip downloads. (-NoDownload)" } elseif ( $downloadReq ) { Write-Host "Creating collection of updates to download..." $updatesToDownload = new-object -com "Microsoft.Update.UpdateColl" foreach ($update in $searchResult.Updates){ $updatesToDownload.Add($update) | out-null } Write-Host "Downloading updates..." $downloader = $updateSession.CreateUpdateDownloader() $downloader.Updates = $updatesToDownload $downloader.Download() Write-Host "List of downloaded updates:" $i = 0 foreach ($update in $searchResult.Updates){ $i++ if ( $update.IsDownloaded ) { Write-Host $i">" $update.Title "(downloaded)" } else { Write-Host $i">" $update.Title "(not downloaded)" } } } else { Write-Host "All updates are already downloaded." } Write-Host "" if (-not $Automate ) { $response = Read-Host "Next step: install updates. May I continue? (Y/N)" if ($response -match '^[Yy]$') { Write-Host "Proceed to the next step." } else { Write-Host "Aborted." exit 0 } } else { Write-Host "Proceed to the net step: install updates. (-Automate)" } if ( $NoInstall ) { Write-Host "Skip installation. (-NoInstall)" exit 0 } $updatesToInstall = new-object -com "Microsoft.Update.UpdateColl" Write-Host "" Write-Host "Creating collection of downloaded updates to install..." foreach ($update in $searchResult.Updates){ if ( $update.IsDownloaded ) { $updatesToInstall.Add($update) | out-null } } $returnValue = 1 if ( $updatesToInstall.Count -eq 0 ) { Write-Host "Not ready for installation." } else { Write-Host "Installing" $updatesToInstall.Count "updates..." $installer = $updateSession.CreateUpdateInstaller() $installer.Updates = $updatesToInstall $installationResult = $installer.Install() if ( $installationResult.ResultCode -eq 2 ) { Write-Host "All updates installed successfully." } else { Write-Host "Some updates could not installed." } } # $returnValue = $installationResult.ResultCode if ( $installationResult.RebootRequired ) { Write-Host "One or more updates are requiring reboot." # $returnValue = $returnValue + 100 } else { Write-Host "Finished. Reboot are not required." } Write-Host "" Write-Host "Listing of updates installed and indivisual installation results:" $i = 0 foreach ($update in $updatesToInstall){ $result = $installationResult.GetUpdateResult($i) $i++ Write-Host $i">" $update.Title ":" $(InstallationResultToText $result.ResultCode) ", HRESULT:" $($result.HResult) } if ($installationResult.RebootRequired) { if ($RebootToComplete) { Write-Host "After 60 seconds, the system will initiate a restart automatically. To abort , press Ctrl+C. (-RebootToComplete)" for ($i = 60; $i -gt 0; $i--) { Write-Host "." -NoNewLine Start-Sleep -Seconds 1 } restart-Computer -Force } elseif (-not $Automate) { $response = Read-Host "Reboot is pending. Would you like to reboot? (Y/N)" if ($response -match '^[Yy]$') { Write-Host "Reboot now!..." sleep -Seconds 3 restart-Computer -Force } else { Write-Host "You need to reboot this device later." } } } Exit $returnValue