模拟异常调用DirectoryEntry.Invoke方法

时间:2011-08-11 05:53:43

标签: iis impersonation

尝试使用System.DirectoryServices.DirectoryEntry.Invoke()方法回收远程IIS服务器上的应用程序池时,我遇到了令人沮丧的问题。

基本上下文是两个客户端计算机和一个IIS 7.0服务器计算机(Windows 2008 Server),myServer,都在同一个Windows域中。我想在客户端机器上运行代码 在IIS服务器上回收AppPool。

以下是相关的代码段:

DirectoryEntry directoryEntry = new DirectoryEntry(“IIS:// myServer / W3SVC / AppPools / SomeAppPool”,domainUserid,password,AuthenticationTypes.Secure);

directoryEntry.Invoke(“Recycle”,null);

从一台客户端计算机上,代码成功运行,但在另一台客户端计算机上,代码会抛出与模拟相关的异常(参见下文)。 我在两台客户端计算机上以相同的域用户身份登录,并在代码中使用相同的域用户信息。

我检查了服务器端的事件查看器和其他日志,看看服务器上的请求处理方式是否存在明显差异,并且没有成功完成大量的Google搜索。

任何人都可以提供关于要查找的内容或我可以运行的诊断(在客户端计算机或服务器计算机上)以确定发生这种情况的原因的线索吗?

感谢您的帮助!马丁

2011-08-10 22:35:39,478 [10] WARN - ActionRestartIIS:异常重启IIS System.Reflection.TargetInvocationException:异常已被删除 由调用目标抛出。 ---> System.Runtime.InteropServices.COMException(0x80070542):未提供所需的模拟级别, 或者提供的模拟级别无效。 (HRESULT异常:0x80070542)    ---内部异常堆栈跟踪结束---    在System.DirectoryServices.DirectoryEntry.Invoke(String methodName,Object [] args)

1 个答案:

答案 0 :(得分:0)

在回答自己问题的优良传统中,这就是我所学到的。我修改了我的代码以使用System.Management类,这些类似乎可以跨域更好地工作。示例代码如下:

ConnectionOptions connectionOptions = new ConnectionOptions();

connectionOptions.Authority =“ntlmdomain:”+ this.domain;

connectionOptions.Username = this.username; connectionOptions.Password = this.password;

connectionOptions.EnablePrivileges = true;

connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;

ManagementScope managementScope = new ManagementScope(@“\”+ this.iisserver + @“\ root \ microsoftiisv2”,connectionOptions);

managementScope.Connect();

ManagementObject appPool = new ManagementObject(managementScope,new ManagementPath(“IISApplicationPool.Name ='W3SVC / AppPools /”+ apppool +“'”),null);

appPool.InvokeMethod(“Recycle”,null,null);