Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hi Everyone,

I am having trouble with the below script. Here is the requirement:

1) Each text file needs to be compared with a single CSV file. The CSV file contains the data to that if present in the text file should match.

2) If the data in the text file matches, output the matches only and run jobs etc..

3) If the text file has no matches to the CSV file, exit with 0 as no matches are found.

I have tried to do this, but what I end up with is matches, and also non matches. What I really need is to match the lines, run the jobs,exit, if text file has no matches, then return 0

$CSVFIL = Import-Csv -Path $DRIVE	estcsvfile.csv
$TEXTFIL = Get-Content -Path "$TEXTFILFOL*.txt" |
  Select-String -Pattern 'PAT1' | 
    Select-String -Pattern 'PAT2' | 
      Select-String -Pattern 'TEST'

ForEach ($line in $CSVFIL) {

If ($TEXTFIL -match $line.COL1)  {

Write-Host 'RUNNING:' ($line.JOB01)

} else {

write-host "No Matches Found Exiting"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
264 views
Welcome To Ask or Share your Answers For Others

1 Answer

I would handle this a different way. First you need to find matches, if there are matches then process else output 0.

$matches = @()

foreach ($line in $CSVFIL)
{
    if ($TEXTFIL -contains $line.COL1)
    { $matches += $line }
}

if ($matches.Count -gt 0)
{
    $matches | Foreach-Object {
        Write-Output "Running: $($_.JOB01)"
    }
}
else
{
    Write-Output "No matches found, exiting"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...