Use vbs to determine whether a specific patch is installed

Source: Internet
Author: User
Tags new windows update

Q:
Hello, script expert! How can I use a script to determine whether a specific patch is installed?

-- GM

A:
Hello, GM. You know, this problem may have been accidentally thrown into the waste basket and pretended to have never been seen before. Why? Well, in the past, the only way we got information about patches, fast fixes, and other updates was to use the WMI Win32_QuickFixEngineering class. This is good, but for various reasons, Win32_QuickFixEngineering occasionally loses the installed patch. Worse, on Windows 2000 computers, Win32_QuickFixEngineering is sometimes on standby, no information is returned. (How to solve this problem? Of course, it is solved by installing patches .) In any case, it cannot be a good thing.

However, those days have passed. Now, thanks to the new Windows Update Service with significant improvements, it is easy to determine which updates have been installed on your computer and are not yet installed. For example, the following script tells us whether the patch Security Update for Windows XP (KB899587) has been installed on the computer ):

Set objSession = CreateObject ("Microsoft. Update. Session ")
Set objSearcher = objSession. CreateUpdateSearcher
Set objResults = objSearcher. Search ("Type = 'soft '")
Set colUpdates = objResults. Updates

For I = 0 to colUpdates. Count-1
If colUpdates. Item (I). Title = _
"Security Update for Windows XP (KB899587)" Then
If colUpdates. Item (I). IsInstalled <> 0 Then
Wscript. Echo "This update is installed ."
Wscript. Quit
Else
Wscript. Echo "This update is not installed ."
Wscript. Quit
End If
End If
Next

Wscript. Echo "This update is not installed ."

We will not describe each line of code in detail. The details of the Windows Update Service are somewhat beyond the scope of this column. If you want to know Windows Update (especially Microsoft. update. for more information, see the "script Story" column... We also need to manage Windows Update!

However, we will find that, despite the Search method, we do not actually Search for a specific update. Searching means to only search for the required items, and its accuracy is extremely low. So we cannot do that. Instead, we need to return all the updated sets, filter the entire set, and find the updates titled Security Update for Windows XP (KB899587. The final result is the same, but the process of achieving this final result is slightly different.

No, there is no difference. For what you want to know.

So what is the process we are using here? Well, first use the following four lines of code to retrieve the update set of the Local Computer:

Set objSession = CreateObject ("Microsoft. Update. Session ")
Set objSearcher = objSession. CreateUpdateSearcherSet
ObjResults = objSearcher. Search ("Type = 'soft '")
Set colUpdates = objResults. Updates

Note: Yes, this script can be run on a remote computer. For more information, see the script story column.

After retrieving the set, set a For Next loop to traverse all items. In this loop, use the following code to determine whether the updated "title" is Security Update for Windows XP (KB899587 ):

If colUpdates. Item (I). Title = _
"Security Update for Windows XP (KB899587)" Then

We assume that the "title" is Security Update for Windows XP (KB899587 ). In this case, check the value of the IsInstalled attribute. If IsInstalled is equal to 0, the update is not installed. (The installation may fail, or the update may be installed but deleted later ). If IsInstalled is not equal to 0, the update is installed. Check the value and then display the corresponding message:

If colUpdates. Item (I). IsInstalled <> 0 Then
Wscript. Echo "This update is installed ."
Wscript. Quit
Else
Wscript. Echo "This update is not installed ."
Wscript. Quit
End If

You will find that the script is terminated after the message is displayed. Why? Well, the update title is unique: Since we have found the update to be searched, we do not need to traverse the remaining set. Therefore, we end the script and return to our lives.

What if the "title" is not Security Update for Windows XP (KB899587? In this case, we only need to go to the next loop and check the next item in the set. If we can no longer find an update with the specified title, it will exit the loop and execute the last line of code, only reporting that the update is not installed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.