Avatar of Nick Collins
Nick Collins
 asked on

Powershell Script - Extracting data from a csv file

Hello,

I would like some help with a powershell script that extracts names from a CSV file based on particular criteria.

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

$a = Import-Csv  D:\source.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
                $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

Open in new window


In the source file it is extracting the names in the row where 'Select the operatives -' appears

On initial testing it is extracting names on every row but it misses the row where the names 'Wayne Pepperpot, Graham Prickstick'

Any ideas?

Names in the source file are made up
Powershell

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
Nick Collins

ASKER
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61