C#、WMI 和 Windows 7 与 Windows 2008 对话

发布于 2024-10-20 02:30:37 字数 1080 浏览 1 评论 0原文

我已经为此绞尽脑汁好几天了,但我不明白发生了什么事。我的开发盒是 Windows 7 64 位计算机,我尝试通过 WMI 连接到 Windows Server 2008 域控制器。

我的代码非常简单:

ConnectionOptions options = new ConnectionOptions();
options.Username = _username;
options.Password = _password;
options.EnablePrivileges = true; 
options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = AuthenticationLevel.Default;           

ManagementScope scope = new ManagementScope(@"\\" + _hostip + @"\root\cimv2", options);

scope.Connect(); // this works!

ManagementEventWatcher watcher = new ManagementEventWatcher();
watcher.Scope = scope;
watcher.Query = new EventQuery(@"SELECT * FROM __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' and TargetInstance.LogFile = 'Security'");

watcher.Start() // throws exception
watcher.WaitForNextEvent(); // works

我对 WMI 的了解有限,我无法弄清楚为什么 WaitForNextEvent() 有效但 Start() 无效。我不断收到的异常是:访问被拒绝。 (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))

我已多次检查我尝试连接的域控制器是否设置正确(远程处理已打开、启用并具有权限),用户帐户也是如此我用的是连接。

任何想法和见解将不胜感激。

I have been racking my head over this for a couple days now and I cannot figure out what is going on. My dev box is a Windows 7 64-bit machine and I am trying to connect via WMI to a Windows Server 2008 domain controller.

My code is pretty simple:

ConnectionOptions options = new ConnectionOptions();
options.Username = _username;
options.Password = _password;
options.EnablePrivileges = true; 
options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = AuthenticationLevel.Default;           

ManagementScope scope = new ManagementScope(@"\\" + _hostip + @"\root\cimv2", options);

scope.Connect(); // this works!

ManagementEventWatcher watcher = new ManagementEventWatcher();
watcher.Scope = scope;
watcher.Query = new EventQuery(@"SELECT * FROM __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' and TargetInstance.LogFile = 'Security'");

watcher.Start() // throws exception
watcher.WaitForNextEvent(); // works

My knowledge of WMI is limited and I cannot figure out why WaitForNextEvent() works but Start() does not. The exception I keep getting is: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

I've checked multiple times that the domain controller I'm attempting to connect to is set up properly (remoting is on, enabled and has permission) as is the user account I'm using to connect.

Any ideas and insight would be greatly appreciated.

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

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

发布评论

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

评论(2

你与清晨阳光 2024-10-27 02:30:37

试试这个:

options.Authentication = AuthenticationLevel.PacketPrivacy;

Try this:

options.Authentication = AuthenticationLevel.PacketPrivacy;
牵你的手,一向走下去 2024-10-27 02:30:37

我认为您遇到此错误,因为部分受信任的代码无法将此成员用作 在文档中进行解释。有关详细信息,请参阅使用部分受信任代码中的库

您可以在不使用此成员的情况下接收事件。

I think that you have hot this error because this member cannot be used by partially trusted code as explain in documentation. For more information, see Using Libraries from Partially Trusted Code.

You can receive events without using this member.

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