Akseone
Akseone

Reputation: 17

ForEach loop not producing the desired result

I have written a script to Identify a timestamp in a logfile:

    $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

This returns the result I am after which is the timespan between each refresh.

Now I am trying to expand on this so it loops through the whole file. And so far my script just hangs.

    $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

    } 

From what I can tell this is correct unless I have grouped too much into the ForEach loop. Can I ask what I am missing?

Upvotes: 0

Views: 56

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 28983

change

$Workfrom = Get-Content "C:\Webserverlogfiles\ise*.log"

Foreach ($Line in (Get-Content $Workfrom)

to

$Workfrom = Get-Content "C:\Webserverlogfiles\ise*.log"

Foreach ($Line in $Workfrom) {

$workfrom is already the lines of text. Otherwise, perhaps you meant Get-ChildItem in the first line?

Upvotes: 2

Related Questions