如何使用 Regex 通过 PowerShell 从专有名称中提取 CN

发布于 2024-12-24 23:43:34 字数 372 浏览 0 评论 0原文

我有一堆字符串,它们是来自 AD 的组的 DN。我需要提取通用名称。 示例字符串是“CN =我想要的组名称,OU =组容器,DC = corp,DC = test,DC =本地”

我正在寻找的是一些PowerShell代码,它将从中提取“我想要的组名称”串并丢弃其余部分。

我可以用这个撕掉 CN

$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s = $s.Remove(0,3) 

但在那之后,我没有一个好的方法来撕掉从“,OU”开始的所有内容,

我确信有一些正则表达式可以做到这一点,但我需要一些帮助来解决它。

I have a bunch of strings that are DN's of groups from AD. I need to pull out the Common Name.
An example string is "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"

What I am looking for is some PowerShell Code that will pull "Group Name I Want" out of that string and discard the rest.

I can rip of the CN with this

$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s = $s.Remove(0,3) 

But after that, I don't have a good way to rip off everthing starting at ",OU"

I am sure there is some regex that will do this but I need some help figuring it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

或十年 2024-12-31 23:43:35
$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s -replace "(CN=)(.*?),.*",'$2'
$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s -replace "(CN=)(.*?),.*",'$2'
惟欲睡 2024-12-31 23:43:35

jon Z 的答案的更短版本:

$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s = $s -replace '^CN=|,.*

^ $ 是字符串的开始和结束锚点。 | 是一个“或”。

匹配项是行开头的 CN= 或以逗号开头并到达行末尾的字符串(即 CN 之后的所有内容)。 replace 不替换任何内容,因此您将丢弃所有匹配项,只留下 CN 本身。

如果您的 CN 中有逗号,那么显然这不起作用(呃)。

假设这样的逗号后面跟着一个空格,这将工作并且适用于前面的示例(\S - 非空白字符):

$s = $s -replace '^CN=|,\S.*

我测试了是否 jon Z 或此变体执行速度更快。有 1,470,000 个 DN,第一个执行时间为 36.87 秒,而这里的执行时间为 34.75 秒。其中并不是很多。

那是从文件中读取 DN。奇怪的是,将文件“slurping”到一个数组中并执行它,这两个正则表达式都多花了一分钟的时间。 PC 不受内存限制 - 文件只有 100MB。现在我懒得去探究这个问题的真相!

^ $ 是字符串的开始和结束锚点。 | 是一个“或”。

匹配项是行开头的 CN= 或以逗号开头并到达行末尾的字符串(即 CN 之后的所有内容)。 replace 不替换任何内容,因此您将丢弃所有匹配项,只留下 CN 本身。

如果您的 CN 中有逗号,那么显然这不起作用(呃)。

假设这样的逗号后面跟着一个空格,这将工作并且适用于前面的示例(\S - 非空白字符):


我测试了是否 jon Z 或此变体执行速度更快。有 1,470,000 个 DN,第一个执行时间为 36.87 秒,而这里的执行时间为 34.75 秒。其中并不是很多。

那是从文件中读取 DN。奇怪的是,将文件“slurping”到一个数组中并执行它,这两个正则表达式都多花了一分钟的时间。 PC 不受内存限制 - 文件只有 100MB。现在我懒得去探究这个问题的真相!

我测试了是否 jon Z 或此变体执行速度更快。有 1,470,000 个 DN,第一个执行时间为 36.87 秒,而这里的执行时间为 34.75 秒。其中并不是很多。

那是从文件中读取 DN。奇怪的是,将文件“slurping”到一个数组中并执行它,这两个正则表达式都多花了一分钟的时间。 PC 不受内存限制 - 文件只有 100MB。现在我懒得去探究这个问题的真相!

^$ 是字符串的开始和结束锚点。 | 是一个“或”。

匹配项是行开头的 CN= 或以逗号开头并到达行末尾的字符串(即 CN 之后的所有内容)。 replace 不替换任何内容,因此您将丢弃所有匹配项,只留下 CN 本身。

如果您的 CN 中有逗号,那么显然这不起作用(呃)。

假设这样的逗号后面跟着一个空格,这将工作并且适用于前面的示例(\S - 非空白字符):

我测试了是否 jon Z 或此变体执行速度更快。有 1,470,000 个 DN,第一个执行时间为 36.87 秒,而这里的执行时间为 34.75 秒。其中并不是很多。

那是从文件中读取 DN。奇怪的是,将文件“slurping”到一个数组中并执行它,这两个正则表达式都多花了一分钟的时间。 PC 不受内存限制 - 文件只有 100MB。现在我懒得去探究这个问题的真相!

An even shorter variation on jon Z's answer:

$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s = $s -replace '^CN=|,.*

The ^ and $ are the string beginning and end anchors. The | is an "or".

The matches are the CN= at the beginning of the line or a string that starts with a comma and goes to the end of the line (i.e. everything after the CN). The replace is replacing with nothing, so you're discarding all the matches and leaving just the CN itself.

That obviously does not work if you have a comma in your CN (ugh).

Assuming such a comma is followed by a space, this will work and be fine for the previous examples (\S - non whitespace char):

$s = $s -replace '^CN=|,\S.*

I tested to see if jon Z's or this variation was faster to execute. With 1,470,000 DNs, the first took 36.87s to execute and the one here took 34.75. Not really a lot in it.

That was reading the DNs out of a file. Bizarrely, "slurping" the file into an array and executing over that took both regexes a minute longer. The PC was not memory-bound - the file was only 100MB. Can't be bothered getting to the bottom of that one right now!

The ^ and $ are the string beginning and end anchors. The | is an "or".

The matches are the CN= at the beginning of the line or a string that starts with a comma and goes to the end of the line (i.e. everything after the CN). The replace is replacing with nothing, so you're discarding all the matches and leaving just the CN itself.

That obviously does not work if you have a comma in your CN (ugh).

Assuming such a comma is followed by a space, this will work and be fine for the previous examples (\S - non whitespace char):


I tested to see if jon Z's or this variation was faster to execute. With 1,470,000 DNs, the first took 36.87s to execute and the one here took 34.75. Not really a lot in it.

That was reading the DNs out of a file. Bizarrely, "slurping" the file into an array and executing over that took both regexes a minute longer. The PC was not memory-bound - the file was only 100MB. Can't be bothered getting to the bottom of that one right now!

I tested to see if jon Z's or this variation was faster to execute. With 1,470,000 DNs, the first took 36.87s to execute and the one here took 34.75. Not really a lot in it.

That was reading the DNs out of a file. Bizarrely, "slurping" the file into an array and executing over that took both regexes a minute longer. The PC was not memory-bound - the file was only 100MB. Can't be bothered getting to the bottom of that one right now!

The ^ and $ are the string beginning and end anchors. The | is an "or".

The matches are the CN= at the beginning of the line or a string that starts with a comma and goes to the end of the line (i.e. everything after the CN). The replace is replacing with nothing, so you're discarding all the matches and leaving just the CN itself.

That obviously does not work if you have a comma in your CN (ugh).

Assuming such a comma is followed by a space, this will work and be fine for the previous examples (\S - non whitespace char):

I tested to see if jon Z's or this variation was faster to execute. With 1,470,000 DNs, the first took 36.87s to execute and the one here took 34.75. Not really a lot in it.

That was reading the DNs out of a file. Bizarrely, "slurping" the file into an array and executing over that took both regexes a minute longer. The PC was not memory-bound - the file was only 100MB. Can't be bothered getting to the bottom of that one right now!

安静 2024-12-31 23:43:35
$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s1 = ($s -split ",*..=")[1]
$s1
$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s1 = ($s -split ",*..=")[1]
$s1
魔法少女 2024-12-31 23:43:35
$s = "CN=Hall\, Jeremy,OU=Group Container,DC=corp,DC=test,DC=local"
$s1 = ($s -split ",*..=")[1]
$s1

这对我有用 - 但是,我在名称输出中留下了一个 \。 (我从 get-aduser 函数中提取 manager 属性,它会打印尊贵的名称。Hall\,Jeremy 是我剩下的一个例子。

有人想尝试一下吗?

$s = "CN=Hall\, Jeremy,OU=Group Container,DC=corp,DC=test,DC=local"
$s1 = ($s -split ",*..=")[1]
$s1

This works for me - however, I have a \, left in name output. (I'm pulling the manager property from the get-aduser function and it prints the distinguishedName. Hall\, Jeremy is one example of what I'm left with.

Anyone want to take a stab?

∞梦里开花 2024-12-31 23:43:35

使用该线程中的各种答案,我得出以下结论:

$s = ($s -split ",*..=")[1]

$s = $s.Remove($s.IndexOf("\" ),1)

$s

Using the various answers in this thread, I came up with the following:

$s = ($s -split ",*..=")[1]

$s = $s.Remove($s.IndexOf("\"),1)

$s

我ぃ本無心為│何有愛 2024-12-31 23:43:35
$DN = Get-ADUser Username -Properties * | Select DistinguishedName | ForEach-Object {($_.DistinguishedName) -replace('\\','')}
$OUTemp = $DN -creplace "^[^,]*,",""
$OU = $OUTemp -creplace "^[^,]*,",""
$OU

经过大量搜索后,这对我有用。我对第 2 行和第 3 行的冗余并不感到自豪,但我不知道如何使其在单行上工作。

当 \ 转义字符存在时,第一行末尾的“-replace”负责处理格式化为 CN=LastName\, FirstName 的 DistinguishedNames。如果没有它,它不会对名称产生负面影响。

我的 $OU 的结果是 OU=ChildOU2,OU=ChildOU1,OU=ParentOU,DC=DOMAIN,DC=local

$DN = Get-ADUser Username -Properties * | Select DistinguishedName | ForEach-Object {($_.DistinguishedName) -replace('\\','')}
$OUTemp = $DN -creplace "^[^,]*,",""
$OU = $OUTemp -creplace "^[^,]*,",""
$OU

This worked for me after lots of searching. I am not proud of the redundancy in lines 2 and 3 but I don't know how to make it work on a single line.

The "-replace" at the end of the first line took care of DistinguishedNames that were formatted as CN=LastName\, FirstName when the \ escape character was present. It doesn't negatviely affect at name without out it.

The results of my $OU is OU=ChildOU2,OU=ChildOU1,OU=ParentOU,DC=DOMAIN,DC=local

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文