1

I have a list of EN/FR users that have special characters in them. I am trying to get their SamAccountName but the script doesn't work correctly:

Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear();
cls
$ErrorActionPreference = "STOP"
$names = Import-CSV 'C:\temp\input.csv' -Header Givenname,Surname -Delimiter ","
$CompleteReport=@()
ForEach ($Name in $Names)
{
    $FirstFilter = $Name.Givenname
    $SecondFilter = $Name.Surname
    Write-Host $Name.Givenname, $Name.Surname
    $aduser = Get-ADUser -Filter { GivenName -like $FirstFilter -and Surname -like $SecondFilter}  | select enabled, GivenName,  Surname, samaccountname, UserPrincipalName
    $CompleteReport = $CompleteReport+$aduser
} 
$CompleteReport | Export-Csv 'C:\output.csv' -NoTypeInformation

The problem is my input list is not clear and that causes the loop keep on going without showing me the error on the item causing the issue. How can I catch the user name that is causing this error?

FalconRider
  • 75
  • 2
  • 9
  • you're using `-like` but no wildcards, are you looking for an exact match between `GivenName` and `SurName` ? – Santiago Squarzon Feb 12 '22 at 04:31
  • Yes, the file contains two columns that has Firstname and Last name. some missing the last name or can't find the item in AD. I can't figure out how to capture those and skip the bad inputs so I can manually look at them later... – FalconRider Feb 12 '22 at 04:35
  • Please open your input csv file in notepad, copy the first 3 or 4 lines and paste that in your question as formatted text. It looks like your file has column names `Legal_First_Name` and `Legal_Last_Name` and because your code specifies different headers, the existing headers are seen as data. That can only fail then because nobody will have a first name 'Legal_First_Name' I think... – Theo Feb 12 '22 at 19:09

1 Answers1

0

Might be possible that the error comes from empty strings on either GivenName or Surname from your CSV, as for sorting that problem, there is no way around fixing the CSV itself, searching AD Users by GivenName or Surname only will not bring accurate results. The following should at least handle the missing data from your CSV and point out those users that couldn't be found so you can fix your CSV.

$ErrorActionPreference = "STOP"
# -Delimiter ',' is already assumed
$names = Import-CSV 'C:\temp\input.csv' -Header Givenname, Surname 

$result = forEach($name in $names) {
    $givenname = if($name.GivenName) {
        '(givenName={0})' -f $name.Givenname
    }
    $surname = if($name.Surname) {
        '(surname={0})' -f $name.Surname
    }
    $aduser = Get-ADUser -LDAPFilter "(&${surname}${givenname})"
    if(-not $aduser) {
        Write-Warning "Can't find Name: $($name.Givenname) and Surname: $($name.Surname)"
        continue
    }
    $aduser
}

$result | Select-Object Enabled, GivenName, Surname, SamAccountName, UserPrincipalName |
Export-Csv 'C:\output.csv' -NoTypeInformation
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • it is erroring out: ``` WARNING: Can't find Name: Legal_First_Name and Surname: Legal_Last_Name WARNING: Can't find Name: John Mike and Surname: Doe ``` – FalconRider Feb 12 '22 at 05:03