WUApiLib.iupdate VB.net检索属性错误

时间:2018-05-17 22:35:37

标签: vb.net windows-update

我有以下代码来检查Windows更新

Function CheckWinUpdates() As Integer
    CheckWinUpdates = 0
    Dim WUSession As UpdateSession
    Dim WUSearcher As UpdateSearcher
    Dim WUSearchResults As ISearchResult
    Try
        WUSession = New UpdateSession
        WUSearcher = WUSession.CreateUpdateSearcher()
        WUSearchResults = WUSearcher.Search("IsInstalled=0 and Type='Software'")
        CheckWinUpdates = WUSearchResults.Updates.Count
    Catch ex As Exception
        CheckWinUpdates = -1
    End Try

    If CheckWinUpdates > 0 Then
        Try
            'Dim Update As IUpdate
            Dim i As Integer = 0
            For i = 0 To WUSearchResults.Updates.Count - 1
                'Update = WUSearchResults.Updates.Item(i)
                EventLog.WriteEntry("Item is type: " & WUSearchResults.Updates.Item(i).ToString, EventLogEntryType.Information, 85)
                EventLog.WriteEntry("Deadline: " & WUSearchResults.Updates.Item(i).Deadline.ToString, EventLogEntryType.Information, 85)
                EventLog.WriteEntry("Type: " & WUSearchResults.Updates.Item(i).Type.ToString, EventLogEntryType.Information, 85)
                EventLog.WriteEntry("Released on: " & WUSearchResults.Updates.Item(i).LastDeploymentChangeTime, EventLogEntryType.Information, 85)
                EventLog.WriteEntry("This windows update is required: " & WUSearchResults.Updates.Item(i).Title, EventLogEntryType.Information, 85)
                'EventLog.WriteEntry("This windows update is required: " & Update.Title & vbCrLf & "Released on: " &
                '                    Update.LastDeploymentChangeTime & vbCrLf & "Type: " & Update.Type.ToString & vbCrLf &
                '                    "Deadline: " & Update.Deadline.ToString & vbCrLf & vbCrLf & "Item is type: " & Update.ToString, EventLogEntryType.Information, 85)
            Next
        Catch ex As Exception
            EventLog.WriteEntry("Error while attempting to log required updates:" & vbCrLf & ex.Message, EventLogEntryType.Error, 86)
        End Try
    End If

    WUSearchResults = Nothing
    WUSearcher = Nothing
    WUSession = Nothing
End Function

我的意图是:a)获取适用的Windows更新数量,以及b)查看可用的其他属性,更具体地说,查看有多少是超过一周或2周。

我知道UpdateSearcher不允许按日期搜索,所以我希望遍历每个项目,然后再报告每个项目。

目前我的函数确实非常高兴地返回了更新的数量,但是当我尝试获取任何属性时,我得到“对象引用未设置为对象的实例”。

我出错的任何想法?

1 个答案:

答案 0 :(得分:0)

我得到了这个工作,转过来它不喜欢截止日期属性,我不知道它提出"对象引用没有设置",但是我需要它无所谓。