Windows Update: detect if important updates available

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Windows Update: detect if important updates available

10 Jan 2017, 18:51

I am looking for a way to detect if there are
important updates available on Windows 7, or on Windows generally.

I have had problems with various methods so far:
- FindWindow: works on Windows XP, doesn't work on Windows 7
- check system tray icons: fiddly, and the method varies depends on the Windows version
- ComObjCreate("Microsoft.Update.Session"): extremely slow or doesn't work
- Run, control /name Microsoft.WindowsUpdate: cannot be run as a hidden window

Btw on Windows XP I could check for Windows updates via:

Code: Select all

^q::
if (A_OSVersion = "WIN_XP")
if DllCall("FindWindow", "str", "wuauclt_icon", "uint", 0)
MsgBox NOTE: WINDOWS UPDATES AVAILABLE
Return
I used the Windows XP method on a backup script, that notified you of any updates,
at the point where it asked if you wanted to shutdown/restart after backup.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Windows Update: detect if important updates available

15 Jan 2017, 00:57

Well the current best solution I have is now:
- JEE_ExpGetSystrayItems uses Acc to list the names of the systray items.
- JEE_ExpUpdatesAvailable then checks for the text 'New updates are available'.
- (JEE_ExpGetTaskbarItems is a similar function to the systray one.)

I would still be interested in any other methods, maybe via objects or via a system dll call. But this solution is pretty good.

Code: Select all

;==================================================

;tested on Windows 7
JEE_ExpGetTaskbarItems(vSep="`n")
{
	vDHW := A_DetectHiddenWindows
	DetectHiddenWindows, On

	ControlGet, hWnd, Hwnd,, MSTaskListWClass1, ahk_class Shell_TrayWnd
	oAcc := Acc_Get("Object", "tool bar", 0, "ahk_id " hWnd)
	vOutput := ""
	Loop, % oAcc.accChildCount
		vOutput .= oAcc.accName(A_Index) vSep
	vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right

	DetectHiddenWindows, % vDHW
	return vOutput
}

;==================================================

;tested on Windows 7
JEE_ExpGetSystrayItems(vSep="`n")
{
	vDHW := A_DetectHiddenWindows
	DetectHiddenWindows, On

	ControlGet, hWnd, Hwnd,, ToolbarWindow321, ahk_class NotifyIconOverflowWindow
	oAcc := Acc_Get("Object", "tool bar", 0, "ahk_id " hWnd)
	vOutput := ""
	Loop, % oAcc.accChildCount
		vOutput .= oAcc.accName(A_Index) vSep
	vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right

	DetectHiddenWindows, % vDHW
	return vOutput
}

;==================================================

;tested on Windows 7
JEE_ExpUpdatesAvailable()
{
	vText := "`n" JEE_ExpGetSystrayItems("`n") "`n"
	return !!InStr(vText, "`nNew updates are available`n")
}

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Windows Update: detect if important updates available

28 Sep 2017, 04:17

Getting Windows Update information via Microsoft.Update.Session:

In the past this had taken about 6 minutes, but testing just now around half to one minute. I would be grateful if anyone has a quick method that simply checks if there are updates available e.g. that a little yellow icon will appear by the Start button.

Code: Select all

#SingleInstance force
ListLines, Off
#KeyHistory 0
Menu, Tray, Click, 1
#NoEnv
AutoTrim, Off
#UseHook
SetBatchLines, -1

hModule := DllCall("kernel32\LoadLibrary", Str,"wuapi.dll", Ptr)

oUpdateSession := ComObjCreate("Microsoft.Update.Session")
oUpdateSession.ClientApplicationID := "MSDN Sample Script"
oUpdateSearcher := oUpdateSession.CreateUpdateSearcher()

vTickCount1 := A_TickCount
oSearchResult := oUpdateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
;oSearchResult := oUpdateSearcher.Search("IsInstalled=0")
;oSearchResult := oUpdateSearcher.Search("")
vTickCount2 := A_TickCount

vUpdatesTotal := oSearchResult.Updates.Count

vOutput := ""
Loop, % vUpdatesTotal
{
	oUpdate := oSearchResult.Updates.Item(A_Index-1)
	try vSeverity := oUpdate.MsrcSeverity

	vIDs := ""
	Loop, % oUpdate.KBArticleIDs.Count
		vIDs .= oUpdate.KBArticleIDs(A_Index-1)

	vName := ""
	Loop, % oUpdate.Categories.Count
		vName .= oUpdate.Categories.Item(A_Index-1).Name ","
	vName := SubStr(vName, 1, -1)

	try vBrowseOnly := oUpdate.BrowseOnly
	try vType := oUpdate.Type
	try vIsInstalled := oUpdate.IsInstalled
	try vIsHidden := oUpdate.IsHidden
	vOutput .= (A_Index-1) "`t" vSeverity "`t" vName "`t" vBrowseOnly "`t" vType "`t" vIsInstalled "`t" vIsHidden "`t" oUpdate.Title "`r`n"

	;other properties:
	;MsgBox, % oUpdate.Deadline
	;MsgBox, % oUpdate.Description
	;MsgBox, % oUpdate.EulaText
	;MsgBox, % oUpdate.MaxDownloadSize
	;MsgBox, % oUpdate.SupportURL
}

vTickCount := (vTickCount2 - vTickCount1)
vTickCountMin := (vTickCount2 - vTickCount1) / 60000
vOutput .= "`r`nTOTAL: " vUpdatesTotal "`r`n"
vOutput .= "`r`nTICK COUNT (MS): " vTickCount "`r`n"
vOutput .= "`r`nTICK COUNT (MIN): " vTickCountMin "`r`n"

vPath = %A_Desktop%\z updates %A_Now%.txt
FileAppend, % vOutput, % "*" vPath, UTF-8
MsgBox, % vUpdatesTotal " [" vTickCountMin "]"
DllCall("kernel32\FreeLibrary", Ptr,hModule)
return
Links:
Searching, Downloading, and Installing Updates (Windows)
https://msdn.microsoft.com/en-us/library/aa387102
IUpdate interface (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Last edited by jeeswg on 28 Sep 2017, 05:19, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Windows Update: detect if important updates available

28 Sep 2017, 04:47

(New-Object -ComObject Microsoft.Update.AutoUpdate).DetectNow()
... stolen from [here]. Hope that helps :?: :!:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Windows Update: detect if important updates available

28 Sep 2017, 05:27

Thanks BoBo. From what I read that will start updates, this other line below brings up a little dialog.

Code: Select all

q::
;ComObjCreate("Microsoft.Update.AutoUpdate").DetectNow()
ComObjCreate("Microsoft.Update.AutoUpdate").ShowSettingsDialog()
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jollyjoe and 346 guests