•      Powered by
 

Retrieve metabase information using .NET 2.0 console application.

This is a console application I wrote to collect metabase information from machines running IIS 6.0.   The script reads in a text file with a list of machines, connects to the remote server and retrieves the data.   The data is stored into an array and when all machines have been quiered, the results are written to a text file.  I found collecting metabase information from several machines using .NET 2.0 is very quick.   I'm passing this code along as a starting point, if you find a more '.NET'ish' way to perform this code; please feel free to pass along.

Module Module1

    Private arrList As New System.Collections.ArrayList
    Private arrFTPList As New System.Collections.ArrayList
    Private m_GetAnonymousUser As String
    Private m_Path As String
    Private m_AppPoolID As String

    Sub Main()

        Dim reader As System.IO.StreamReader
        Dim line As String
        Dim item As String
        Try
            ' open a reader for the input file, and read line by line
            reader = New System.IO.StreamReader("IISList.txt")
            Do While reader.Peek() >= 0
                Try
                    ' read a line and split it into tokens, divided by the specified
                    ' separators
                    line = reader.ReadLine()
                    Console.WriteLine(line)
                    GetSites(line & ".orcsweb.com")
                Catch ex As Exception
                    errorhandler(ex, line)
                End Try
            Loop

            For Each item In arrList
                LogInfo(item)
            Next
            Console.WriteLine("Done")
            Console.WriteLine("Count of records" & arrList.Count)
        Catch exp As Exception

        End Try
    End Sub

    Private Sub GetSites(ByVal strServerIP As String)
        Dim aBinding As Object
        Dim binding As Object
        Dim objWWW As Object
        Dim item As Object

        objWWW = GetObject("IIS://" & strServerIP & "/W3SVC")
        For Each item In objWWW
            Try
                If (item.Class = "IIsWebServer") Then
                    aBinding = item.ServerBindings
                    If (IsArray(aBinding)) Then
                        If aBinding(0) <> "" Then
                            binding = getBinding(aBinding(0))
                        End If
                    Else
                        If aBinding <> "" Then
                            binding = getBinding(aBinding)
                        End If
                    End If
                    'Will list host-header information
                    GetServerBindings(strServerIP, item.Name, item.ServerComment)
                ElseIf (item.class = "IIsFtpService") Then
                    aBinding = item.ServerBindings
                    If (IsArray(aBinding)) Then
                        If aBinding(0) <> "" Then
                            binding = getBinding(aBinding(0))
                        End If
                    Else
                        If aBinding <> "" Then
                            binding = getBinding(aBinding)
                        End If
                    End If
                    'Will list host-header information
                    GetServerBindings(strServerIP, item.Name, item.ServerComment)
                End If
            Catch ex As Exception
                errorhandler(ex, strServerIP)
            End Try
        Next

    End Sub

    Function GetAnonymousUser(ByVal strServerIP As String, ByVal strSiteName As String) As Object
        Dim objWebsite As Object

        ' Get the root virtual directory object for a server
        objWebsite = GetObject("IIS://" & strServerIP & "/W3SVC/" & strSiteName & "/ROOT")
        Try
            m_GetAnonymousUser = objWebsite.AnonymousUserName
        Catch ex As Exception
            m_GetAnonymousUser = "NOTHING"
        End Try

        Try
            m_Path = objWebsite.Path
        Catch ex As Exception
            m_Path = "NOTHING"
        End Try

        Try
            m_AppPoolID = objWebsite.AppPoolID
        Catch ex As Exception
            m_AppPoolID = "NOTHING"
        End Try
    End Function

    Function GetServerBindings(ByVal strServerIP As String, ByVal strSiteName As String, ByVal ServerComment As String) As Object
 
       Dim objWebsite As Object
        Dim obj As String
        Dim serverBinds As Object = ""
        'Dim arrValue As String()
        'Dim retValue As String
        ' Get the root virtual directory object for a server
        objWebsite = GetObject("IIS://" & strServerIP & "/W3SVC/" & strSiteName)
        GetAnonymousUser(strServerIP, strSiteName)
        For Each obj In objWebsite.ServerBindings
            Try
                serverBinds = serverBinds & obj & " "
                arrList.Add(obj & "!" & strServerIP & "!" & strSiteName & "!" & ServerComment & "!" & m_GetAnonymousUser & "!" & m_Path & "!" & m_AppPoolID)
            Catch ex As Exception
                errorhandler(ex, strServerIP)
            End Try
        Next
        GetServerBindings = serverBinds
    End Function

    Function GetFTPServerBindings(ByVal strServerIP As String, ByVal strSiteName As String, ByVal ServerComment As String) As Object
        Dim objWebsite As Object
        Dim obj As String
        Dim serverBinds As Object = ""
        objWebsite = GetObject("IIS://" & strServerIP & "/MSFTPSVC/" & strSiteName)
        For Each obj In objWebsite.ServerBindings
            Try
                serverBinds = serverBinds & obj & " "
                arrFTPList.Add(obj & "!" & strServerIP & "!" & strSiteName & "!" & ServerComment & "!" & m_Path)
            Catch ex As Exception
                errorhandler(ex, strServerIP)
            End Try
        Next
        GetFTPServerBindings = serverBinds
    End Function

    Function getBinding(ByVal bindstr)
        Dim intPort As Int32
        Dim strHostHeader As String
        Dim intFirstColon As Int32
        Dim intSecondColon As Int32

        intFirstColon = InStr(bindstr, ":")
        intSecondColon = InStr(intFirstColon + 1, bindstr, ":")

        intPort = Mid(bindstr, intFirstColon + 1, intSecondColon - intFirstColon - 1)
        strHostHeader = Mid(bindstr, intSecondColon + 1)
        getBinding = strHostHeader & ":" & intPort
    End Function

    Sub LogInfo(ByVal item As String)

        Dim fs As System.IO.FileStream
        Dim sw As System.IO.StreamWriter

        Dim strHtmlFile As String = "IISInfo.txt"
        If System.IO.File.Exists(strHtmlFile) = False Then
            fs = System.IO.File.Create(strHtmlFile)
            fs.Close()
        End If

        'sleep for 10 milliseconds to allow OperatingSystem to release threads on file
        Threading.Thread.Sleep(100)
        'Append Encoded string to file

        sw = System.IO.File.AppendText(strHtmlFile)
        sw.WriteLine(item)
        sw.Flush()
        sw.Close()
    End Sub

    Sub errorhandler(ByVal exp As Exception, ByVal strServerIP As String)
        Dim fs As System.IO.FileStream
        Dim sw As System.IO.StreamWriter
        Dim strHtmlFile As String = "IISErrorInfo.txt"

        Try
            If System.IO.File.Exists(strHtmlFile) = False Then
                fs = System.IO.File.Create(strHtmlFile)
                fs.Close()
            End If

            'sleep for 10 milliseconds to allow OperatingSystem to release threads on file
            Threading.Thread.Sleep(100)
            'Append Encoded string to file

            sw = System.IO.File.AppendText(strHtmlFile)
            sw.WriteLine("Computer:" & strServerIP & " Error: " & exp.ToString())
            sw.Flush()
            sw.Close()
        Catch f As Exception
        End Try
    End Sub
End Module

 

tio

Terms of Use | Privacy Statement ©2005-2006 IISLogs.com. All rights reserved - Powered by IIS7 - info @ www.IIS.net