Help with Foreach loop with if and else statement

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.

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.

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.

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

 

[quote quote=161363][/quote]
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.

[quote quote=161388]

Sam Boutros wrote:
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.[/quote]

It’s a gift, really :smiley:

[quote quote=161366]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
[/quote] Your script work but if you have 100 lines to loop thru the else statement loop thru all the lines.