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 has 6 replies, 4 voices, and was last updated 1 year ago by
    Participant
    .
Viewing 6 reply threads
  • Author
    Posts
    • #161313
      Participant
      Topics: 4
      Replies: 7
      Points: 36
      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: 5
      Replies: 2373
      Points: 6,010
      Helping Hand
      Rank: Community MVP

      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: 12
      Replies: 525
      Points: 1,233
      Helping Hand
      Rank: Community Hero
      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: 4
      Replies: 7
      Points: 36
      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: 12
      Replies: 525
      Points: 1,233
      Helping Hand
      Rank: Community Hero

      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: 4
      Replies: 7
      Points: 36
      Rank: Member

      Try This :

      Script:

      PowerShell
      12 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      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 :

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

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

      CSV:

      PowerShell
      3 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      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.

Viewing 6 reply threads
  • The topic ‘Help with Foreach loop with if and else statement’ is closed to new replies.