1
Import-CSV -Path C:\Users\*******\Desktop\Powershell\Input.csv |
ForEach { 
    Get-ADComputer -identity $_.computer -properties * |
        select CN, extensionAttribute1, created, Description, DistinguishedName, enabled
} | 
export-csv -path C:\Users\*******\Desktop\Powershell\Output.csv -Append -Encoding UTF8 -Delimiter ";"

Hi, how i can change my PS script. If user from CSV not found then paste NAME;"NOT_FOUND", now my script just skip users that were not found with errors.

1 Answer 1

2

What you could do is a try/catch block, so that if an error occurs (when it does not exists) it outputs not found for the value "CN".

Import-CSV -Path C:\Users\*******\Desktop\Powershell\Input.csv | ForEach { 
    $computer = $_.computer
    try{
        Get-ADComputer -identity $computer -properties *
    }catch{
        @{
            CN = "$computer not found"
        }
    }
} | select @{n="CN";e={$_.CN}}, extensionAttribute1, created, Description, DistinguishedName, enabled | Export-Csv -path C:\Users\*******\Desktop\Powershell\Output.csv -Append -Encoding UTF8 -Delimiter ";"

Or as your less readable one-liner:

Import-CSV -Path C:\Users\*******\Desktop\Powershell\Input.csv | ForEach {try{Get-ADComputer -identity $_.computer -properties *}catch{@{CN = "Not found"}}} | select @{n="CN";e={$_.CN}}, extensionAttribute1, created, Description, DistinguishedName, enabled | export-csv -path C:\Users\*******\Desktop\Powershell\Output.csv -Append -Encoding UTF8 -Delimiter ";"

Note that at the select we use an expression. It says name (n)="CN";expression (e)=$_.CN which is the CN and in case of an error it consists of the value "not found" from the catch block. You can also choose to add this expression to more/different values of the select statement if you enrich the object at the catch block. Or use if/else in the expression.

Sign up to request clarification or add additional context in comments.

4 Comments

You need to append switch -ErrorAction Stop to the Get-ADComputer line so the code will enter the catch block even on non-terminating exceptions. Also, Get-ADComputer already returns objects with these properties: DistinguishedName, DNSHostName, Enabled, Name, ObjectClass, ObjectGUID, SamAccountName, SID, UserPrincipalName, so don't use -Properties * (although the OP also does that)
The default errorAction behavior for Get-ADComputer with an unknown identity is already STOP, but nevertheless it might be cleaner to do so indeed. The properties * is copied, but is indeed (almost) never a good practice.
Hi, thanks, but can i do - CN;Not found? Now i have just "Not found" but i need to know name for not found CN. Sorry for bad eng)
I have edited the answer to show the computer name when it is not found. Note that I store the value for computer in $computer because in the catch block the value for $_ changes to the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.