Search code examples
vb.netwindows-update

WUApiLib.iupdate VB.net retrieving properties error


I have the following code to check windows updates

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

My intention with this is to a) get the number of windows updates that are applicable, and b) to look at what other properties are available, and more specifically see how many are older than a week or 2.

I know that UpdateSearcher doesn't allow to search by date, so I am looking to iterate through each item and then later report on each one.

At the moment my function does quite happily return the number of updtes, but when I try to get any of the properties I get "Object reference not set to an instance of an object".

Any ideas where I'm going wrong?


Solution

  • I got this working, turns it it doesn't like the deadline property, I don't know it comes up with "object reference not set", but for what I need it doesn't matter.