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.
[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.