Author Posts

July 26, 2018 at 3:15 pm

So far in PowerShell i work with what i know how to work but that is becoming tedious. For this process meaning creating an additional txt file to store each foreach and split operation. I dont fully understand flowing into and out of a hash table or other data storage options, i can get individual line results testing my splits and such but keeping a large group flowing without dumping the pipe to a txt and then using get-content to bring it back in eludes me.

example content snip of original txt file...
2018-06-26 13:47:43[INFO [SYS.Base.Run.Message[ID: OPR0036 Message: LASM – Calibration complete.
2018-06-26 13:47:44[INFO [SYS.Base.Run.Message[OK was clicked on the following message – ID: OPR0036 Message: LASM – Calibration complete.
2018-06-26 14:01:11[INFO [SYS.Base.Run.Message[ID: OPR0036 Message: Printer – Calibration complete.
2018-06-26 14:01:12[INFO [SYS.Base.Run.Message[OK was clicked on the following message – ID: OPR0036 Message: Printer – Calibration complete.
2018-06-29 08:28:49[INFO [SYS.Base.Run.Servo.Angle[HomeOffset has been updated from 173.400 to 170.418.
2018-06-29 08:28:56[INFO [SYS.Base.Run.Servo.Bevel[HomeOffset has been updated from 7.350 to 7.743.

End goal is to take this information and be able to massage it into summaries and reports. Here is what i have currently that i keep changing but seem to need pointers on the progression flow...

$SawLog = Get-Content "C:\temp\run.log"
$Date = "2018-06-29"

$SawLog | Select-String -Pattern $Date -Context 0,1000 | %{ $SawLog = $_ -split '\['; $NoDate =  $SawLog[1] + ' ' + $SawLog[2] + ' ' + $SawLog[3] }
Write-Host $NoDate

This gets me the last $_ in the foreach and not all lines to apply other actions too

July 26, 2018 at 7:39 pm

I'm unfortunately not at all following what your goal is. What is the hoped-for output?

July 26, 2018 at 8:12 pm

i have managed most of it i think but i have a If statement i cant seem to get to work. How do i get a split definition to pick up the -eq "All original values have been restored" so it will use a different part of the split in the output of the split? line 13...

$SawLog = Get-Content "C:\temp\run.log"
$HealthLog = "C:\temp\HealthLog.txt"
$Date = "2018-07-17"

If (Test-Path $HealthLog) {Remove-Item -Path $HealthLog -Force}
$NoDate = @()
$SawLog | Select-String -Pattern $Date -Context 0,1000 | %{ $SawLog = $_ -split '\['; $NoDate +=  $SawLog[2] + '  -  ' + $SawLog[3] | Out-String }
$CalFilter = @()
$CalFilter += $NoDate | Select-String -Pattern 'HomeOffset|.Message  -  ID: OPR0035'
$Record = @()
$Record += $CalFilter | Out-String
$MSGFilter = @()
$CalFilter | %{ $CalFilter = $_ -split '\.'; If ($CalFilter[4] -eq "All original values have been restored") {$MSGFilter += $CalFilter[3] | Out-String} Else { $MSGFilter += $CalFilter[4] | Out-String } }
$Output = @()
$MSGFilter | %{ $MSGFilter = $_ -split 'from'; $Output += $MSGFilter[0] | Out-String }
$Answer = $Output | Group-Object -NoElement | Format-Table -AutoSize | Out-String
"Calibration Details for $Date" + $Record + "Calibrations Performed on $Date" + $Answer | Set-Content -Path $HealthLog

Start-Process -FilePath 'Notepad++' -ArgumentList $HealthLog

July 26, 2018 at 8:17 pm

the output looks like this but the all original values have been restored would be better defined by printer-calibration canceled which is what the if statement in question is for...

Calibration Details for 2018-07-17
SYS.Base.Run.Message – ID: OPR0035 Message: Printer – Calibration canceled. All original values have been restored.

SYS.Base.Run.Message – ID: OPR0035 Message: Printer – Calibration canceled. All original values have been restored.

SYS.Base.Run.Message – ID: OPR0035 Message: Printer – Calibration canceled. All original values have been restored.

SYS.Base.Run.Servo.Bevel – HomeOffset has been updated from 7.231 to 7.331.

SYS.Base.Run.Message – ID: OPR0035 Message: Printer – Calibration canceled. All original values have been restored.

Calibration Summary for 2018-07-17
Count Name
—– —-
4 All original values have been restored...
1 Bevel – HomeOffset has been updated ...

July 26, 2018 at 8:25 pm

I'm not completely sure if I got what you're trying to do but I could imagine that ConvertFrom-String might be helpful for you. This video shows a nice example how to use it: Sophisticated Techniques of Plain Text Parsing.

July 26, 2018 at 8:30 pm

So, you've kind of made up your own language [grin] with splits and progression flow, so I'll try to follow.

$CalFilter | %{ $CalFilter = $_ -split '\.'; If ($CalFilter[4] -eq "All original values have been restored") {$MSGFilter += $CalFilter[3] | Out-String} Else { $MSGFilter += $CalFilter[4] | Out-String } }

This would be a LOT easier to follow without all the aliases and combined lines. A LOT. Readability counts :).

$CalFilter | 
ForEach-Object {
 $CF = $_ -split '\.' # This would have overwritten the original $CalFilter, which should still be piping things in
 If ($CF[4] -eq "All original values have been restored") {
  $MSGFilter += $CF[3] | Out-String
 } Else { 
  $MSGFilter += $CF[4] | Out-String 
 }
}

I don't recommend piping a variable into a ForEach that uses the same variable. There's a lot of times when you can end up overwriting things and getting weird output. For example, I'm not following what the logic there is intended to do, which is why I switched to a different "internal" variable to make sure I have your intent correct.

If I wasn't getting the results I wanted, I'd probably set some breakpoints, and check the contents of my variables as it went through, to make sure they contained what I thought they did. Having each statement on its own line vastly facilitates that debugging exercise.

I know that's not an answer; I'm still not following exactly what it is you're doing (sorry). I'm just trying to share my process in hopes it'll help you.

July 26, 2018 at 8:47 pm

write-host on $CF[4] (i changed internal variables to $SL and $CF) produced All original values have been restored but had two leading spaces. Put the spaces in the If statement string and now it picks it up. As for why the internal variables naming and combined lines were done, i just followed an example i found searching and didnt opt for more "variables" in the code until i got it working.

July 26, 2018 at 9:02 pm

Yeah, so, a -like comparison using * wildcards might be a less-sensitive way of matching that string.

July 26, 2018 at 9:03 pm

And as a note, I'll almost -always- add Write-Debug and/or Write-Verbose statements (vs Write-Host), since they can be turned on/off by running with/without -Debug and/or -Verbose (assuming you're using [CmdletBinding()]). Kind of provides built-in debugging as I go, that way.