HiddenUpdatesToXML.vb

Imports Microsoft.UpdateServices.Administration
Imports System
Imports System.Xml
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
 
Module HiddenUpdatesToXML
 
    Const usage As String = vbCrLf & "HiddenUpdates.exe creates an HTML file with a " & _
                           "list of computers where updates are set hidden by users." & _
                            vbCrLf & vbCrLf & _
                            "USAGE:" & vbCrLf & vbCrLf & _
                            "    HiddenUpdates.exe 90" & vbCrLf
 
    Sub Main(ByVal args() As String)
        Dim xml As XmlTextWriter = Nothing
        Dim Server As IUpdateServer
        Dim includeDownstreamComputers As Boolean = False
        Dim days As Integer = 90
        Dim file As String = (Environment.CurrentDirectory & "\HiddenUpdates.xml")
        If System.IO.File.Exists(file) = True Then
            System.IO.File.Delete(file)
        End If
        Console.WriteLine("Program is starting ...")
        'get the arguments we were started with
        If (args.Length >= 2) Then
            Console.WriteLine(usage)
            Exit Sub
        Else
            Try
                days = Convert.ToInt32(args(0))
                Console.WriteLine("Looking " & args(0) & " days backwards ...")
            Catch
                Console.WriteLine("Argument error!")
                Console.WriteLine(usage)
                Console.WriteLine("Using 90 days backwards ...")
            End Try
        End If
        xml = New XmlTextWriter(Environment.CurrentDirectory & _
                                "\HiddenUpdates.xml", System.Text.Encoding.UTF8)
        xml.Formatting = Formatting.Indented
        xml.Indentation = 4
        xml.WriteStartDocument(True)
 
        Try
            Console.WriteLine("Getting events ...")
            '<HiddenStatus>
            xml.WriteStartElement("HiddenStatus")
            'connect to the local server
            Server = AdminProxy.GetUpdateServer
            Console.WriteLine("Server: " & Server.Name)
            Console.WriteLine("Version: " & Server.Version.ToString)
            '<ServerInfo>
            xml.WriteStartElement("ServerInfo")
            xml.WriteAttributeString("Name", Server.Name)
            xml.WriteAttributeString("WSUSVersion", Server.Version.ToString)
            xml.WriteEndElement()
            '</ServerInfo>
            'define date range
            Dim datum1, datum2 As Date
            datum1 = Now
            datum2 = datum1.AddDays(-days)
            Dim EventCollection As UpdateEventCollection
            EventCollection = Server.GetUpdateEventHistory(datum2, datum1)
            EventCollection = Server.GetUpdateEventHistory(datum2, datum1)
            '<GeneratingInfo>
            xml.WriteStartElement("GeneratingInfo")
            xml.WriteAttributeString("FromDate", datum2.ToString)
            xml.WriteAttributeString("ToDate", datum1.ToString)
            xml.WriteAttributeString("Entries", EventCollection.Count.ToString)
            xml.WriteEndElement()
            '</GeneratingInfo>
            Console.WriteLine("Found " & EventCollection.Count.ToString & " events.")
            Console.WriteLine("Writing events ...")
            Dim counter As Integer
            Dim pcname As String
            counter = 0
            For Each myevent As IUpdateEvent In EventCollection
                counter = counter + 1
                If myevent.WsusEventId = WsusEventId.ClientInstallationHidden Then
                    '<HiddenUpdate>
                    xml.WriteStartElement("HiddenUpdate")
                    pcname = myevent.GetComputer.FullDomainName.Replace(".mainp.knetz.net", "")
                    '<Computer>
                    xml.WriteStartElement("Computer")
                    xml.WriteAttributeString("Name", pcname)
                    xml.WriteAttributeString("Date", myevent.CreationDate.ToString)
                    xml.WriteEndElement()
                    '</Computer>
                    '<UpdateInfo>
                    xml.WriteStartElement("UpdateInfo")
                    xml.WriteAttributeString("Title", myevent.GetUpdate.Title)
                    xml.WriteAttributeString("Description", myevent.GetUpdate.Description)
                    xml.WriteAttributeString("Severity", myevent.GetUpdate.MsrcSeverity.ToString)
                    xml.WriteEndElement()
                    '</UpdateInfo>
                    xml.WriteEndElement()
                    '</HiddenUpdate>
                End If
            Next
            '</HiddenStatus>
            xml.WriteEndElement()
            xml.Close()
 
        Catch ex As Exception
            '<Error>
            xml.WriteStartElement("Error")
            xml.WriteAttributeString("Error", "An error occured:" & vbCrLf & _
                                     "Error text: " & ex.Message & vbCrLf & _
                                     "Stack trace: " & ex.StackTrace & vbCrLf)
            '</Error>
            xml.WriteEndElement()
            '</HiddenStatus> perhaps
            xml.WriteEndElement()
            xml.Close()
            Console.WriteLine("Program error!")
            Console.WriteLine(ex.Message)
        End Try
 
    End Sub
 
End Module