Hi,
Basically I use this part of the script to get the samAccountName from the Display Name. Was working great until I tried a user an apostrophe in their surname.
$PersonOnLeaveValue = $PersonOnLeave.SelectedItem $PersonOnLeaveUsername = (Get-ADUser -Filter "Name -like '$PersonOnLeaveValue'" -Properties *).samAccountName
No Apostrophe:
PS C:\> $PersonOnLeaveValue = "General User"
PS C:\> $PersonOnLeaveUsername = (Get-ADUser -Filter "Name -like '$PersonOnLeaveValue'" -Properties *).samAccountName
PS C:\> echo $PersonOnLeaveUsername
generalu
With An Apostrophe:
PS C:\> $PersonOnLeaveValue = "FirstName O'LastName"
PS C:\> $PersonOnLeaveUsername = (Get-ADUser -Filter "Name -like '$PersonOnLeaveValue'" -Properties *).samAccountName
Get-ADUser : Error parsing query: 'Name -like 'FirstName O'LastName'' Error Message: 'syntax error' at position: '24'.
At line:1 char:27
+ $PersonOnLeaveUsername = (Get-ADUser -Filter "Name -like '$PersonOnLeaveValue'" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
I've tried a number of different variations to the (Get-ADUser -Filter "Name -like '$PersonOnLeaveValue'" -Properties *).samAccountName with single and double quotes etc, but can't seem to find one that works.
I am happy to find the samAccountName another way if someone has a better solution.
Thanks :)
Update:$PersonOnLeaveUsername = (Get-ADUser -Filter {Name -like "$PersonOnLeaveValue"} -Properties *).samAccountNameThis seems to be working a little better. Getting a blank value at the moment. Did work in one of my manual tests though. Just looking for what has changed
- Edited by Grubsy 7 minutes ago updated