Previous Next

Thread: Using WMI to recycle an app pool on remote machine

Last post 05-08-2007 3:03 PM by tkiel. 2 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (3 items)

Sort Posts:

  • 10-08-2006, 12:54 AM

    Using WMI to recycle an app pool on remote machine

    My Laptop is running Vista RC1 and trying to use WMI to recycle an application pool on a remote Longhorn server.  I am able to connect with the code listed below but it won't recycle the app pool.  If I change the AuthenticationLevel property to anything else I get an Access Denied right away.  If I have it on PacketPrivacy it connects however this message shows up in the event log?  I also tried this from a straight console application using VB express vs. Visual Web Developer.  same error showed up in the remote server.   If I run this code from a console app locally on my Vista RC1 box, the app pools recycle.  What obvious thing am I missing? :). 

    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 code

    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.EnablePrivileges = True

    options.Authentication = Management.AuthenticationLevel.PacketPrivacy

    Dim path As New Management.ManagementPath("\\" & ServerName & "\root\WebAdministration:ApplicationPool.Name='DefaultAppPool'")

    Dim scope As New Management.ManagementScope(path, options)

    scope.Connect()

    Dim opt As New Management.ObjectGetOptions

    Dim classInstance As New Management.ManagementObject(scope, path, opt)

    ' Execute the method and obtain the return values.

    Dim outParams As Management.ManagementBaseObject = _

    classInstance.InvokeMethod("Recycle", Nothing, Nothing)

    Return "done"

    End Function

    This works on a local Vista RC1 box.

    Function Whatever3()

    Dim path As New Management.ManagementPath("\\.\root\WebAdministration:ApplicationPool.Name='DefaultAppPool'")

    Dim scope As New Management.ManagementScope(path)

    scope.Connect()

    Dim opt As New Management.ObjectGetOptions

    Dim classInstance As New Management.ManagementObject(scope, path, opt)

    ' Execute the method and obtain the return values.

    Dim outParams As Management.ManagementBaseObject = classInstance.InvokeMethod("Recycle", Nothing, Nothing)

    Return "done"

    End Function

    Steve Schofield
    Windows Server MVP - IIS
    http://weblogs.asp.net/steveschofield

    http://www.IISLogs.com
    Log archival solution
    Install, Configure, Forget
  • 10-08-2006, 1:37 AM In reply to

    Re: Using WMI to recycle an app pool on remote machine

    I guess using the AppCmd.exe and Win32_Process to recycle on a remote machine is easier. :)  I used the code below that runs the AppCmd.exe command to recycle and it worked.  Hmm.  I'll be curious to hear from others who have tried this.

     

    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")

    'The lines recycles the app pool called "DefaultAppPool"
    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

    Steve Schofield
    Windows Server MVP - IIS
    http://weblogs.asp.net/steveschofield

    http://www.IISLogs.com
    Log archival solution
    Install, Configure, Forget
  • 05-08-2007, 3:03 PM In reply to

    • tkiel
    • Not Ranked
    • Joined on 04-10-2007, 5:35 PM
    • Posts 5

    Re: Using WMI to recycle an app pool on remote machine

    Add in to use pktprivacy and try running it from the server itself. See if you get a variation in results.

Page 1 of 1 (3 items)
Page view counter