Help with Foreach loop with if and else statement

Welcome Forums General PowerShell Q&A Help with Foreach loop with if and else statement

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #161313

    Participant
    Topics: 3
    Replies: 4
    Points: 18
    Rank: Member

    I am currenting writing a script in an array. The array work but the problem I am having that if it cant find the pc name in the array I want it to output the Computer name is not found. However instead it count all the line down with computer not found per line.

    this is a test script that i am working on

    Import-csv "c:\temp\id.csv" | ForEach-Object {
    $computer = $_.computername
    if ($computer -match $env:computername){
    
    Write-Host $_.Printer0,$_.Printer1,$_.Printer2,$_.Printer3
    
    }
    Else
    {
    write-host "$computer does not exit"
    }
    }
    
    #End result it comes out like this
    
    James does not exit
    Nick does not exit
    Sarah does not exit
    Dominic does not exit
    John does not exit
    Joe does not exit
    
    

    I just want to say the Current pc does not exist in database.

  • #161361

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    You might show some lines of your CSV file – sanitized from sensitive data – formatted as code please. You can edit your initial post – you don't have to add a new one.

  • #161363

    Participant
    Topics: 9
    Replies: 423
    Points: 676
    Helping Hand
    Rank: Major Contributor
    if ($Found = Import-csv 'c:\temp\id.csv' | where { $_.computername -match $env:computername }) {
        Write-Host $Found.Printer0,$Found.Printer1,$Found.Printer2,$Found.Printer3
    } else {
        Write-Host "$env:computername does not exit"
    }
    

    You don't need to loop and manually iterate through the list of items imported from CSV. You can simply use where-object to identify whether the desired row exists and have it saved in $Found variable.

  • #161366

    Participant
    Topics: 0
    Replies: 13
    Points: 43
    Helping Hand
    Rank: Member

    Try This :

    Script:

    Import-csv "c:\temp\id.csv" | ForEach-Object {
    $computer = $_.name
    if ($computer -match $env:computername){
    
    Write-Host "Printer Assigned are : $($_.printer01), $($_.printer02 )"
    
    }
    Else
    {
    write-host "$computer does not exit"
    }
    }

    Output :

    Printer Assigned are : hp01, hp02
    DC-02 does not exit

     

    CSV:

    name,printer01,printer02
    DC-01,hp01,hp02
    DC-02,xerox01,hp02

     

  • #161388

    Participant
    Topics: 3
    Replies: 4
    Points: 18
    Rank: Member

    how do you make it so simple i been kicking my heading thinking. I guess I was stuck on my code. This does exactly what it wanted it to do.

  • #161399

    Participant
    Topics: 9
    Replies: 423
    Points: 676
    Helping Hand
    Rank: Major Contributor

    how do you make it so simple i been kicking my heading thinking. I guess I was stuck on my code. This does exactly what it wanted it to do.

    It's a gift, really 😀

  • #161520

    Participant
    Topics: 3
    Replies: 4
    Points: 18
    Rank: Member

    Try This :

    Script:

    PowerShell
    12 lines

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Import-csv "c:\temp\id.csv" | ForEach-Object {
    $computer = $_.name
    if ($computer -match $env:computername){
    Write-Host "Printer Assigned are : $($_.printer01), $($_.printer02 )"
    }
    Else
    {
    write-host "$computer does not exit"
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Output :

    1
    2
    Printer Assigned are : hp01, hp02
    DC-02 does not exit
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    CSV:

    PowerShell
    3 lines

    1
    2
    3
    name,printer01,printer02
    DC-01,hp01,hp02
    DC-02,xerox01,hp02
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Your script work but if you have 100 lines to loop thru the else statement loop thru all the lines.

The topic ‘Help with Foreach loop with if and else statement’ is closed to new replies.