I am hoping you can help me with creating a simple Powershell script to query Active Directory attributes. I basically need to extract a users Logon Name (samAccountName), using an input file with Last Name, First Name and export this data to a CSV file. The biggest challenge I have with this script is to incorporate error handling. I would like to report on any errors if the user was not found in AD. The script below works for the most part but does not report on users not found in AD. It simply skips the error and does not report it in the export. Please feel free to start with the script below and rewrite it entirely.

ForEach ($User in (Get-Content D:\ADTools\Scripts\input.txt | ConvertFrom-CSV -Header LastName, FirstName))

{ $Filter = “givenName -like “”$($User.FirstName)”" -and sn -like “”$($User.LastName)“”"

Get-ADUser -Filter $Filter | Select name,samAccountName | Export-Csv “D:\ADTools\Scripts\ExportNames.csv” -Append

}

Sample Input.txt file:

Amsden,Lisa

Archer,Trina

Baker,Richard

Balderston,Daniel

Sample Export file: (If the account is not found, it simply excludes it. I would like an entry in the samAccountName column stating ‘Acct not found’ or something similar.



name



samAccountName



LISA AMSDEN



user1



TRINA ARCHER



user2



TRINA ARCHER 1



user3



RICHARD BAKER



user4



DANIEL BALDERSTON



user5



RICHARD BAUGHMAN



user6

6 Spice ups

Welcome!

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

1 Spice up

You should be able to do something like this:

ForEach ($User in (Get-Content D:\ADTools\Scripts\input.txt | ConvertFrom-CSV -Header LastName, FirstName)) {
    $Filter = "givenName -like ""*$($User.FirstName)*"" -and sn -like ""$($User.LastName)"""

    $aduser = Get-ADUser -Filter $Filter | Select name, samAccountName

    if ($aduser) {
        $aduser | Export-Csv "D:\ADTools\Scripts\ExportNames.csv" -Append
    }
    else {
        "User '$user' not found" | Export-Csv "D:\ADTools\Scripts\ExportNames.csv" -Append
    }
}

The issue is that if you use the filter, and the filter cannot find a match, the output is blank / no output.

1 Spice up
This is the error I receveid when I used the script:

Export-Csv : Cannot append CSV content to the following file: D:\ADTools\Scripts\ExportNames.csv. The appended object does not have 
a property that corresponds to the following column: name. To continue with mismatched properties, add the -Force parameter, and 
then retry the command.
At line:10 char:36
+         "User '$user' not found" | Export-Csv "D:\ADTools\Scripts\ExportNames.cs ...
+                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (name:String) [Export-Csv], InvalidOperationException
    + FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand

Export-Csv : Cannot append CSV content to the following file: D:\ADTools\Scripts\ExportNames.csv. The appended object does not have 
a property that corresponds to the following column: name. To continue with mismatched properties, add the -Force parameter, and 
then retry the command.
At line:10 char:36
+         "User '$user' not found" | Export-Csv "D:\ADTools\Scripts\ExportNames.cs ...
+                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (name:String) [Export-Csv], InvalidOperationException
    + FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand

 

how about like so:

ForEach ($User in (Get-Content D:\ADTools\Scripts\input.txt | ConvertFrom-CSV -Header LastName, FirstName)) {
    $Filter = "givenName -like ""*$($User.FirstName)*"" -and sn -like ""$($User.LastName)"""

    $aduser = Get-ADUser -Filter $Filter | Select name, samAccountName

    if ($aduser) {
        $aduser | Export-Csv "D:\ADTools\Scripts\ExportNames.csv" -Append
    }
    else {
        new-object PSCustomObject -Property @{name = "$($User.FirstName) $($User.LastName)";samaccountname = 'User not found'} |
        Export-Csv "D:\ADTools\Scripts\ExportNames.csv" -Append
    }
}
1 Spice up