ForEach 循环没有产生预期的结果
ForEach loop not producing the desired result
我写了一个脚本来识别日志文件中的时间戳:
$start = Get-Content "C:\Webserverlogfiles\ise*.log" | select -first 1 -skip 6
$end -match '(([0-9][0-9]:){2}([0-9][0-9]))'
$end = $Matches[1]
$start -match '(([0-9][0-9]:){2}([0-9][0-9]))'
$start = $Matches[1]
$TimeDiff = New-TimeSpan $end $start
if ($TimeDiff.Seconds -lt 0) {
$Hrs = ($TimeDiff.Hours) + 23
$Mins = ($TimeDiff.Minutes) + 59
$Secs = ($TimeDiff.Seconds) + 59 }
else {
$Hrs = $TimeDiff.Hours
$Mins = $TimeDiff.Minutes
$Secs = $TimeDiff.Seconds }
$refreshrate = '{0:00}:{1:00}:{2:00}' -f $Hrs,$Mins,$Secs
echo $refreshrate
这returns我得到的结果是每次刷新之间的时间跨度。
现在我试图对此进行扩展,以便它循环遍历整个文件。到目前为止,我的脚本只是挂起。
$Workfrom = Get-Content "C:\Webserverlogfiles\ise*.log"
Foreach ($Line in (Get-Content $Workfrom)) {
$end = $line | Select-String 'ShowStatus = Reset Status' -SimpleMatch
$end -match '(([0-9][0-9]:){2}([0-9][0-9]))'
$end = $Matches[1]
$start = $line | Select-String 'ShowStatus = Waiting for server ()' -SimpleMatch
$start -match '(([0-9][0-9]:){2}([0-9][0-9]))'
$start = $matches[1]
$TimeDiff = New-TimeSpan $end $start
if ($TimeDiff.Seconds -lt 0) {
$Hrs = ($TimeDiff.Hours) + 23
$Mins = ($TimeDiff.Minutes) + 59
$Secs = ($TimeDiff.Seconds) + 59 }
else {
$Hrs = $TimeDiff.Hours
$Mins = $TimeDiff.Minutes
$Secs = $TimeDiff.Seconds }
$refreshrate = '{0:00}:{1:00}:{2:00}' -f $Hrs,$Mins,$Secs
echo $refreshrate
}
据我所知,这是正确的,除非我在 ForEach 循环中分组太多。我可以问我缺少什么吗?
改变
$Workfrom = Get-Content "C:\Webserverlogfiles\ise*.log"
Foreach ($Line in (Get-Content $Workfrom)
到
$Workfrom = Get-Content "C:\Webserverlogfiles\ise*.log"
Foreach ($Line in $Workfrom) {
$workfrom
已经是文本行。否则,也许你的意思是第一行的 Get-ChildItem
?
我写了一个脚本来识别日志文件中的时间戳:
$start = Get-Content "C:\Webserverlogfiles\ise*.log" | select -first 1 -skip 6
$end -match '(([0-9][0-9]:){2}([0-9][0-9]))'
$end = $Matches[1]
$start -match '(([0-9][0-9]:){2}([0-9][0-9]))'
$start = $Matches[1]
$TimeDiff = New-TimeSpan $end $start
if ($TimeDiff.Seconds -lt 0) {
$Hrs = ($TimeDiff.Hours) + 23
$Mins = ($TimeDiff.Minutes) + 59
$Secs = ($TimeDiff.Seconds) + 59 }
else {
$Hrs = $TimeDiff.Hours
$Mins = $TimeDiff.Minutes
$Secs = $TimeDiff.Seconds }
$refreshrate = '{0:00}:{1:00}:{2:00}' -f $Hrs,$Mins,$Secs
echo $refreshrate
这returns我得到的结果是每次刷新之间的时间跨度。
现在我试图对此进行扩展,以便它循环遍历整个文件。到目前为止,我的脚本只是挂起。
$Workfrom = Get-Content "C:\Webserverlogfiles\ise*.log"
Foreach ($Line in (Get-Content $Workfrom)) {
$end = $line | Select-String 'ShowStatus = Reset Status' -SimpleMatch
$end -match '(([0-9][0-9]:){2}([0-9][0-9]))'
$end = $Matches[1]
$start = $line | Select-String 'ShowStatus = Waiting for server ()' -SimpleMatch
$start -match '(([0-9][0-9]:){2}([0-9][0-9]))'
$start = $matches[1]
$TimeDiff = New-TimeSpan $end $start
if ($TimeDiff.Seconds -lt 0) {
$Hrs = ($TimeDiff.Hours) + 23
$Mins = ($TimeDiff.Minutes) + 59
$Secs = ($TimeDiff.Seconds) + 59 }
else {
$Hrs = $TimeDiff.Hours
$Mins = $TimeDiff.Minutes
$Secs = $TimeDiff.Seconds }
$refreshrate = '{0:00}:{1:00}:{2:00}' -f $Hrs,$Mins,$Secs
echo $refreshrate
}
据我所知,这是正确的,除非我在 ForEach 循环中分组太多。我可以问我缺少什么吗?
改变
$Workfrom = Get-Content "C:\Webserverlogfiles\ise*.log"
Foreach ($Line in (Get-Content $Workfrom)
到
$Workfrom = Get-Content "C:\Webserverlogfiles\ise*.log"
Foreach ($Line in $Workfrom) {
$workfrom
已经是文本行。否则,也许你的意思是第一行的 Get-ChildItem
?