如何发现 Windows 更新是可选的、推荐的还是重要的 [英] How to Discover if Windows Update is optional, recommended, or important

查看:14
本文介绍了如何发现 Windows 更新是可选的、推荐的还是重要的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一种自动程序,其中包括搜索 Windows 更新.它可以很好地搜索和检索更新,但我无法深入了解更新的优先级.我想要一个类似于以下内容的输出:

I'm currently writing a type of automated program that among other things searches for windows updates. It can search for and retrieve updates just fine, but I am having trouble drilling down for what priority an update is. I would like to have an output similar to:

总更新:25重要:12可选:13

Total Updates: 25 Important: 12 Optional: 13

.IsMandatory 字段仅在更新专门针对用水户协会本身时使用,因此重要的更新不需要标记为 .IsMandatory.

The .IsMandatory field is only used when the update is specifically for WUA itself, so important updates are not necessary labeled with .IsMandatory.

用于搜索用水户协会的代码片段如下:

Code snippet for searching the WUA is below:

Dim updateSession  ' Object to hold our MS Update Session
Dim updateSearcher ' Object to perform our MS Win Update Search
Dim results        ' Object to hold our MS Win Update Search Results
Dim stopWatch As New Stopwatch()
stopWatch.Start()

outputWriter.WriteLine("----WINDOWS UPDATES-----@ " & Now, False)
outputWriter.WriteLine("  -We are beginning our update search. Please note, this may take a few minutes." & _
                       "  On Windows Server 2003 this may take 800 years.", False)

' We cannot guarantee the update check will succeed, so use a try catch in case of error.
Try
    updateSession = CreateObject("Microsoft.Update.Session")
    updateSearcher = updateSession.CreateUpdateSearcher()
    results = updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
Catch ex As Exception
    outputWriter.WriteLine("  ERROR: Something went wrong in our update search. Details below...", False)
    outputWriter.WriteLine("  Error Msg: " & ex.Message, False)
    Return 1
 End Try

Dim imp_updates = 0
Dim opt_updates = 0
For i = 0 To results.Updates.Count - 1
    Dim update = results.Updates.Item(i)
    If update.IsMandatory = False Then 
        opt_updates = opt_updates + 1
    Else
        imp_updates = imp_updates + 1
    End If
 Next

 outputWriter.WriteLine("Important Updates: " & imp_updates, True)
 outputWriter.WriteLine("Optional Updates:  " & opt_updates, True)

推荐答案

原来我的代码中没有对 WUApi.DLL 的引用.一旦我把那个引用放进去,我就成功地将我的结果转换为 IUpdate3.IUpdate3 是一个接口,它包含一个名为 BrowseOnly 的属性.BrowseOnly 的作用是指定更新是否可选.使用 BrowseOnly,我可以确定更新是重要的还是可选的.

It turns out that I did not have a reference to WUApi.DLL in my code. Once I put that reference in, I was successfully able to cast my result as an IUpdate3. IUpdate3 is an interface which contains a property named BrowseOnly. What BrowseOnly does, is specify if an update is optional or not. Using BrowseOnly, I can determine if an update is important or optional.

见下文(代码块中间使用了 IUpdate3)...

See below (IUpdate3 used around the middle of the code chunk)...

''' <summary>
''' Performs a windows update check for any updates that are...
''' A: Not installed
''' B: Not hidden
''' C: Software updates (OS and software, no hardware updates)
''' </summary>
''' <returns>0 on success, 1 on failure</returns>
''' <remarks></remarks>
Function checkForUpdates() As Integer

    Dim updateSession  ' Object to hold our MS Update Session
    Dim updateSearcher ' Object to perform our MS Win Update Search
    Dim results        ' Object to hold our MS Win Update Search Results
    Dim stopWatch As New Stopwatch()
    stopWatch.Start()

    outputWriter.WriteLine("----WINDOWS UPDATES-----@ " & Now, False)
    outputWriter.WriteLine("  -We are beginning our update search. Please note, this may take a few minutes." & _
                           "   On Windows Server 2003 this may take 800 years.", False)

    ' We cannot guarantee the update check will succeed, so use a try catch in case of error.
    Try
        updateSession = CreateObject("Microsoft.Update.Session")
        updateSearcher = updateSession.CreateUpdateSearcher()
        results = updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
    Catch ex As Exception
        outputWriter.WriteLine("  ERROR: Something went wrong in our update search. Details below...", False)
        outputWriter.WriteLine("  Error Msg: " & ex.Message, False)
        Return 1
    End Try

    outputWriter.WriteLine("  -Windows update search has successfully completed. Beginning iteration of result set...", False)

    ' Similar to above, we cannot guarantee iterating through our result set will succeed. Use a try catch.
    Try
        Dim totalUpdates = results.Updates.Count
        outputWriter.WriteLine("-----Windows Updates-----@ " & Now, True)
        If results.Updates.Count = 0 Then
            outputWriter.WriteLine("Total Updates: 0", True)
            outputWriter.WriteLine("Important:     0", True)
            outputWriter.WriteLine("Optional:      0", True)
        Else
            Dim imp_updates = 0
            Dim opt_updates = 0
            For i = 0 To totalUpdates - 1
                Dim update = results.Updates.Item(i)
                If CType(update, WUApiLib.IUpdate3).BrowseOnly = True Then ' BrowseOnly is specifically used for whether an update is deemed optional or not (True for optional)
                    opt_updates = opt_updates + 1
                Else
                    imp_updates = imp_updates + 1
                End If
            Next

            outputWriter.WriteLine("Total Updates: " & totalUpdates, True)
            outputWriter.WriteLine("Important:     " & imp_updates, True)
            outputWriter.WriteLine("Optional :     " & opt_updates, True)

        End If
        stopWatch.Stop()
        If stopWatch.ElapsedMilliseconds >= 1000 Then
            outputWriter.WriteLine("--------Total Time: " & Math.Round((CDbl(stopWatch.ElapsedMilliseconds) / 1000), 2) & " Sec----------------" & vbCrLf, True)
        Else
            outputWriter.WriteLine("--------Total Time: " & stopWatch.ElapsedMilliseconds & " MS-------------------" & vbCrLf, True)
        End If

    Catch ex As Exception
        outputWriter.WriteLine("  ERROR: We encountered an issue while iterating through Windows Update Search Results. Details below...", False)
        outputWriter.WriteLine("  Error Msg: " & ex.Message, False)
        Return 1
    End Try

    outputWriter.WriteLine("  -Iteration of Windows Update Search Results has successfully completed.", False)
    outputWriter.WriteLine("-------------------------" & vbCrLf, False)

    ' Clean up our objects.
    updateSession = Nothing
    updateSearcher = Nothing
    results = Nothing

    ' Success!
    Return 0
End Function

这篇关于如何发现 Windows 更新是可选的、推荐的还是重要的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆