简体   繁体   English

PowerShell 中的正则表达式从 Active Directory 中的 Managedby 属性获取城市名称

[英]Regex in PowerShell to get the city name from the Managedby property in Active Directory

Can anyone help me with this.谁能帮我这个。 I need to derive a City name from the "managedby" attribute in Active Directory which looks like this:我需要从 Active Directory 中的“managedby”属性派生一个城市名称,如下所示:

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

So I need to take everything out and be left with "PARIS"所以我需要把所有东西都拿出来,留下"PARIS"

I really don't know enough about Regex but assume its going to involve using -replace in some way.我真的对正则表达式知之甚少,但假设它会以某种方式涉及使用 -replace。 I have tried following some examples on the web but I just get lost.我曾尝试在 web 上遵循一些示例,但我只是迷路了。 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任何帮助将不胜感激

Actually you don't need regex for that.实际上,您不需要正则表达式。 If the structure of the distinguished name is always the same you can use nested -split s... like this:如果可分辨名称的结构始终相同,则可以使用嵌套的-split s...,如下所示:

(('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.我推荐第二个版本,因为这样可以避免混淆名称的 CN 部分中的逗号。 ;-) ;-)

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:如果你喜欢用正则表达式来做,你可以使用环视来提取用户 OU 和域之间的内容,如下所示:

'CN=Marley\, Bob,OU=Users,OU=PARIS,DC=Domain,DC=com' -match '(?<=Users,OU=).+(?=,DC=DOmain)'
$Matches[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).以下是基于-replace的解决方案,它假定城市名称跟在输入字符串中的最后一个,OU=之后(尽管使正则表达式更具体并不难)。

It also supports city names with escaped , characters ( \, ), such as PARIS\, Texas .它还支持带有转义字符 ( \, ) 的城市名称,例如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. .+,OU=贪婪地匹配一个或多个 ( + ) 任意字符 ( . ) 直到输入字符串中的最后一个,OU= substring。

  • (.+?) 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 ( .+ ). ,DC=.+匹配下一次出现的 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ ,DC后跟字符串中剩下的任何内容 ( .+ )。

  • 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是唯一返回的内容:

    • $1 refers to the value of the 1st capture group, which contains the city name. $1是指第一个捕获组的值,其中包含城市名称。
  • The second -replace operation unescapes the \, , ie turns it into , - note how the literal \ to replace had to be escaped as \\ in the regex.第二个-replace操作取消了\,的转义,即把它变成, - 注意要替换的文字\如何必须在正则表达式中转义为\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM