FeaturedPower ShellSCCMScripts

check Single Patch KB ID installed on Remote Machine

Use this script to check if a single patch kb id installed on remote server

use single line of command

$lastpatch = Get-WmiObject  Win32_Quickfixengineering | select @{Name="InstalledOn";Expression={$_.InstalledOn -as [datetime]}} | Sort-Object -Property Installedon | select-object -property installedon -last 1

or use this full script to get full details with services

# save Information pref
$Old_I_Pref = $InformationPreference
# enable Information output
$InformationPreference = 'Continue'

$PatchList = @(
    'KB4535680' # Security Update
    'KB4589210' # Service Pack
    )

$SystemList = @(
    'chn6vmdp01'
    'isr1vmsccm1'
    'bel1vmsccm1'
    )

    foreach ($SL_Item in $SystemList){

     Write-Information '    Getting HotFix info ...'
           Get-HotFix -ComputerName $SL_Item -Id $PatchList | Sort-Object -Property HotFixID
           
}

$Online = 'Online'
$No_NetResponse = '==  Not Reachable =='
$No_WSManResponse = '---- No WSMan Response ----'

$Results = foreach ($SL_Item in $SystemList)
    {
    Write-Information ''
    $WSMan_OK = $False
    Write-Information ('Testing connection to {0} ...' -f $SL_Item)
    if (Test-Connection -ComputerName $SL_Item -Count 1 -Quiet)
        {
        $NetConnection_OK = $True
        Write-Information '    Net connecion OK.'
        Write-Information '    Testing WSMan connection ...'
        $WSMan_OK = [bool](Test-WSMan -ComputerName $SL_Item -ErrorAction SilentlyContinue)
        if ($WSMan_OK)
            {
            Write-Information '    WSMan connection OK.'
            Write-Information '    Getting HostName ...'
            $GCI_CS_Params = @{
                ClassName = 'CIM_ComputerSystem'
                ComputerName = $SL_Item
                }
            $HostName = (Get-CimInstance @GCI_CS_Params).Name
            Write-Information ('        Hostname = [ {0} ]' -f $HostName)
            Write-Information '    Getting LastBootUpTime info ...'
            $GCI_OS_Params = @{
                ClassName = 'Win32_OperatingSystem'
                ComputerName = $SL_Item
                }
            $LastBootUpTime = (Get-CimInstance @GCI_OS_Params).LastBootUpTime
            Write-Information ('        LastBootUpTime = [ {0} ]' -f $LastBootUpTime)

            Write-Information '    Getting HotFix info ...'
            $HotFixInfo = Get-HotFix -ComputerName $SL_Item -Id $PatchList |
                Sort-Object -Property HotFixID
            Write-Information ('        Found = {0} of {1} hotfixes.' -f $HotFixInfo.Count, $PatchList.Count)
            }
            else
            {
            Write-Warning '        WSMan connection failed.'
            }
        }
        else
        {
        $NetConnection_OK = $False
        Write-Warning '        Net connection failed.'
        }

    # the "(FalseThing, TrueThing)[BooleanValue]" stuff below is a kinda-sorta ternary expression
    # it's a short version of "if (Bool is True){TrueThing}else{FalseThing}"
    [PSCustomObject]@{
        InputSystemName = $SL_Item
        OnlineStatus = ($No_NetResponse, $Online)[$NetConnection_OK]
        HostName = ($No_WSManResponse, $HostName)[$WSMan_OK]
        LastBootUpTime = ($No_WSManResponse, $LastBootUpTime)[$WSMan_OK]
        HotFixList = ($No_WSManResponse,($HotFixInfo.HotFixID -join '; '))[$WSMan_OK]
        }
    }

$Results

# restore previous Information pref
$InformationPreference = $Old_I_Pref






########################################################################################################
$lastpatch = Get-WmiObject  Win32_Quickfixengineering | select @{Name="InstalledOn";Expression={$_.InstalledOn -as [datetime]}} | Sort-Object -Property Installedon | select-object -property installedon -last 1

 

Show More

Related Articles

Back to top button