Solved Checking last update install date within 30 days

January 18, 2018 at 08:29:38
Specs: Windows 7
I still new to writing my own scripts and I'm not sure how to progress...

This returns the full date of my most recent patch install:

get-hotfix | select-object -expandproperty installedon | sort-object -descending | select-object -first 1

This returns the object (installedon) name and the date in a different format:

Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 1 | Select-Object -Property InstalledOn

What I would like to do now is check if this returned date is within the last 30 days and return a true or false.

message edited by croberts0


See More: Checking last update install date within 30 days

Report •

✔ Best Answer
January 25, 2018 at 09:50:35
Someone should probably mention the OP's using PowerShell. Also, I hope you'll forgive me for using WMI directly. It's what I learned, and I learned before Get-Hotfix existed. Get-Hotfix is just shorthand for mine anyways.
gwmi Win32_QuickFixEngineering |
 sort InstalledOn -desc |
 select -f 1 InstalledOn, @{n='Recent';e={ $_.InstalledOn -ge [datetime]::Today.AddDays(-30) }} |
 ft -a

How To Ask Questions The Smart Way



#1
January 18, 2018 at 23:12:30
please: a sample of the output date? I don't have "get-hotfix" on my win-7. Also check its output not in Unicode or else make sure you take that into account (for batch anyway).

message edited by nbrane


Report •

#2
January 23, 2018 at 04:14:16
get-hotfix | select-object -expandproperty installedon | sort-object -descending | select-object -first 1

Wednesday, January 10, 2018 12:00:00 AM

Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 1 | Select-Object -Property InstalledOn

InstalledOn
--------------
1/10/2018 12:00:00 AM


Report •

#3
January 24, 2018 at 12:46:54
@echo off & setlocal
:: first make a one-line vbscript to do the date math
if not exist dif.vbs (
>>dif.vbs echo wscript.echo datediff^(wscript.arguments^(0^), wscript.arguments^(1^), wscript.arguments^(2^)^)
)
for /f "skip=3 tokens=1" %%a in ('Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 1 | Select-Object -Property InstalledOn') do (
echo last patch: %%a
for /f %%b in ('cscript dif.vbs d %%a %date:~4,10%') do set dif=%%b
)
echo %dif% days ago
:: this should set your errorlevel to '1' if over 30, otherwise it's zero:
set xit=1
if %dif% leq 30 set xit=0
exit /b %xit%

Report •

Related Solutions

#4
January 25, 2018 at 09:50:35
✔ Best Answer
Someone should probably mention the OP's using PowerShell. Also, I hope you'll forgive me for using WMI directly. It's what I learned, and I learned before Get-Hotfix existed. Get-Hotfix is just shorthand for mine anyways.
gwmi Win32_QuickFixEngineering |
 sort InstalledOn -desc |
 select -f 1 InstalledOn, @{n='Recent';e={ $_.InstalledOn -ge [datetime]::Today.AddDays(-30) }} |
 ft -a

How To Ask Questions The Smart Way


Report •

Ask Question