如何使用 Powershell 从文本文件的一行中查找域和用户名

How to find Domain & Username from a line in text file using Powershell

我需要从文本文件中提取 domain\username。

仅提取 domain\username 示例行:

<![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]'The logged on user is\s+([^\]]+)').Match((Get-Content -Path 'C:\Windows\ccm\logs\execmgr.log' -Raw)).Groups[1].Value