如何在 match/select-string 中使用变量结果 - Powershell

How to use a variable result in a match/select-string - Powershell

我正在查看客户端持有应用程序会话的日志文件。 这没有记录清楚,所以你必须先找到客户端号码更新,然后再次检查为客户端分配号码的日志。

我无法完成这最后一步,当我在匹配中包含我找到的号码时,或者 select-string 我没有命中。写清楚就可以了。

这是我的过程:

    $path1 = "\path\previous\logfile\.log.1"
    $path2 = "\path\current\logfile\*.log"
    $getLogContent = Get-Content $path1, $path2
    $stringToSplit =$getlogcontent.where({$_ -match "Sending device updates to client"}, 'Last')
    $client= $stringtosplit.split("#")
    $clientString = $client[1]
    $number= $clientstring.split(":")
    $finalSearch = "got client ID $number"
    $SessionOwner= $getlogcontent | Select-String $finalSearch -Context 3,0
    $sessionOwner

这是我的数据:

PS:>$stringToSplit
2020-09-18 12:24:50,042 DEBUG P0000 [ApplicationSrv] [ApplicationFactory] Sending device updates to client #122:

我要匹配的对象:

PS:>$SessionOwner= $getlogcontent.where({$_ -match "got client ID"})
$SessionOwner
2020-09-18 06:54:56,914 INFO  P0000 Application[Client 115 ] registered successfully, got client ID 115.
2020-09-18 06:58:05,109 INFO  P0000 Application[Client 116 ] registered successfully, got client ID 116.
2020-09-18 07:00:04,013 INFO  P0000 Application[Client 117 ] registered successfully, got client ID 117.
2020-09-18 07:06:37,101 INFO  P0000 Application[Client 118 ] registered successfully, got client ID 118.
2020-09-18 07:06:43,081 INFO  P0000 Application[Client 119 ] registered successfully, got client ID 119.
2020-09-18 07:11:19,963 INFO  P0000 Application[Client 120 ] registered successfully, got client ID 120.
2020-09-18 10:20:26,638 INFO  P0000 Application[Client 121 ] registered successfully, got client ID 121.
2020-09-18 10:43:57,085 INFO  P0000 Application[Client 122 ] registered successfully, got client ID 122.
2020-09-18 10:59:21,873 INFO  P0000 Application[Client 123 ] registered successfully, got client ID 123.
2020-09-18 11:23:31,339 INFO  P0000 Application[Client 124 ] registered successfully, got client ID 124.
2020-09-18 11:41:13,406 INFO  P0000 Application[Client 125 ] registered successfully, got client ID 125.
2020-09-18 12:14:23,370 INFO  P0000 Application[Client 126 ] registered successfully, got client ID 126. 

我觉得我已经尝试了各种连接、正则表达式、"search text $varaible"'search text'$variable 并制作了一个新的完整 $variable。我完全不知道如何将 $number 添加到我的字符串并进行搜索。

它没有按您预期的方式工作的原因是因为这个拆分

$number= $clientstring.split(":")

实际输出两行

$number | foreach {"Line $_"}
Line 122
Line

您可以抓住第一个元素来更正它。

$number= $clientstring.split(":")[0]

$number | foreach {"Line $_"}
Line 122

不过,我建议稍微简化一下您的代码。这将一次性完成

if($getLogContent.where({$_ -match '(?<=Sending device updates to client #)\d{3}'}, 'last'))
{
    $number = $matches.0
}

$number | foreach {"Line $_"}
Line 122

您没有说明为什么要抓住上面的三行。然而,这里留下的是我推荐的代码

$path1 = "\path\previous\logfile\.log.1"
$path2 = "\path\current\logfile\*.log"
$getLogContent = Get-Content $path1, $path2
if($getLogContent.where({$_ -match '(?<=Sending device updates to client #)\d{3}'}, 'last'))
{
    $number = $matches.0
}
$SessionOwner= $getlogcontent | Select-String "got client ID $number" -Context 3,0
$sessionOwner

输出

  2020-09-18 07:06:43,081 INFO  P0000 Application[Client 119 ] registered successfully, got client ID 119.
  2020-09-18 07:11:19,963 INFO  P0000 Application[Client 120 ] registered successfully, got client ID 120.
  2020-09-18 10:20:26,638 INFO  P0000 Application[Client 121 ] registered successfully, got client ID 121.
> 2020-09-18 10:43:57,085 INFO  P0000 Application[Client 122 ] registered successfully, got client ID 122.