停止IIS应用程序池c#

时间:2016-04-11 14:59:49

标签: c# iis wmi

我正在尝试使用以下c#代码停止远程系统的IIS应用程序池:

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());
}

如果我停止我的应用程序池,它可以工作,但我在服务器上收到一堆错误:

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

我非常确定我不应该收到这些错误,因为如果我手动(以编程方式)启动/停止应用程序池,我就不会收到这些警告或错误。

任何人都可以帮助我理解为什么我会收到这些错误。我正在设置Packet_Privacy我认为警告对我没有意义。

由于 布拉德

0 个答案:

没有答案