param( [string]$Criteria = "IsInstalled=0 and Type='Software' and IsHidden=0", [switch]$RebootToComplete ) 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 "-Criteria: $Criteria" #Write-Host "-RebootToComplete: $RebootToComplete" #Write-Host "" Write-Host "### Running Windows Update (for indivisual 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 ($i -eq 1) { $response = Read-Host "1 update found. Do you want to install it ?(Y/N)" } else { $response = Read-Host "Enter the number of the update you wish to install (Type C to abort)" } if ($response -match '^[Yy]$') { } elseif ($response -match '^[Cc]$') { Write-Host "Aborted." exit 0 } elseif ($response -as [int]) { if ($response -gt $i) { Write-Host "Aborted." exit 0 } } $targetupdatetitle = $searchResult.Updates.item($response - 1).Title if ( $downloadReq ) { Write-Host "Creating collection of updates to download..." $updatesToDownload = new-object -com "Microsoft.Update.UpdateColl" foreach ($update in $searchResult.Updates){ if ($update.Title -eq $targetupdatetitle) { $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." } $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 "One update installed successfully." } else { Write-Host "Update could not installed." } } if ( $installationResult.RebootRequired ) { Write-Host "One update are requiring reboot." } 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