【问题标题】:How to find Domain & Username from a line in text file using Powershell如何使用 Powershell 从文本文件中的一行中查找域和用户名
【发布时间】: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


【解决方案1】:

对于这样的字符串,我会使用这样的正则表达式:

([regex]'The logged on user is\s+([^\]]+)').Match((Get-Content -Path 'C:\Windows\ccm\logs\execmgr.log' -Raw)).Groups[1].Value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2011-12-18
    • 2013-03-31
    • 2022-09-27
    相关资源
    最近更新 更多