问题描述
我正在浏览客户端持有应用程序会话的日志文件。 该日志没有清楚记录,因此您必须首先找到要更新的客户端编号,然后再次检查为该客户端分配了该编号的日志。
当我在比赛中包含找到的号码或选择字符串时,我无法完成最后一步。写起来很简单。
这是我的流程:
$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.