【发布时间】:2021-11-06 21:17:47
【问题描述】:
我需要从文本文件中提取域\用户名。
仅提取域\用户名的示例行:
<![LOG[The logged on user is LEO\userpc]LOG]!><time="02:42:14.378+420" date="09-09-2021" component="execmgr" context="" type="1" thread="6460" file="execreqmgr.cpp:5003">
我试过的代码:
$filter = Get-Content C:\Windows\ccm\logs\execmgr.log | Where-Object { $_.Contains("LEO") } | select -Last 1
$fil = $filter.Replace('<![LOG[The logged on user is ','')
Get-Content $fil | ForEach-Object {
if ( $_ -match '^LEO' ) {
$_ -replace '-', 'x'
}
else {
$_
}
}
【问题讨论】:
-
“我尝试了 RegEx,但没有成功。” - 看起来您忘记在帖子中包含您的尝试
-
添加了尝试过的代码。
-
可能是
([regex]'The logged on user is\s+([^\]]+)').Match((Get-Content -Path 'C:\Windows\ccm\logs\execmgr.log' -Raw)).Groups[1].Value之类的? -
工作,非常感谢:)
-
powershell 可以像 cmtrace 或 trace32 那样解析日志文件吗?
标签: powershell powershell-2.0 powershell-3.0