SHOW:
|
|
- or go back to the newest paste.
| 1 | ###########################################################################" | |
| 2 | # | |
| 3 | # NAME: WindowsUpdate.ps1 | |
| 4 | # Script Version 1.0 | |
| 5 | # | |
| 6 | # AUTHOR: Alin Dumenica | |
| 7 | # EMAIL: [email protected] | |
| 8 | # | |
| 9 | # COMMENT: Script to download and install updates from Windows Update/WSUS. Reporting and rebooting may be customized. | |
| 10 | # For more details, see the following blog-post: | |
| 11 | # http://windows-scripting.co.cc/tutorials/install-windows-updates-windows-powershell | |
| 12 | # | |
| 13 | # You have a royalty-free right to use, modify, reproduce, and | |
| 14 | # distribute this script file in any way you find useful, provided that | |
| 15 | # you agree that the creator, owner above has no warranty, obligations, | |
| 16 | # or liability for such use. | |
| 17 | # | |
| 18 | ###########################################################################" | |
| 19 | ||
| 20 | #Requires -Version 2.0 | |
| 21 | ||
| 22 | #Variables to customize | |
| 23 | $sysinfo = Get-WmiObject -Class Win32_ComputerSystem | |
| 24 | $fqdn = “{0}.{1}” -f $sysinfo.Name, $sysinfo.Domain
| |
| 25 | $compname = $sysinfo.Name | |
| 26 | $EmailReport = $true | |
| 27 | $FileReport = $false | |
| 28 | $To = "[email protected]" | |
| 29 | $From = $compname + "@domain.com" | |
| 30 | $SMTPServer = "your.mail.host.domain.com" | |
| 31 | $FileReportPath = "\\server\share\" | |
| 32 | $AutoRestart = $true | |
| 33 | $AutoRestartIfPending = $true | |
| 34 | ||
| 35 | $Path = $FileReportPath + "$env:ComputerName" + "_" + (Get-Date -Format dd-MM-yyyy_HH-mm).ToString() + ".html" | |
| 36 | ||
| 37 | #Testing if there are any pending reboots from earlier Windows Update sessions | |
| 38 | if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"){
| |
| 39 | ||
| 40 | #Report to e-mail if enabled | |
| 41 | if ($EmailReport -eq $true) {
| |
| 42 | $pendingboot = @{$false="was pending for a restart from an earlier Windows Update session. Due to the reboot preferences in the script, a reboot was not initiated."; $true="was restarted due to a pending restart from an earlier Windows Update session."}
| |
| 43 | $status = $pendingboot[$AutoRestartIfPending] | |
| 44 | $messageParameters = @{
| |
| 45 | Subject = "Windows Update report for $fqdn - $((Get-Date).ToShortDateString())" | |
| 46 | Body = "Invoke-WindowsUpdate was run on $fqdn, and the server $status `nPlease run Invoke-WindowsUpdate again when the server is rebooted." | |
| 47 | from = $From | |
| 48 | To = $To | |
| 49 | SmtpServer = $SMTPServer | |
| 50 | } | |
| 51 | Send-MailMessage @messageParameters -BodyAsHtml | |
| 52 | ||
| 53 | #Report to file if enabled | |
| 54 | if ($FileReport -eq $true) {
| |
| 55 | "Invoke-WindowsUpdate was run on $env:ComputerName, and the server $status `nPlease run Invoke-WindowsUpdate again when the server is rebooted." | Out-File -FilePath $path | |
| 56 | } | |
| 57 | ||
| 58 | #Reboot if autorestart for pending updates is enabled | |
| 59 | if ($AutoRestartIfPending) {shutdown.exe /t 0 /r } }
| |
| 60 | exit | |
| 61 | ||
| 62 | } | |
| 63 | ||
| 64 | #Checking for available updates | |
| 65 | $updateSession = new-object -com "Microsoft.Update.Session" | |
| 66 | write-progress -Activity "Updating" -Status "Checking available updates" | |
| 67 | $updates=$updateSession.CreateupdateSearcher().Search($criteria).Updates | |
| 68 | $downloader = $updateSession.CreateUpdateDownloader() | |
| 69 | $downloader.Updates = $Updates | |
| 70 | ||
| 71 | #If no updates available, do nothing | |
| 72 | if ($downloader.Updates.Count -eq "0") {
| |
| 73 | ||
| 74 | #Report to e-mail if enabled | |
| 75 | if ($EmailReport -eq $true) {
| |
| 76 | $messageParameters = @{
| |
| 77 | Subject = "Windows Update report for $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())" | |
| 78 | Body = "Invoke-WindowsUpdate was run on $env:ComputerName, but no new updates were found. Please try again later." | |
| 79 | from = $From | |
| 80 | To = $To | |
| 81 | SmtpServer = $SMTPServer | |
| 82 | } | |
| 83 | Send-MailMessage @messageParameters -BodyAsHtml | |
| 84 | } | |
| 85 | ||
| 86 | #Report to file if enabled | |
| 87 | if ($FileReport -eq $true) {
| |
| 88 | "Invoke-WindowsUpdate was run on $env:ComputerName, but no new updates were found. Please try again later." | Out-File -FilePath $Path | |
| 89 | } | |
| 90 | ||
| 91 | } | |
| 92 | else | |
| 93 | {
| |
| 94 | #If updates are available, download and install | |
| 95 | write-progress -Activity 'Updating' -Status "Downloading $($downloader.Updates.count) updates" | |
| 96 | ||
| 97 | $Criteria="IsInstalled=0 and Type='Software'" | |
| 98 | $resultcode= @{0="Not Started"; 1="In Progress"; 2="Succeeded"; 3="Succeeded With Errors"; 4="Failed" ; 5="Aborted" }
| |
| 99 | $Result= $downloader.Download() | |
| 100 | ||
| 101 | if (($Result.Hresult -eq 0) –and (($result.resultCode –eq 2) -or ($result.resultCode –eq 3)) ) {
| |
| 102 | $updatesToInstall = New-object -com "Microsoft.Update.UpdateColl" | |
| 103 | $Updates | where {$_.isdownloaded} | foreach-Object {$updatesToInstall.Add($_) | out-null
| |
| 104 | } | |
| 105 | ||
| 106 | $installer = $updateSession.CreateUpdateInstaller() | |
| 107 | $installer.Updates = $updatesToInstall | |
| 108 | ||
| 109 | write-progress -Activity 'Updating' -Status "Installing $($Installer.Updates.count) updates" | |
| 110 | ||
| 111 | $installationResult = $installer.Install() | |
| 112 | $Global:counter=-1 | |
| 113 | ||
| 114 | $Report = $installer.updates | | |
| 115 | Select-Object -property Title,EulaAccepted,@{Name='Result';expression={$ResultCode[$installationResult.GetUpdateResult($Global:Counter++).resultCode ] }},@{Name='Reboot required';expression={$installationResult.GetUpdateResult($Global:Counter++).RebootRequired }} |
| |
| 116 | ConvertTo-Html | |
| 117 | ||
| 118 | #Report to e-mail if enabled | |
| 119 | if ($EmailReport -eq $true) {
| |
| 120 | $messageParameters = @{
| |
| 121 | Subject = "Windows Update report for $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())" | |
| 122 | Body = $Report | Out-String | |
| 123 | from = $From | |
| 124 | To = $To | |
| 125 | SmtpServer = $SMTPServer | |
| 126 | } | |
| 127 | Send-MailMessage @messageParameters -BodyAsHtml | |
| 128 | } | |
| 129 | ||
| 130 | #Report to file if enabled | |
| 131 | if ($FileReport -eq $true) {
| |
| 132 | $Report | Out-File -FilePath $path | |
| 133 | } | |
| 134 | ||
| 135 | #Reboot if autorestart is enabled and one or more updates are requiring a reboot | |
| 136 | if ($autoRestart -and $installationResult.rebootRequired) { shutdown.exe /t 0 /r }
| |
| 137 | } | |
| 138 | } |