Dohko
Dohko

Reputation: 21

Keep DC part of distinguished name

I have for input distinguished names like the following:

CN=A00.user,OU=MyOU,OU=A00,OU=MyOU3,DC=my,DC=domain
CN=A01.user1,OU=MyOU1,OU-MyOU2,OU=A00,OU=MyOU3,DC=my,DC=first,DC=domain

I need to print only the DC part, to get an output like:

my.domain
my.first.domain

Looks like split or replace should work, but I'm having trouble figuring out the syntax.

Upvotes: 1

Views: 2124

Answers (3)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

I would simply remove everything up to and including the first ,DC= and then replace the remaining ,DC= with dots.

$dn = 'CN=A00.user,OU=MyOU,OU=A00,OU=MyOU3,DC=my,DC=domain',
      'CN=A01.user1,OU=MyOU1,OU-MyOU2,OU=A00,OU=MyOU3,DC=my,DC=first,DC=domain'

$dn -replace '^.*?,dc=' -replace ',dc=', '.'

Upvotes: 0

Bill_Stewart
Bill_Stewart

Reputation: 24525

You can use Get-ADPathname.ps1 with the -Split parameter, Select-String with a regular expression, and the -join operator:

(
  Get-ADPathname 'CN=A01.user1,OU=MyOU1,OU-MyOU2,OU=A00,OU=MyOU3,DC=my,DC=first,DC=domain' -Split | Select-String '^DC=(.+)' | ForEach-Object {
    $_.Matches[0].Groups[1].Value
  }
) -join '.'

Output:

my.first.domain

Upvotes: 2

Coding101
Coding101

Reputation: 531

Here's a quick and dirty way to get it done.

("CN=A00.user,OU=MyOU,OU=A00,OU=MyOU3,DC=my,DC=domain " -split "," | 
Where-Object { $_.StartsWith("DC=") } | 
ForEach-Object { $_.Replace("DC=","")}) -join "."

Produces my.domain

Upvotes: 1

Related Questions