Powershell - Extracting Data Help

Good Morning,

I am not a expert with Powershell scripts but I would like some help with a script and a source file it is reading.

Shellscript below

$TargetLabels = @("Select the operatives -")

$a = Import-Csv D:\iauditor_exports_folder\exports\template_A97ABBC542DA4F21A291703B5BEB4C74.csv

$a | % -begin{$rec=@(); $o=@{}} `
-process{ `
if ($_.label -eq "End of Site Details") {
if ($o.count -ne 0) {
$rec += new-object -TypeName psobject -Property $o
if ($TargetLabels -contains $_.label){
$o += @{$_.label=$_.response}
#add the one we were constructing
$rec += new-object -TypeName psobject -Property $o
$rec | Export-Csv -Path D:\iauditor_exports_folder\DailyWorksheets\Output\DataNames.txt -NoTypeInformation

When the script runs it extracts the data required but for some reason it misses one line of data…

It’s easier to assist you if you provide examples of the data and the expected result. When you use Import-CSV, it generates a PSObject that you can filter with a WHERE clause, like so:

#Emulate Import-CSV
$csv = @()
$csv += [pscustomobject]@{Label = 'Label 1';Record='Select the operatives - ONE'}
$csv += [pscustomobject]@{Label = 'Label 2';Record='Stuff I do not care about'}
$csv += [pscustomobject]@{Label = 'Label 3';Record='Select the operatives - TWO'}
$csv += [pscustomobject]@{Label = 'Label 4';Record='More stuff I do not care about'}
$csv += [pscustomobject]@{Label = 'Label 5';Record='Select the operatives - THREE'}
$csv += [pscustomobject]@{Label = 'Label End';Record='End of Site Details'}

$results = $csv | Where{$_.Record -like 'Select the operatives - *'}



Label   Record                       
-----   ------                       
Label 1 Select the operatives - ONE  
Label 2 Select the operatives - TWO  
Label 3 Select the operatives - THREE