locked
Error when working with apostrophe in name when creating groups RRS feed

  • Question

  • I am creating bulk group and one of the fields I need to populate is the "Managed By" field. when I am creating the groups one of the names I have to use for managed by is John O'Brien. 

    This is what I get: Error parsing query: 'name -like 'John O'Brien'' Error Message: 'syntax error'. For some reason if I use samAccountname the field doesn't get populated

    Is there a way to work around for this problem?

    Wednesday, October 28, 2020 6:02 PM

All replies

  • I am creating bulk group and one of the fields I need to populate is the "Managed By" field. when I am creating the groups one of the names I have to use for managed by is John O'Brien. 

    This is what I get: Error parsing query: 'name -like 'John O'Brien'' Error Message: 'syntax error'. For some reason if I use samAccountname the field doesn't get populated

    Is there a way to work around for this problem?

    i did not see your code

    The opinion expressed by me is not an official position of Microsoft

    Wednesday, October 28, 2020 6:04 PM
  • How about this?

    $x | where {$_.name -like "*John O'Brian*"}

    FYI, you need at least one wildcard for the -like operator to work correctly (or at all, in fact).


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, October 28, 2020 6:28 PM
  • $FilePath = "DC:\Scripts\Bulk-Group.csv" $csv = import-csv $FilePath $csv | Foreach-Object { $_.PSObject.Properties | Foreach-Object { $_.Value = $_.Value.Trim()} } $csv | Export-Csv $FilePath -NoTypeInformation #Create new groups $path = "OU=PermissionGroups,OU=Groups,DC=Domain,DC=com" $GroupScope = "Global" $GroupCategory = "Security" Import-csv $FilePath | foreach{ New-ADGroup -Path $Path -GroupScope $GroupScope -GroupCategory $GroupCategory -ManagedBy (Get-aduser -Filter "name -like '$($_.GroupManagedby)'" -properties *).SamAccountName -SamAccountName $_.GroupName -Name $_.GroupName -Description $_.GroupDescription -OtherAttributes @{'info'=$_.Groupinfo1} -PassThru -Verbose } $csv |%{Get-ADGroup $_.GroupName -Properties * | select Name,Managedby,Description,WhencReated,info} | fl

    Here is the csv file structure:

    GroupName,GroupManagedby,GroupDescription,GroupInfo1
    SG-VPN-AL-PROD,John O'Brien,This group will provide access to the project application over VPN.,TCK7690654
    Thursday, October 29, 2020 3:34 PM
  • one way described by Rich

    another

      -Filter "name -like ""$($_.GroupManagedby)"""


    The opinion expressed by me is not an official position of Microsoft

    Thursday, October 29, 2020 4:58 PM
  • one way described by Rich

    another

      -Filter "name -like ""$($_.GroupManagedby)"""


    The opinion expressed by me is not an official position of Microsoft

    In a filter do not use double-double quotes.

     -Filter "name -like '$($_.GroupManagedby)'"

    Single quotes are only seen as basic characters when used inside of double-quotes.  Using compound quotes to escape a quote in a filter can have odd side effects.


    \_(ツ)_/

    Thursday, October 29, 2020 8:40 PM
  • one way described by Rich

    another

      -Filter "name -like ""$($_.GroupManagedby)"""


    The opinion expressed by me is not an official position of Microsoft

    In a filter do not use double-double quotes.

     -Filter "name -like '$($_.GroupManagedby)'"

    Single quotes are only seen as basic characters when used inside of double-quotes.  Using compound quotes to escape a quote in a filter can have odd side effects.


    \_(ツ)_/

    jrv, your solution come from the question and author said that single quotes does not work when GroupManagedBy person have single quote in a name, like O'Brien

    looks like the best way proposed by Rich, my solution should work for this case but i'magree that this is odd


    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Thursday, October 29, 2020 9:10 PM
    Thursday, October 29, 2020 9:08 PM
  • The single quote always works when the name is in a variable.  If the OP is trying to say different then they are not telling us something about what they are doing and why.

    See the following:

    PS C:\scripts> $GroupManagedby = "M'Debon"
    PS C:\scripts> "name -like '$($GroupManagedby)'"
    name -like 'M'Debon'
    PS C:\scripts>

    Objects in AD other than user names should NOT use special characters.


    \_(ツ)_/

    Friday, October 30, 2020 3:03 AM
  • The single quote always works when the name is in a variable.  If the OP is trying to say different then they are not telling us something about what they are doing and why.

    See the following:

    PS C:\scripts> $GroupManagedby = "M'Debon"
    PS C:\scripts> "name -like '$($GroupManagedby)'"
    name -like 'M'Debon'
    PS C:\scripts>

    Objects in AD other than user names should NOT use special characters.


    \_(ツ)_/

    jrv, did u try this filter with ad cmdlets, like get-aduser?

    The opinion expressed by me is not an official position of Microsoft

    Friday, October 30, 2020 7:46 AM