我有一个将事件日志以所需格式存档到文件的功能。使用WMI备份应用程序事件日志时需要seBackupPrivilige
我正在测试Windows事件日志,Application
,Security
和System
。在所有测试中,代码都以本地管理员权限运行。
在我的开发环境中,代码将每个日志文件备份到我们称为“* .evt”文件的所有文件中。
在目标参考系统上,Security
和System
日志正常工作,但处理Application
日志会抛出ManagementException
。
以下包括异常的一个interogation。我的问题是,我有权假定这是一个安全特权问题?哪些代码更改会使此代码适用于我所需的所有情况?如果没有明确的答案,你的想法和想法是值得赞赏的。
错误码:存取遭拒
ErrorInformation:说明:打开日志文件,但未能对其进行备份,privilige错误
操作:ExecMethod
的ParameterInfo:Win32_NTEventlogFile.Name =“C:\ WINDOWS \ system32 \ config \ AppEvent.Evt“
特权不可用: - SeBackupPrivilege
PrivilegesRequired: - SeBackupPrivilege
的ProviderName:WINMGMT
的StatusCode:2147749891
using System.Management;
/* ... Omitted for brevity */
public static void WMIBackup(String logName, String targetFile)
{
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
scope.Options.EnablePrivileges = true;
ObjectQuery query = new ObjectQuery(
String.Format("SELECT * FROM Win32_NTEventLog WHERE LogFileName={0}",
logName)
);
using (ManagementObjectSearcher search =
new ManagementObjectSearcher(scope, query))
{
var logs = search.Get();
if (logs.Count != 1)
throw new ArgumentOutOfRangeException("logName not found");
foreach (ManagementObject log in logs)
{
ManagementClass eventLogClass =
new ManagementClass("Win32_NTEventLogFile");
ManangementBaseObject params =
eventLogClass.GetMethodParameters("BackupEventLog");
params["ArchiveFileName"] = targetFile;
log.InvokeMethod(
"BackupEventLog",
params,
new InvokeMethodOptions(
null,
InvokeMethodOptions.InfiniteTimeout)
);
}
}
}
所有数据被转录这样的道歉勘误表。
该死的,我复制这个代码一对一,它开始失败。我花了一段时间才明白为什么。事实证明,他搞砸了查询。它应该是'选择*从Win32_NTEventLogFile其中LogFileName = {0}'而不是。 – c00000fd 2018-02-25 11:30:21
@ c00000fd已更新。 – Jodrell 2018-02-26 15:03:31