如何使用Regex通过PowerShell从专有名称中仅提取CN [英] How can I use Regex to pull just the CN from a Distinguished Name with PowerShell

查看:94
本文介绍了如何使用Regex通过PowerShell从专有名称中仅提取CN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆字符串,它们是AD中组的DN.我需要拔出通用名称". 一个示例字符串是"CN =我想要的组名,OU =组容器,DC = corp,DC = test,DC =本地"

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"

我正在寻找的是一些PowerShell代码,该代码将从该字符串中提取我想要的组名",并丢弃其余的代码.

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

我可以以此撕裂CN

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

但是在那之后,我没有一个很好的方法来摆脱以,OU"开头的所有内容

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.

推荐答案

$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s -replace "(CN=)(.*?),.*",'$2'

这篇关于如何使用Regex通过PowerShell从专有名称中仅提取CN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆