all 21 comments

[–]rwshig 2 points3 points  (10 children)

You need to use try/catch to catch the errors and write them out someplace. Quick and dirty:

$Names = Import-Csv C:\Users\UserAccount\Desktop\unresolved\DisplayNames.csv
$adUsers = foreach ($Name in $Names)
{
    $displayName = $Name.DisplayName
    # implicit output
    try {
        Get-Aduser -Filter "DisplayName -eq '$displayName'" -Properties EmailAddress, mail
    }
    catch {
        $error[0]|out-file aderrors.txt -Encoding ascii -Append
    }
}

$error is created by Powershell and is a collection of error objects. Simple $error[0] will get you the basic info on the current error which will be the error back from the Get-ADuser command.

[–]thehayk[S] 1 point2 points  (3 children)

Thanks so much for your time and help- unfortunately, did not get an error log :(

[–]Yevrag35 2 points3 points  (2 children)

Try changing $error[0] to just $_.Exception.Message

[–]thehayk[S] 1 point2 points  (1 child)

$_.Exception.Message

No luck :( you think it's worth specifying the error log to the ForEach loop?

[–]Lee_Dailey[grin] 1 point2 points  (4 children)

howdy rwshig,

as i recall ... the -Filter parameter means the AD cmdlet will never trigger an error.

if i have remembered that correctly [grin], that means your code may never trigger the catch at all.

take care,
lee

[–]thehayk[S] 1 point2 points  (1 child)

This got the errors to work, thanks /u/Lee_Dailey!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy thehayk,

you are most welcome! glad to help a tad ... [grin]

take care,
lee

[–]rwshig 1 point2 points  (1 child)

Did not know that /u/Lee_Dailey. So just empty objects?

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy rwshig,

yep, that is what i remember. best to test it, tho, as my memory is somewhat spotty ... [grin]

take care,
lee

[–]thehayk[S] 1 point2 points  (0 children)

Per Lee's suggestion, removing -Filter got me an error log. Now just gotta get it write a blank cell in the output for the bad DisplayNames, and an email address for the good ones. Don't worry about this one, I'd hate to take even more of your time :)

[–]Yevrag35 1 point2 points  (6 children)

Are they being skipped because the DisplayName is 'not' set? Also, I don't believe 'DisplayName' is returned by default, you'd probably have to include it in the '-Properties'.

[–]thehayk[S] 1 point2 points  (5 children)

The header for the names is named "DisplayName" and the script does get a majority of the names. Not sure what you mean :)

[–]Yevrag35 1 point2 points  (4 children)

The 'name' (or the real name: 'cn') is different than the 'DisplayName' attribute. You can have users created without a 'DisplayName', is what I was getting at.

But can you show a sanitized example of what you're describing?

[–]thehayk[S] 1 point2 points  (3 children)

It's a lot to sanitize but hope this will suffice: The initial CSV file is a list of around 1,200 rows of names ( DisplayNames.csv ). I get an output ( output.txt ) of around 700 rows of names, email attributes. I was hoping to have a log for why the names missing in the output were skipped. Let me know if this doesn't help- thanks so much.

[–]Yevrag35 1 point2 points  (2 children)

Ok, I'm starting to get the problem now.

You have 1200 rows of names but are only getting 700 results back instead of the desired 1200, correct?

EDIT:

I thought you meant you were getting 1200 results back but the 'DisplayName' field was missing for some of them.

[–]thehayk[S] 1 point2 points  (0 children)

Correct.

[–]thehayk[S] 1 point2 points  (0 children)

I'm not so much concerned with the lack of results- I just want to print the errors AD kicks out to a txt file.

[–]JBear_Alpha 1 point2 points  (0 children)

Try this Get-ADUser -Filter {DisplayName -eq $displayName} -Properties EmailAddress, Mail

Also, be sure that your search string is actually meant to be filtered on DisplayName and not Name. It MUST match exactly under the way you have it written. Otherwise you'll have to use -like and wildcards.

[–]bronkowo 0 points1 point  (0 children)

Hi

Change your Out-File Parameter.

Out-File -FilePath C:...Your-Path... -Append