IIS7 – post #10 – Connecting to remote Longhorn server using WMI & rootWebAdministration 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 rootWebAdministration to recycle app pools via WMI but I ran into an issue.  This is the error.

"Access to the rootWebAdministration 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", "lh5600Administrator", "MyPassword")
   End Sub

   Function Whatever3(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 & "rootWebAdministration", 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()
        Whatever4("x,x,x,x", "lh5600Administrator", "MyPassword!")
    End Sub

    Function Whatever4(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 & "rootcimv2: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:windowssystem32inetsrvappcmd 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