Last active
September 24, 2018 13:56
-
-
Save jschpp/675a3ef843b7574e6a04acb6c4480642 to your computer and use it in GitHub Desktop.
Answer for https://www.reddit.com/r/PowerShell/comments/9igf1i/logical_table_population_from_text_file_data/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ParseLine([string]$Line) { | |
$returnObject = @{ | |
Date = $null | |
Time = $null | |
Status = $null | |
} | |
$returnObject.Date = Get-Date $line.Split(',')[0] -Format "dd/MM/yyyy" | |
$returnObject.Time = Get-Date $line.Split(',')[0] -Format "hh:mm:ss" | |
$returnObject.Status = $line.Split(',')[1].TrimEnd('.') | |
return $returnObject | |
} | |
$Output = @() | |
$file = Get-Content $filename # this needs to be filled somewhere *grin* | |
$PreviousLine = ParseLine -Line $File[0] | |
foreach ($LineIdx in 1..$($File.Count)) { | |
if (-not $File[$LineIdx]) { | |
Write-Verbose "Empty line: $($LineIdx + 1)" | |
continue | |
} | |
if (-not $PreviousLine) { | |
# If unset: set and go to next line | |
$PreviousLine = ParseLine -Line $File[$LineIdx] | |
continue | |
} | |
$CurrentLine = ParseLine -Line $File[$LineIdx] | |
$temp = [ordered] @{ | |
Date = $PreviousLine.Date | |
Instance = $null # no idea where you get this value | |
Started = $null | |
Finished = $null | |
} | |
switch ("$($PreviousLine.Status)$($CurrentLine.Status)") { | |
"StartedFinished" { | |
$temp.Started = $PreviousLine.Time | |
$temp.Finished = $CurrentLine.Time | |
# Since both lines were processed the buffers can be cleared | |
$PreviousLine = $null | |
$CurrentLine = $null | |
} | |
"StartedStarted" { | |
$temp.Started = $PreviousLine.Time | |
# since it is possible that the current started has a finish we keep it in the buffer | |
$PreviousLine = $CurrentLine | |
$CurrentLine = $null | |
} | |
"FinishedStarted" { | |
$temp.Finished = $PreviousLine.Time | |
# since it is possible that the current started has a finish we keep it in the buffer | |
$PreviousLine = $CurrentLine | |
$CurrentLine = $null | |
} | |
} | |
$Output += New-Object PSObject -Property $temp | |
} | |
# if after Processing all records there is still a line in the buffer we also write it down | |
if ($PreviousLine) { | |
$temp = [ordered] @{ | |
Date = $PreviousLine.Date | |
Instance = $null # no idea where you get this value | |
Started = $null | |
Finished = $null | |
} | |
switch ($PreviousLine.Status) { | |
"Started" { | |
$temp.Started = $PreviousLine.Time | |
} | |
"Finished" { | |
$temp.Finished = $PreviousLine.Time | |
} | |
} | |
$Output += New-Object PSObject -Property $temp | |
} | |
return $Output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment