troubleshooting Question

Powershell Script - Extracting data from a csv file

Avatar of Nick Collins
Nick Collins asked on
Powershell
2 Comments1 Solution57 ViewsLast Modified:
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

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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros