Steve Schofield's Blog

Browse by Tags

All Tags » WMI   (RSS)

  • IIS7 - post #10 - Connecting to remote Longhorn server using WMI & root\WebAdministration namespace.

    I'm using a machine running Vista RC1 to query my remote Longhorn RC1 server.  The code samples listed below return information Worker Processes and helps recycle an application pool.  To recycle the app pool, I use Win32_Process class to start an AppCmd.exe.  One thing I found when connecting to a remote server is use the Management.AuthenticationLevel.PacketPrivacy enum.  This property allows you to connect otherwise you'll get Access Denied.   This property is part of the System.Management.ConnectionOptions namespace.

    'Here is a code sample
    Dim options As New System.Management.ConnectionOptions()
    options.Username = strUID
    options.Password = strPWD
    options.Impersonation = Management.ImpersonationLevel.Impersonate
    options.Authentication = Management.AuthenticationLevel.PacketPrivacy

    I tried using \root\WebAdministration to recycle app pools via WMI but I ran into an issue.  This is the error.

    "Access to the root\WebAdministration namespace was denied because the namespace is marked with RequiresEncryption but the script or application attempted to connect to this namespace with an authentication level below Pkt_Privacy. Change the authentication level to Pkt_Privacy and run the script or application again."

    Here is the thread http://forums.iis.net/thread/1421721.aspx, hopefully we'll have an answer.  Using the Win32_Process namespace to launch AppCmd.exe was a workaround.  I posted a couple other threads that are handy using WMI and the System.Management namespace to authenticate and connect to a remote server.  Check out my WMI tag.  Here are the code samples.

    Return Worker Process information.

       Sub Main()
            Whatever3("x.x.x.x", "lh5600\Administrator", "MyPassword")
       End Sub

       Function Whatever4(ByVal ServerName As String, ByVal strUID As String, ByVal strPWD As String) As String
            Try
                'Using System.Management to retrieve WMI / IIS7 info.
                'Define the WMI connection information
                Dim options As New System.Management.ConnectionOptions()
                options.Username = strUID
                options.Password = strPWD
                options.Impersonation = Management.ImpersonationLevel.Impersonate
                options.Authentication = Management.AuthenticationLevel.PacketPrivacy

                'Define the Scope information / Note the path defined.
                Dim scope As System.Management.ManagementScope
                scope = New System.Management.ManagementScope("\\" & ServerName & "\root\WebAdministration", options)

                'Define Query and Searcher objects
                Dim WMIQuery As New System.Management.SelectQuery("SELECT * FROM WorkerProcess")
                Dim searcher As New System.Management.ManagementObjectSearcher(scope, WMIQuery)

                'Connect to WMI
                Try
                    scope.Connect()
                Catch ex As Exception
                End Try

                'Write the list of Worker Process info 
                For Each queryObj As Management.ManagementObject In searcher.Get()

                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("WorkerProcess instance")
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("ApplicationPool: {0}", queryObj("ApplicationPool"))
                Next

            Catch err As Management.ManagementException
                Console.WriteLine("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Function

    Recycle DefaultAppPool using AppCMD and WMI


        Sub Main()
            Whatever3("x,x,x,x", "lh5600\Administrator", "MyPassword!")
        End Sub

        Function Whatever3(ByVal ServerName As String, ByVal strUID As String, ByVal strPWD As String) As String
            Dim options As New System.Management.ConnectionOptions
            options.Username = strUID
            options.Password = strPWD
            options.Authentication = Management.AuthenticationLevel.PacketPrivacy

            Dim path As New System.Management.ManagementPath("\\" & ServerName & "\root\cimv2:Win32_Process")
            Dim scope As New System.Management.ManagementScope(path, options)

            scope.Connect()

            Dim opt As New System.Management.ObjectGetOptions()
            Dim classInstance As New System.Management.ManagementClass(scope, path, opt)

            Dim inParams As System.Management.ManagementBaseObject = classInstance.GetMethodParameters("Create")
            inParams("CommandLine") = "c:\windows\system32\inetsrv\appcmd recycle apppool defaultapppool"

            ' Execute the method and obtain the return values.
            Dim outParams As System.Management.ManagementBaseObject = classInstance.InvokeMethod("Create", inParams, Nothing)
            Return "ReturnValue:" & outParams("returnValue") & " Process ID: {0}" & outParams("processId")
        End Function

    Happy IIS7 coding!!

    Steve Schofield
    Microsoft MVP - IIS

     

    Share this post: Email it! | bookmark it! | digg it! | reddit!
  • IIS7 - post #8

    Here is sample code to return a collection of Application Pools using WMI and IIS7.  This is pretty dynamic because this could be used to retrieve from a local machine or altering the code to connect to a remote machine running IIS7.

    'Define the WMI connection information
    Dim options As New System.Management.ConnectionOptions()
    'options.Username = strUID
    'options.Password = strPWD

    'Define the Scope information / Note the path defined.
    Dim scope As System.Management.ManagementScope
    scope =
    New System.Management.ManagementScope(\\.\root\WebAdministration)

    'Define Query and Searcher objects
    Dim WMIQuery As New System.Management.SelectQuery("SELECT * FROM ApplicationPool")
    Dim searcher As New System.Management.ManagementObjectSearcher(scope, WMIQuery)

    'Connect to WMI
    Try
           scope.Connect()
    Catch ex As Exception
           Exit Sub
    End Try

    'Dim variables for information that will be returned
    Dim AppPoolName As System.Management.ManagementObject
    Dim col As System.Management.ManagementObjectCollection

    'Return the collection
    col = searcher.Get()

    'Write the list of Application Pools to webpage
    For Each AppPoolName In col
           Response.Write(AppPoolName.GetPropertyValue(
    "Name").ToString() & "<BR>")
    Next


    Here is the IIS7'ish way of using the Microsoft.Web.Administration on a local machine.  I'm still trying to figure out how to use this new IIS7 namespace to connect to a remote machine.

            Dim Server As New Microsoft.Web.Administration.ServerManager  
            Dim col As ApplicationPoolCollection

            col = Server.ApplicationPools

            Dim AppPoolName As String = ""
            Dim x As Integer
           
            For x = 0 To col.Count - 1
                AppPoolName = col(x).Name
            Next
    Share this post: Email it! | bookmark it! | digg it! | reddit!

Powered by Community Server 2.1