2

Can anyone help me with this. I need to derive a City name from the "managedby" attribute in Active Directory which looks like this:

CN=Marley\, Bob,OU=Users,OU=PARIS,DC=Domain,DC=com

So I need to take everything out and be left with "PARIS"

I really don't know enough about Regex but assume its going to involve using -replace in some way. I have tried following some examples on the web but I just get lost. I can remove all special characters using:

'CN=Marley\, Bob,OU=Users,OU=PARIS,DC=Domain,DC=com' -replace '[\W]', ''

But I have no idea how to clean that up further.

Any help would be greatly appreciated

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
IanB
  • 271
  • 3
  • 13

2 Answers2

2

Actually you don't need regex for that. If the structure of the distinguished name is always the same you can use nested -splits ... like this:

(('CN=Marley\, Bob,OU=Users,OU=PARIS,DC=Domain,DC=com' -split '=')[3] -split ',')[0]

or this:

(('CN=Marley\, Bob,OU=Users,OU=PARIS,DC=Domain,DC=com' -split ',')[-3] -split '=')[1]

I'd recommend the second version because this way you avoid confusion you can have with commas in the CN part of the distinguished name. ;-)

If you like to do it with regex anyway you can use look-arounds to extract what's between the users OU and the domain like this:

'CN=Marley\, Bob,OU=Users,OU=PARIS,DC=Domain,DC=com' -match '(?<=Users,OU=).+(?=,DC=DOmain)'
$Matches[0]
Olaf
  • 4,690
  • 2
  • 15
  • 23
0

The following is a -replace-based solution that assumes that the city name follows the last ,OU= in the input string (though it wouldn't be hard to make the regex more specific).

It also supports city names with escaped , characters (\,), such as PARIS\, Texas.

$str = 'CN=Marley\, Bob,OU=Users,OU=PARIS\, Texas,DC=Domain,DC=com' 
# -> 'PARIS, Texas'
$str -replace '.+,OU=(.+?),DC=.+', '$1' -replace '\\,', ','
  • .+,OU= greedily matches one or more (+) arbitrary characters (.) up to the last ,OU= substring in the input string.

  • (.+?) matches on or more subsequent characters non-greedily (+?), via a capture group (capturing subexpression, (...)).

  • ,DC=.+ matches the next occurrence of substring ,DC followed by whatever is left in the string (.+).

  • Note that this means that the regex matches the entire string, so that the value of the substitution expression, $1, is the only thing returned:

    • $1 refers to the value of the 1st capture group, which contains the city name.
  • The second -replace operation unescapes the \,, i.e. turns it into , - note how the literal \ to replace had to be escaped as \\ in the regex.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • What if they had a seperate OU level for the state ... like this `'CN=Marley\, Bob,OU=Users,OU=PARIS,OU=Texas,DC=Domain,DC=com'`? ;-) :-D – Olaf Apr 19 '20 at 14:58