可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s -replace "(CN=)(.*?),.*",'$2'
回答2:
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!
回答3:
$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s1 = ($s -split ",*..=")[1]
$s1
回答4:
$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?
回答5:
$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
回答6:
Using the various answers in this thread, I came up with the following:
$s = ($s -split ",*..=")[1]
$s = $s.Remove($s.IndexOf("\"),1)
$s