How to check windows server last update time with Powershell script

I wrote a simple Powershell script to check windows last successfully update time. And also I calculate the number of days after the last successful update. You can use it to monitor your windows servers.

This script will display  if  host get any  update  in 44  days. So  also will display  host  last  successfully  update time   with number  of days.

#You can run script  with to command  I added below on  batch script
#powershell.exe -ExecutionPolicy RemoteSigned -Command "& {C:\Windows_Patch.ps1}"

$now = Get-Date -format d
$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$out = $Searcher.QueryHistory(0, 1) | Select-Object Date | findstr -i ':' | %{ $_.Split(" ",[StringSplitOptions]"RemoveEmptyEntries")[0] }
$days = New-TimeSpan -Start $out -End $now
##Also change number  of days  as  you wish.
if (($days.Days -lt 44))
{
Write-Host "Yes - $out - $days"
}
else
{
Write-Host "No - $out - $days"
}

 

I'm a IT Infrastructure and Operations Architect with extensive experience and administration skills and works for Turk Telekom. I provide hardware and software support for the IT Infrastructure and Operations tasks.

205 Total Posts
Follow Me

Related Post