ManagementEventWatcher(WMI) 通知来自远程计算机的事件时出现异常
我正在尝试使用 WMI 和 C# 从远程计算机的事件查看器获取通知。我可以使用 ManagementObjectSearcher
连接系统并获取事件日志。但是当我尝试使用 ManagementEventWatcher.Start 方法时,我遇到了异常:
访问被拒绝。 (HRESULT 异常:0x80070005 (E_ACCESSDENIED))
我已将 WMI 控制中的权限授予 root\cimv2
,并在 DCOM Config 中授予用户帐户的管理员权限。
我有普通的 Windows 应用程序,因此在我的例子中我没有使用 ASP.net(ASPNET 用户)。
我的代码是:
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = @"Domain\UName";//txtUserName.Text;
connectionOptions.Password = "pass";//txtPassword.Text;
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope managementScope = new ManagementScope(@"\\server\root\cimv2",connectionOptions);
managementScope.Options.EnablePrivileges = true;
managementScope.Connect(); // this line is executing fine.
eventWatcher = new ManagementEventWatcher(managementScope, new EventQuery("Select * From __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' and TargetInstance.LogFile = 'Application'"));
eventWatcher.EventArrived += new EventArrivedEventHandler(Arrived);
eventWatcher.Scope.Options.EnablePrivileges = true;
eventWatcher.Start(); // Error occurs here
I am trying to get notification from a remote machine 's event viewer using WMI and C#. I am able to connect the system and also get event log by using ManagementObjectSearcher
. But when I tried to use ManagementEventWatcher.Start
method I am getting a exception:
Access is denied. (Exception from HRESULT: 0x80070005
(E_ACCESSDENIED))
I have given the permisions in WMI Control to root\cimv2
and also given the admin rights to the user's account in DCOM Config.
I have normal windows application hence I am not using ASP.net(ASPNET user) in my case.
My code is:
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = @"Domain\UName";//txtUserName.Text;
connectionOptions.Password = "pass";//txtPassword.Text;
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope managementScope = new ManagementScope(@"\\server\root\cimv2",connectionOptions);
managementScope.Options.EnablePrivileges = true;
managementScope.Connect(); // this line is executing fine.
eventWatcher = new ManagementEventWatcher(managementScope, new EventQuery("Select * From __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' and TargetInstance.LogFile = 'Application'"));
eventWatcher.EventArrived += new EventArrivedEventHandler(Arrived);
eventWatcher.Scope.Options.EnablePrivileges = true;
eventWatcher.Start(); // Error occurs here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,请记住 Microsoft 建议使用半同步操作 (正如布莱恩建议的那样):
另请参阅设置异步的安全性在 VBScript 中调用。
如果您仍想使用异步操作,请参阅以下文章:
YMMV,但对我来说(客户端:Win7 x64 SP1 服务器:Windows Server 2008 Enterprise SP2 w/o 防火墙)的解决方案在第三篇文章中发现了
E_ACCESSDENIED
异常:请注意,我是在客户端中执行上述操作的。虽然这解决了我的 DCOM 权限问题,但我随后遇到了 WMI 访问被拒绝错误 (
0x80041003
)。事实证明这是由于第二篇文章中提到的注册表项所致:请注意,您需要在服务器中设置以上内容。一旦我这样做了,异步回调就起作用了。您可以尝试的其他操作是以管理员身份运行客户端并设置 ConnectionOptions.EnablePrivileges 为 true。
有关故障排除,请参阅:
最后,我建议您利用 Microsoft 的 WMI 测试器 (
%windir%\system32\wbem\wbemtest.exe
)First, keep in mind that Microsoft recommends the use of semi-synchronous operations (as Brian suggested):
See also Setting Security on an Asynchronous Call in VBScript.
If you still want to use Async operations, refer to the following articles:
YMMV, but for me (Client: Win7 x64 SP1 Server: Windows Server 2008 Enterprise SP2 w/o firewall) the solution for the
E_ACCESSDENIED
exception was found in the third article:Note that I did the above in the client. While that fixed the DCOM permission problem for me, I then encountered WMI access denied errors (
0x80041003
). Turns out it was due to a registry key mentioned in the second article:Note that you need to set the above in the server. Once I did that, async callbacks worked. Other things you could try are running your client as an administrator and setting ConnectionOptions.EnablePrivileges to true.
For troubleshooting see:
Finally, I recommend you take advantage of Microsoft's WMI tester (
%windir%\system32\wbem\wbemtest.exe
)尝试使用 WaitForNextEvent() 半同步监听:
我们还发现 wbemtest.exe 很有用。单击“通知查询...”按钮来侦听事件。您可以尝试各种连接方法(同步、异步或半同步)。所有连接方法在连接到本地计算机时都有效,但我们只能半同步远程工作。异步(您正在使用的)更加复杂(并且安全性较低),因为服务器必须与客户端建立连接。
这里有一些关于安全和配置设置的好信息:
http://www.packettrap.com/network /知识库/PacketTrap-MSP/WMI-Troubleshooting.aspx#_Toc239699682
Try listening semi-synchronously with WaitForNextEvent():
We've also found wbemtest.exe useful. Click the Notification Query... button to listen for events. You can try the various connection methods (synchronous, asynchronous or semi-synchorous). All connection methods work when connecting to your local machine but we were only able to get semi-synchronous to work remotely. Asynchronous (which you are using) is more complex (and less secure) because the server must make a connection back to the client.
Some good information here on security and configuration settings:
http://www.packettrap.com/network/Knowledge-Base/PacketTrap-MSP/WMI-Troubleshooting.aspx#_Toc239699682
我花了好几个小时才弄清楚这个问题。以上都不适合我。
分析 IIS 服务器上的事件日志后,我发现每次在 ManagementEventWatcher 对象上调用 Start 方法时,我都会在系统日志中收到以下错误事件:
注册表搜索显示,具有错误中指定的 APPID 的应用程序是
要使异步回调工作,您需要将此 COM 对象的本地激活权限授予 IIS APPPOOL\DefaultAppPool 用户,这听起来很简单,除了用户在安全数据库。这是因为它是创建 IIS 应用程序池时自动构建的系统生成的用户帐户。
完成此工作的过程如下:
I spent hours figuring this one out. None of the above worked for me.
After analyzing the Event logs on my IIS server I found I was receivingthe following error event in the System Log every time I called the Start method on the ManagementEventWatcher object:
A registry search revealed that the application with the APPID specified in the error was
To make the asynchronous callback work you need to grant Local Activation permissions on the this COM object to the IIS APPPOOL\DefaultAppPool user, which sounds easy enough except for the fact that user does not show up as a valid acount in the security database. This is because it is a system generated user account automatically built when an IIS Application Pool is created.
The process to make this work is as follows: