title: Stopping IIS Application Pool c#
body: <p>I am trying to stop a remote system's IIS application pool using the following c# code:</p>
  
  <pre><code>try
  {
      // Assign a variable to store the Connection Options
      var connection = new ConnectionOptions();
  
      // Connect as this user
      connection.Username = Globals.AdminUsername;
  
      // Connect with this password
      connection.Password = Globals.AdminPassword;
  
      // Connect using this domain
      connection.Authority = "ntlmdomain:MyDomain";
  
      // Gets or sets a value indicating whether user privileges 
      // need to be enabled for the connection operation.
      connection.EnablePrivileges = true;
  
      // Enable impersonation so non admins can run admin level commands 
      connection.Impersonation = ImpersonationLevel.Impersonate;
  
      //Each data packet is signed and encrypted. This helps protect 
      // the entire communication between the client and server.
      connection.Authentication = AuthenticationLevel.PacketPrivacy;
  
      // Define a WMI management scope 
      var wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\MicrosoftIISv2", ServerName), connection);
  
      string objPath = "IISApplicationPool.Name='W3SVC/AppPools/" + AppPoolName + "'";
  
      // Run the query using the scope defined above
      using (ManagementObject AppPool = new ManagementObject(wmiScope,new ManagementPath(objPath), null))
      {
          switch (Action)
          {
              case "Start":
                  AppPool.InvokeMethod("Start", null, null);
                  break;
              case "Stop":
                  AppPool.InvokeMethod("Stop", null, null);
                  break;
              case "Recycle":
                  AppPool.InvokeMethod("Recycle", null, null);
                  break;
          }
      }
  }
  catch (Exception ex)
  {
     System.Windows.Forms.MessageBox.Show(ex.ToString());
  }
  </code></pre>
  
  <p>If I stop my application pool it works, but I get a bunch of errors on the server:</p>
  
  <pre><code> -----
   The root\MicrosoftIISv2 namespace is marked with the RequiresEncryption flag. 
   Access to this namespace might be denied if the script or application does not have the 
   appropriate authentication level. Change the authentication level to Pkt_Privacy 
   and run the script or application again.
   -----
  
   -----
   An unhandled exception occurred and the process was terminated.
   Application ID: DefaultDomain
   Process ID: 1640
   Exception: System.AppDomainUnloadedException
   Message: Attempted to access an unloaded AppDomain.
   StackTrace: 
   -----
  
   -----
   Application: w3wp.exe
   Framework Version: v4.0.30319
   Description: The process was terminated due to an unhandled exception.
   Exception Info: System.Configuration.ConfigurationErrorsException
   Stack:
   at System.Diagnostics.TraceUtils.GetRuntimeObject(System.String, System.Type, System.String)
   -----
  
   -----
   Faulting application name: w3wp.exe, version: 7.5.7601.17514, time stamp: 0x4ce7a5f8
   Faulting module name: KERNELBASE.dll, version: 6.1.7601.18409, time stamp: 0x53159a86
   Exception code: 0xe0434352
   Fault offset: 0x0000c42d
   Faulting process id: 0x668
   Faulting application start time: 0x01d194020cab3c08
   Faulting application path: C:\Windows\SysWOW64\inetsrv\w3wp.exe
   Faulting module path: C:\Windows\syswow64\KERNELBASE.dll
   Report Id: 57bb8e2a-fff5-11e5-8ddc-005056a330b1
   -----
  </code></pre>
  
  <p>I'm pretty sure that I shouldn't be getting these errors because if I manually (vs programmatically) start/stop the application pool I don't get these warnings or errors. </p>
  
  <p>Can anyone help me understand why I'm getting these errors. I am setting Packet_Privacy I think so that warning doesn't make sense to me. </p>
  
  <p>Thanks
  Brad</p>