1

I have written enough PS code to go through a list of displayNames (e.g "John Smith", "Taylor Hanson" - all stored on seperate lines of a txt file) to spit back enough data into another text file that can be used for mailmerge etc. Convincing thousands of employees to simply update Windows is like breaking stones! It has to be automatted to some degree...

Here is the code... the functions that let the user open a specific text file and later save are out of view...

$displayname = @()
$names = get-content $FileIN
foreach ($name in $names) {


    $displaynamedetails = Get-ADUser -filter { DisplayName -eq $name } | Select Name, GivenName,  Surname, UserPrincipalName
    $displayname += $displaynamedetails

}

$displayname | Export-Csv -NoTypeInformation -path $fileOUT -Encoding UTF8

From time to time, a name might be spelled incorrectly in the list, or the employee may have left the organisation.

Is there any way that a statement such as 'Not Found' can be written to the specific line of the text file if an error is ever made (so that an easy side-by-side comparison of the two files can be made?

For most of the other solutions I've tried to find, the answers are based around the samAccoutName or merging the first and last names together. Here, i am specifically interested in displaynames.

Thanks

William Lombard
  • 337
  • 1
  • 3
  • 14

1 Answers1

2

You can give this a try, since -Filter or -LDAPFilter don't throw any exception whenever an object couldn't be found (unless you're feeding a null value) you can add an if condition to check if the variable where the AD User object is going to be stored is not null and if it is you can add this "not found" user into a different array.

$domain = (Get-ADRootDSE).DefaultNamingContext
$names = Get-Content $FileIN
$refNotFound = [System.Collections.Generic.List[string]]::new()

$displaynamedetails = foreach($name in $names)
{
    if($aduser = Get-ADUser -LDAPFilter "(DisplayName=$name)")
    {
        $aduser
        continue
    }

    $refNotFound.Add(
        "Cannot find an object with DisplayName: '$name' under: $domain"
    )
}

$displaynamedetails | Select-Object Name, GivenName,  Surname, UserPrincipalName |
Export-Csv -NoTypeInformation -path $fileOUT -Encoding UTF8

$refNotFound # => Here are the users that couldn't be found

Side note, consider stop using $displayname = @() and += for well known reasons.
As for AD Cmdlets, using scriptblock based filtering (-Filter {...}) is not supported and even though it can work, it can also bring you problems in the future.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • that is a fantastic help! I've just tested it. Is there any way that the PS shell output e.g "Cannot find an object with DisplayName: 'John Bob under....'' could be written to the outputted text file as well? – William Lombard Dec 15 '21 at 14:31
  • @WilliamLombard yeah it probably can be done, but I'm not sure what you mean by _"be written to the outputted text file as well"_. You want to show the message on the console or add this not found values on the text file? I'm not following – Santiago Squarzon Dec 15 '21 at 14:38
  • So that one sees the headers at the top of the file, then the details of each user was found OK and then a line in the file that indicates when a user was not found. The rationale being ithat I would like like to compare the 'input' and the 'output' file. An equal number of lines on both would be ideal. If it cannot be done programatically, I understand. – William Lombard Dec 15 '21 at 14:42
  • @WilliamLombard mmm i'm not sure I understand, do you want to modify `$FileIN` so on each line it says something like `$name + "Not Found"` or `$name + "Found"`? – Santiago Squarzon Dec 15 '21 at 14:51
  • Just to have a mark in the output file that makes it clear when a specific user (listed in the input file) couldn't be found (alongside the various details of those who were). – William Lombard Dec 15 '21 at 14:55
  • @WilliamLombard ok, you want everything on the same CSV instead of two separated files. is that so? – Santiago Squarzon Dec 15 '21 at 14:58
  • 1
    It's cool. I think I'll just manually append the lines from the console to the bottom of output file. Thanks for your help. – William Lombard Dec 15 '21 at 15:13