This is a quick, simple post. The requirement is simple. Get a report from all the computers in the network reporting the last Windows update date, Windows update installed KB, and the Windows uptime.

The script gets the content from a file containing all the computer names as an array and saves the result on the user’s desired destination.

To use the script, use the following format.

MyScript.ps1 -FilePath C:\MyComputerlist.txt -SaveTo C:\SaveResult.csv

The result is a CSV file that

result

Here is the script. Feel free to use it, and let me know if you recommend any updates.

Param
    (
        # File Path 
        [Parameter(Mandatory=$true)]
        $FilePath,
        [parameter(mandatory=$true)]
        $SaveTo
    )
 
$Computers = Get-Content -Path $FilePath

$Result=@()

Foreach ($Computer in $Computers) {

    $ResultObject=[PSCustomObject]@{
        'Server Name'=''
        'Uptime'=''
        'Days Since Last Patch'=''
        'Last Update KB'=''
        'Last Patch Date & Time'=''
    }

    Try{

      Write-host "Processing $($computer)"

      $ResultObject.'Server Name' = $Computer

        # Get Uptime
        Write-host "Processing Uptime for $($computer)"
        $uptime = (Get-CimInstance -Class Win32_OperatingSystem -ComputerName $computer -ErrorAction Stop).LastBootUpTime
        $ResultObject.Uptime = ((Get-Date) - $uptime).days

        # Get Last Patch Date & Time
        Write-host "Processing Last patch for $($computer)"
        $lastPatch = Get-CimInstance -Class Win32_QuickFixEngineering -ComputerName $computer -ErrorAction Stop| Sort-Object -Property InstalledOn -Descending | Select-Object -First 1
        $ResultObject.'Last Patch Date & Time' = ($lastPatch.InstalledOn).Date.ToString("dd/MM/yyy")
        $ResultObject.'Last Update KB'=$lastPatch.HotFixID

        # Get Days Since Last Patch

        Write-host "Processing Last Days between patch for $($computer)"
        $Dateforpatch=(Get-Date) - $lastPatch.InstalledOn
        $ResultObject.'Days Since Last Patch' =($dateforpatch).Days
        $Result+=$ResultObject

        }

            Catch{
                Write-Host "Ops, Error for $($computer)"
                $Result+=$ResultObject
                $Error[0]

            }

}
$result
$result | Export-Csv $SaveTo -NoTypeInformation

I hope this script helps. There will be more scripts like that. Let me know if you have any ideas in the comment section below.

That’s it for today; take a look at the following post. You will love it.

PowerShell Module to get RDP Session Info

5/5 - (2 votes)