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

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

问题描述

我有一堆字符串,它们是来自 AD 的组的 DN.我需要提取通用名称.示例字符串是CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"

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'

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

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