调用 DirectoryEntry.Invoke 方法时出现模拟异常

发布于 2024-11-28 18:39:09 字数 1063 浏览 2 评论 0原文

我在尝试使用 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("回收", null);

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

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

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

感谢您的帮助! Martin

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

I have a frustrating problem trying to use the System.DirectoryServices.DirectoryEntry.Invoke() method to recycle app pools on a remote IIS server.

The basic context is two client machines and an IIS 7.0 server machine (Windows 2008 Server), myServer, all in the same Windows domain. I want to run code on the client machines to
recycle an AppPool on the IIS server.

Here's the relevant code snippet:

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

directoryEntry.Invoke("Recycle", null);

From one client machine, the code runs successfully, but on the other client machine, the code throws an exception relating to impersonation (see below).
I'm logged in as the same domain user on both client machines, and use the same domain user information in the code.

I've checked the server-side Event Viewer and other logs to see if there's some obvious difference in how the request is processed on the server, and done a significant amount of googling without success.

Can anyone give a clue as to what to look for or what diagnostics I can run (on either the client machines or on the server machine) to determine why this is happening?

Thanks for any help! Martin

2011-08-10 22:35:39,478 [10] WARN - ActionRestartIIS: Exception restarting IIS System.Reflection.TargetInvocationException: Exception has been
thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x80070542): Either a required impersonation level was not provided,
or the provided impersonation level is invalid. (Exception from HRESULT: 0x80070542)
--- End of inner exception stack trace ---
at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2024-12-05 18:39:09

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

ConnectionOptions connectionOptions = new ConnectionOptions();

connectionOptions.Authority = "ntlmdomain:" + this.domain;

连接选项.用户名 = this.用户名;
连接选项.密码 = this.密码;

连接选项.EnablePrivileges = true;

connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;

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

管理范围.Connect();

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

appPool.InvokeMethod("回收", null, null);

In that fine tradition of answering one's own questions, here's what I learnt. I modified my code to use System.Management classes and these seem to work better across domains. Sample code below:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文