1

This is a snippet of a much larger script. When the user enters a Last name in the field that contains an apostrophe it will not find the user unless they enter 2 apostrophes. How can I remedy this?

function getacctname {
    $fname = $FirstName.Text
    $lname = $LastName.Text
    Try {
     $User.Text = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" -Properties physicalDeliveryOfficeName |
     Select-Object 'SamAccountName',@{Name='Office';Expression={$_.physicalDeliveryOfficeName}} |
     Out-Gridview  -Title 'Windows Logon' -PassThru | Select-Object -Expand SAMAccountName
           $Email.Text = (Get-ADUser $User.text -Properties mail).mail
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
PCMedicJAX
  • 79
  • 8
  • 2
    You mean if they have a surname like `O'Connor` then your query breaks? If so, you can use `.Replace("'", "''")` before passing the filter to the AD Provider – Santiago Squarzon Feb 03 '23 at 14:31
  • Another option is to use escaped embedded `"` chars: ``-Filter "GivenName -eq `"$fname`" -and SurName -eq `"$lname`""``. Although generally not advisable for conceptual reasons (and technically it won't work with implicit remoting), using single-quoting overall and letting the AD provider resolve the variable references may work too: ``-Filter 'GivenName -eq $fname -and SurName -eq $lname`` - see [this answer](https://stackoverflow.com/a/44184818/45375) for more info. – mklement0 Feb 03 '23 at 14:59

0 Answers0