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