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?