0

I need to extract the element from my Distinguished Name into array,and except the DC elemnet.

for exemple :"CN=TestGroup,OU=Groups,OU=UT-SLC,OU=US,DC=Company,DC=com"

I need to have array like below

Name Value
CN TestGroup
OU Groups
OU UT-SLC
OU US

Can someone help me?

Daniel shi
  • 27
  • 3
  • 1
    `'CN=TestGroup,OU=Groups,OU=UT-SLC,OU=US,DC=Company,DC=com' -Split ',' |ConvertFrom-StringData` – iRon Jan 07 '22 at 09:26
  • 2
    @iRon DNs can contain escaped commas `\,` in some odd cases. `'CN=Test\, Group,OU=Groups,OU=UT-SLC,OU=US,DC=Company,DC=com' -split '(?<!\\),' |ConvertFrom-StringData` works nice for those cases. – Santiago Squarzon Jan 07 '22 at 13:54
  • 1
    @Santiago, has a point. But if we going to look deeper into this anyways. You also want to take care of the situation where a name ends with a backslash (escaped: `Name\\ `) where the following comma (`,`) is a valid separator. See: [(PowerShell) split string with escaped separator characters](https://stackoverflow.com/a/22249126/1701026) – iRon Jan 07 '22 at 16:04
  • @iRon thanks for that one, I'll save it on my bookmarks hehe – Santiago Squarzon Jan 07 '22 at 16:07

1 Answers1

1

I don't know if this what you are looking for, please check it

$x= "CN=TestGroup,OU=Groups,OU=UT-SLC,OU=US,DC=Company,DC=com"
$y = $x.Split(',')
$z = foreach ($line in $y){
[pscustomobject]@{Name = $line.Split('=')[0]; Value=$line.Split('=')[1]}
}
$z | where {$_.name -ne "DC"}
Marwa Fadl
  • 11
  • 2
  • Nice, but as @Olaf mentioned user should submit his code, either working or not, to help him instead of giving him the answer like that. – Mahmoud Moawad Jan 07 '22 at 07:53
  • 1
    yes sorry, but i don't have enough votes to comment on his post to direct him the correct way – Marwa Fadl Jan 07 '22 at 07:55
  • According to his history on SO he seems not interested in a dialog ... he seems to never answer to comments or even answers. – Olaf Jan 07 '22 at 08:08
  • @olaf, yes you are right – Marwa Fadl Jan 07 '22 at 08:12
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 07 '22 at 08:44