here's one way to get what i think you want. since you did not specify exactly what you want, i grabbed both lines associated with the SideIndicator
and built a [PSCustomObject]
from them. if you want less info, modify that section as desired. [grin]
what it does ...
- fakes reading in a text file
when ready to do this with real data, replace the entire #region/#endregion
block with a call to Get-Content
.
- sets the target to decide what to keep
- iterates thru the lines
- tests for the target
- if found, builds a
[PSCustomObject]
that holds the wanted lines
- if NOT found, does nothing
- sends the
PSCO
out to the $Result
collection
- displays that on screen
the code ...
#region >>> fake reading in a text file
# in real life, use Get-Content
$InStuff = @'
InputObject : created_at : Tue Sep 24 02:31:26 +0000 2020
SideIndicator : =>
InputObject : text : CH#66551 by @Test: Testing this script to see if it works
SideIndicator : =>
InputObject : created_at : Tue Sep 24 00:27:01 +0000 2020
SideIndicator : =>
InputObject : text : CH#34330 by Test: Fixed every thing that is able to breathe
SideIndicator : =>
InputObject : created_at : Tue Sep 22 02:31:26 +0000 2020
InputObject : text : CH#66551 by @User2: Fixing stuff
InputObject : created_at : Tue Sep 22 00:27:01 +0000 2020
InputObject : text : CH#34330 by User1: Seeing if it works
InputObject : created_at : Mon Sep 21 15:54:20 +0000 2020
InputObject : text : CH#34294 by User1: Trying to find a workaround
InputObject : created_at : Mon Sep 21 15:29:13 +0000 2020
InputObject : text : CH#34291 by User3: Doing something else
InputObject : created_at : Mon Sep 21 15:03:15 +0000 2020
InputObject : text : CH#34286 by User3: Running around
InputObject :
InputObject :
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a text file
$Target = 'SideIndicator'
$Result = foreach ($Index in 0..$InStuff.GetUpperBound(0))
{
if ($InStuff[$Index] -match $Target)
{
[PSCustomObject]@{
IO_Line = $InStuff[$Index -1]
SI_Line = $InStuff[$Index]
}
}
} # end >>> foreach ($Index in 0..$InStuff.GetUpperBound(0))
$Result
output ...
IO_Line SI_Line
------- -------
InputObject : created_at : Tue Sep 24 02:31:26 +0000 2020 SideIndicator : =>
InputObject : text : CH#66551 by @Test: Testing this script to see if it works SideIndicator : =>
InputObject : created_at : Tue Sep 24 00:27:01 +0000 2020 SideIndicator : =>
InputObject : text : CH#34330 by Test: Fixed every thing that is able to breathe SideIndicator : =>