FeaturedPower ShellScripts

List Available Patches (windows update) – Remote machine

Use this script to check if a remote machine has any new windows update available

 

<#
.SYNOPSIS
This script will list all available windows updates
.PARAMETER URL
User the Computer parameter to specify the Computer to remotely list  windows updates .
#>

function install-OSUpdate {
        [CmdletBinding()]

        param (

        [parameter(Mandatory=$true,Position=1)]
        [string[]]$computer


        )

                                                                                                                                                                                                                        ForEach ($c in $computer){

    
    
        #install pswindows updates module on remote machine
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        $nugetinstall = invoke-command -ComputerName $c -ScriptBlock {Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force}

        invoke-command -ComputerName $c -ScriptBlock {
            [Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
            install-module pswindowsupdate -force
        }

        invoke-command -ComputerName $c -ScriptBlock {Import-Module PSWindowsUpdate -force}

        Do{
            #Reset Timeouts
            $connectiontimeout = 0
            $updatetimeout = 0
        
            #starts up a remote powershell session to the computer
            do{
                $session = New-PSSession -ComputerName $c
                "reconnecting remotely to $c"
                sleep -seconds 10
                $connectiontimeout++
            } until ($session.state -match "Opened" -or $connectiontimeout -ge 10)

            #retrieves a list of available updates

            "Checking for new updates available on $c"

            $updates = invoke-command -session $session -scriptblock {Get-wulist -MicrosoftUpdate -verbose}

            #counts how many updates are available

            $updatenumber = ($updates.kb).count
            write-host "$updatenumber updates available"

            #if there are available updates proceed with installing the updates and then reboot the remote machine

            
   

        }until($updates)

    

        "Windows is now up to date on $c"

        }


}

 

Show More

Related Articles

Back to top button