Waiting for Running Windows Update Scan Jobs

I’ve seen numerous places on the internet where it is suggested to do something like:

Start-Sleep -s 300

after running an extra Windows Update scan job in a task sequence (this is to work around issues where Windows Updates may still be available on a newly imaged computer which is caused by cached scan results).

The solution to the cached scan job issue is to run this powershell command:

Invoke-Wmi Method -Class SMS_Client -Name TriggerSchedule -ArgumentList (,"{00000000-0000-0000-0000-000000000113}") -Namespace root\ccm 

I find that a better way to wait for the scan job to complete is to actually look for it:

Do{Start-Sleep -s 5}While(Get-WmiObject -Class CCM_ScanJobInstance -Namespace root\ccm\scanagent)

This essentially sleeps for 5 seconds, then checks if the scan job is still running….if it is, sleep for another 5 seconds….and keep repeating.  This is beneficial as depending on how many software changes were made on your computer, the scan job may take several seconds to several minutes to run.


2 thoughts on “Waiting for Running Windows Update Scan Jobs

  1. Russ November 16, 2014 / 2:40 pm

    Don’t you want it to do while false instead of true? If you run it the way you have it currently, if the scan exists it’ll never break out of the loop. ie: Do{Start-Sleep -s 5}While(!(Get-WmiObject -Class CCM_ScanJobInstance -Namespace root\ccm\scanagent))

  2. mdedeboer November 19, 2014 / 9:03 am

    The intent of the script is to check and wait until the scan job is finished. I have only every seen one instance of a scan job….so once the 1 scan job finishes we’ll break out of the loop. If you are simply checking if the job kicked off, and don’t care about when the job finishes then I would use false instead of true. This way you are only waiting for however long the scan job executes for rather than for 300 seconds or whatever arbitrary number you choose.

    I hope this helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s