none
Escaping forward slashes in the output of a variable? RRS feed

  • Question

  • Hey there,

    I have a script which generates a variable array from a list of existing AD groups. Some of those groups have forward slashes(/) in them which throws an error when I attempt to process one of those groups. How can I escape the result of a variable without breaking the variable itself? For instance my script pulls into the variable a group called Dumb/Idea.254, but when the next command uses the variable it fails because the / breaks the rest. What do I need to do to process the variable as-is regardless of what characters are in the names? So that "$group = Dumb/Idea.254" processes as well as "$group = Idea1"?

    Thanks!

    Friday, November 6, 2015 10:43 PM

All replies

  • Escape to a new variable.

    $var = 'Dumb/Idea.254'
    $var -replace '/','/2f'


    \_(ツ)_/


    Friday, November 6, 2015 10:56 PM
    Moderator
  • Actually forward slashes are not an issue with AD Cmdlets. Theu are only an issue if you use the "search" API.

    PS C:\scripts> get-adgroup -filter 'name -eq "test/grp3"'


    DistinguishedName : CN=Test/Grp3,OU=TestOU,DC=TESTNET,DC=local
    GroupCategory     : Security
    GroupScope        : Global
    Name              : Test/Grp3
    ObjectClass       : group
    ObjectGUID        : 735760a9-0e6d-4aae-9004-9c7859f877f7
    SamAccountName    : Test_Grp3
    SID               : S-1-5-21-1997746983-321353523-153608166-2201


    \_(ツ)_/

    Friday, November 6, 2015 11:11 PM
    Moderator
  • The other method is to use the SAmAccountName which cannot contain that character.If the name was auto-genertated then AD will have replaces it with  an underscore.

    Get-AdGroup 'Dumb_Idea.254'


    \_(ツ)_/

    Friday, November 6, 2015 11:14 PM
    Moderator
  • I tried that out and had no luck. Here's the snippet I use to get the groups with the -replace included where I put it, maybe it's not in the right sequence?

    Foreach($term in $terms){ Try {
    #Gets an array of the user's group membership into a variable $groups = Get-ADUser $term -property MemberOf | % {$_.MemberOf | Get-AdGroup | select Name | sort name} | foreach { $_.Name } #Removes the user from all groups foreach ($group in $groups) { $group -replace '/','/2f' Remove-ADGroupMember $group -Members $term -Confirm:$false }




    • Edited by Blasteau Friday, November 6, 2015 11:19 PM
    Friday, November 6, 2015 11:18 PM
  • Note that even DistinguishedName has no issues.

    PS C:\scripts> get-adgroup 'CN=Test/Grp3,OU=TestOU,DC=TESTNET,DC=local'


    DistinguishedName : CN=Test/Grp3,OU=TestOU,DC=KAHLNET,DC=local
    GroupCategory     : Security
    GroupScope        : Global
    Name              : Test/Grp3
    ObjectClass       : group
    ObjectGUID        : 735760a9-0e6d-4aae-9004-9c7859f877f7
    SamAccountName    : Test_Grp3
    SID               : S-1-5-21-1997746983-321388823-153608166-2201


    \_(ツ)_/

    Friday, November 6, 2015 11:20 PM
    Moderator
  • I tried that out and had no luck. Here's the snippet I use to get the groups with the -replace included where I put it, maybe it's not in the right sequence?

    Foreach($term in $terms){ Try {
    #Gets an array of the user's group membership into a variable $groups = Get-ADUser $term -property MemberOf | % {$_.MemberOf | Get-AdGroup | select Name | sort name} | foreach { $_.Name } #Removes the user from all groups foreach ($group in $groups) { $group -replace '/','/2f' Remove-ADGroupMember $group -Members $term -Confirm:$false }




    You cannot use the name as a samname.  You can do this:

     $gname='Test/Grp3'
     get-adgroup -filter "Name -eq '$gname'"

    $gname='Test/Grp3'
    $fixed = $gname -replace '/','_'
    Get-AdGroupMember $fixed


    \_(ツ)_/

    Friday, November 6, 2015 11:28 PM
    Moderator
  • Here's the error:
    Error: Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException
    Error: Cannot find an object with identity: 'Dumb/Idea Team' under: 'DC=domain,DC=net'.

    It's generated when running Get-ADGroup -Identity "Dumb/Idea Team" or if I have that group in the $groups variable.

    Friday, November 6, 2015 11:30 PM
  • And finally we can do this:

     get-adgroup -filter "Name -eq '$gname'" | Get-AdGroupMember


    \_(ツ)_/

    Friday, November 6, 2015 11:30 PM
    Moderator
  • The forward slash character can be escaped with the backslash escape character. More info on what must be escaped, including in PowerShell, here:

    http://social.technet.microsoft.com/wiki/contents/articles/5312.active-directory-characters-to-escape.aspx


    Richard Mueller - MVP Directory Services

    Saturday, November 7, 2015 12:20 AM