A computer engineer poking at your cerebral cortex.

Windows Updates Through Power Shell

I have been a linux admin for the last decade or so and haven’t used windows much. Now I wan’t to automate windows updates as I have done them on linux.

If you know anything about linux I use ubuntu as my main operating system. So if you know about debian/ubuntu linux I’m trying to do this in windows: sudo apt-get update && sudo apt-get -y upgrade && sudo reboot.

As you can imagine this is a littler harder in windows and only works with windows 8 and up. It starts with using modules in powershell. I heard in a more recent version of powershell you won’t have to download this module anymore.

For now its built into the script and you agree to their terms: https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc

So this is what I wrote in powershell. Use at your own RISK :) The server won’t reboot if there are no updates.

# Copyright (c) 2014, Dan Sheffner http://digisoftinc.org/                                           
# All rights reserved.                                                           
#                                                                                
# Permission is hereby granted, free of charge, to any person obtaining a           
# copy of this software and associated documentation files (the                     
# "Software"), to deal in the Software without restriction, including               
# without limitation the rights to use, copy, modify, merge, publish, dis-          
# tribute, sublicense, and/or sell copies of the Software, and to permit            
# persons to whom the Software is furnished to do so, subject to the fol-           
# lowing conditions:                                                             
#                                                                                
# The above copyright notice and this permission notice shall be included           
# in all copies or substantial portions of the Software.                            
#                                                                                
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS           
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-          
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT            
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,             
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,                
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS            
# IN THE SOFTWARE.                            

# sudo apt-get update && sudo apt-get upgrade && sudo reboot equivalent
# for windows
#
# You agree to the terms of this file by using this script
# https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc

# Where is the module
$modulesPath = "C:\Windows\system32\WindowsPowerShell\v1.0\Modules\"
$moduleName = "PSWindowsUpdate.zip"
$url = "https://s3.amazonaws.com/smtc/" + $moduleName
$total = $modulesPath + $moduleName

function unzip()
{
    $shell = new-object -com shell.application
    $zip = $shell.NameSpace($total)
    foreach($item in $zip.items())
    {
        $shell.Namespace($modulesPath).copyhere($item)
    }
}

Function cleanUp()
{
    $strFileName = $modulesPath + "PSWindowsUpdate.zip"
    If (Test-Path $strFileName)
    {
	    Remove-Item $strFileName
    }

    $strFolderName = $modulesPath + "PSWindowsUpdate\"
    If (Test-Path $strFolderName)
    {
	    Remove-Item $strFolderName -Recurse -Confirm:$false
    }
}

Function getWinUpdateFiles()
{
    $webclient = New-Object System.Net.WebClient
    $webclient.DownloadFile($url,$total)
    unzip
}

Function ActivateWindowsUpdateServices()
{
    Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d -Confirm:$false
    Import-Module PSWindowsUpdate
}

function runUpdates()
{
    Get-WUInstall -MicrosoftUpdate -AutoReboot -IgnoreUserInput -Verbose -Confirm:$false
}


Write-Host "windowsUpdates.ps1 started..."

# call our functions
cleanUp
getWinUpdateFiles
ActivateWindowsUpdateServices
runUpdates