{"id":130,"date":"2017-09-07T10:13:58","date_gmt":"2017-09-07T09:13:58","guid":{"rendered":"http:\/\/brgeek.com.br\/wordpress\/?p=130"},"modified":"2017-09-07T10:15:38","modified_gmt":"2017-09-07T09:15:38","slug":"download-and-install-windows-patches-with-powershell","status":"publish","type":"post","link":"http:\/\/brgeek.com.br\/wordpress\/2017\/09\/07\/download-and-install-windows-patches-with-powershell\/","title":{"rendered":"Download and Install Windows Patches with Powershell"},"content":{"rendered":"<p>ou can automate and execute Windows Update with PowerShell script.<\/p>\n<p>This is a simple example script to install and reboot the server after patching.<\/p>\n<p>Feel free to change the last line of the code to not reboot the server after completion if that&#8217;s necessary.<\/p>\n<p>If you have Powershell version 5 would be easier.<\/p>\n<p>1) The first thing you need to do is confirm the version of PowerShell you have:<br \/>\n<code class=\"\" data-line=\"\">$PSVersionTable.PSVersion<\/code><\/p>\n<p>If version 5 or above, confirm you are running PowerShell as administrator and continue with:<br \/>\n<code class=\"\" data-line=\"\">Install-Module PSWindowsUpdate<br \/>\nGet-Command \u2013module PSWindowsUpdate<\/code><\/p>\n<p>Then you will need to register to use the Microsoft Update Service, not just the default Windows Update Service.<br \/>\n<code class=\"\" data-line=\"\">Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d<\/code><\/p>\n<p>Then run:<br \/>\n<code class=\"\" data-line=\"\">Get-WUInstall \u2013MicrosoftUpdate \u2013AcceptAll \u2013AutoReboot<\/code><\/p>\n<p>Below is the script for other versions other them 5 which is above.<br \/>\n================================================================================<\/p>\n<p><code class=\"\" data-line=\"\"># Download and Install Windows Patches with Reboot<br \/>\n# Script version 1.0<br \/>\n# Date : 08\/25\/2016<br \/>\n# Michael Goulart <\/p>\n<p>$waar = 1 -eq 1<br \/>\n$Session = New-Object -com &quot;Microsoft.Update.Session&quot;<br \/>\n$Search = $Session.CreateUpdateSearcher()<br \/>\n$SearchResults = $Search.Search(&quot;IsInstalled=0 and IsHidden=0&quot;)<br \/>\n$DownloadCollection = New-Object -com &quot;Microsoft.Update.UpdateColl&quot;<br \/>\n$SearchResults.Updates | ForEach-Object {<br \/>\n    if ($_.InstallationBehavior.CanRequestUserInput -ne $waar) {<br \/>\n        $DownloadCollection.Add($_) | Out-Null<br \/>\n        }<br \/>\n    }<br \/>\n if ($($SearchResults.Updates.Count -gt 0)) {<br \/>\n    $Downloader = $Session.CreateUpdateDownloader()<br \/>\n    $Downloader.Updates = $DownloadCollection<br \/>\n    $Downloader.Download()<br \/>\n} <\/p>\n<p>$objServiceManager = New-Object -ComObject &quot;Microsoft.Update.ServiceManager&quot;<br \/>\n$objSession = New-Object -ComObject &quot;Microsoft.Update.Session&quot;<br \/>\n$objSearcher = $objSession.CreateUpdateSearcher()<br \/>\nForeach ($objService in $objServiceManager.Services){<br \/>\n    if($ServiceID){<br \/>\n        if($objService.ServiceID -eq $ServiceID){<br \/>\n            $objSearcher.ServiceID = $ServiceID<br \/>\n            $objSearcher.ServerSelection = 3<br \/>\n            $serviceName = $objService.Name<br \/>\n        }<br \/>\n    }else{<br \/>\n        if($objService.IsDefaultAUService -eq $True){<br \/>\n            $serviceName = $objService.Name<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n$objCollection = New-Object -ComObject &quot;Microsoft.Update.UpdateColl&quot;<br \/>\n$objResults = $objSearcher.Search(&quot;IsInstalled=0&quot;)<br \/>\n$FoundUpdatesToDownload = $objResults.Updates.count<br \/>\nif ($objResults.Updates.count -gt 0){<br \/>\n    foreach($Update in $objResults.Updates){<br \/>\n        if($Update.EulaAccepted -eq 0 ){<br \/>\n            $Update.AcceptEula()<br \/>\n        }<br \/>\n        if($Update.isdownloaded -eq 0){<br \/>\n            $objCollectionTmp = New-Object -ComObject &quot;Microsoft.Update.UpdateColl&quot;<br \/>\n            $objCollectionTmp.Add($Update) | out-null<br \/>\n            $Downloader = $objSession.CreateUpdateDownloader()<br \/>\n            $Downloader.Updates = $objCollectionTmp<br \/>\n        }<br \/>\n    }<br \/>\n    $objCollectionTmp = New-Object -ComObject &quot;Microsoft.Update.UpdateColl&quot;<br \/>\n    foreach($Update in $objResults.Updates){<br \/>\n            $objCollectionTmp.Add($Update) | out-null<br \/>\n    }<br \/>\n    $objInstaller = $objSession.CreateUpdateInstaller()<br \/>\n    $objInstaller.Updates = $objCollectionTmp<br \/>\n    $InstallResult = $objInstaller.Install()<br \/>\n} <\/p>\n<p>shutdown -r -t 30 -c &quot;Restart after windows update&quot; <\/code><\/p>\n<p>GitHub<br \/>\nhttps:\/\/github.com\/mgoular\/DonwloadInstallPatches<\/p>\n<p>Technet<br \/>\nhttps:\/\/gallery.technet.microsoft.com\/scriptcenter\/and-Install-Patches-with-831d7f60<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ou can automate and execute Windows Update with PowerShell script. This is a simple example script to install and reboot the server after patching. Feel free to change the last line of the code to not reboot the server after completion if that&#8217;s necessary. If you have Powershell version 5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":131,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[6,7],"class_list":["post-130","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-powershell","tag-windows-updates"],"jetpack_featured_media_url":"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/WinUpdate.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":3,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":134,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/130\/revisions\/134"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media\/131"}],"wp:attachment":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}