Calculating domain name from an AD object's DN

Hi all

I'm trying to extract the FQDN of the domain from an AD object's DN

So I've got a an excel file with a list of DN as below:

CN=* Lastname\, * Firstname * 1st,OU=3rd Party ,OU=PBBI,OU=Offshore,DC=contoso,DC=com for example.

I've figured out a method but I'm struggling to actually implement it in powershell

My idea is as follows:

1.) Locate the first instance of "DC=" in the string

2.) Cut out the substring from the DC= until the end of the string

3.) Break the resulting substring up into domain names by splitting it at the semi colons eg

dc=contoso,dc=com becomes $namenode1 = contoso $namenode = com and then I combine them so the resultant string is contoso.com and I add that to the last column in the row.

Can anyone provide some guidance or some code samples to assist as I'm a bit stumped.

Thanks in advance

February 21st, 2015 8:23pm

$var = "CN=* Lastname\, * Firstname * 1st,OU=3rd Party ,OU=PBBI,OU=Offshore,DC=contoso,DC=com"
$var = $var -split ','
$var = $var -match 'DC='
$var = $var -replace 'DC=', ''
$var = $var -join '.'
$var

Free Windows Admin Tool Kit Click here and download it now
February 21st, 2015 10:03pm

Hope this helps you.
February 21st, 2015 10:04pm

$dn='CN=* Lastname\, Firstname,OU=3rd Party ,OU=PBBI,OU=Offshore,DC=contoso,DC=com' 
($dn -split ',DC='|where{$_ -notmatch '='}) -join '.'

Free Windows Admin Tool Kit Click here and download it now
February 21st, 2015 10:04pm

Thanks jvr I think that will crack it!!
February 21st, 2015 10:29pm

You are welcome.  Split is very handy as a starting point for breaking up strings.

Free Windows Admin Tool Kit Click here and download it now
February 21st, 2015 10:41pm

It worked like a charm!! I had to mess around with the logic of the rest of script but your section was 100%.

I've not any script or code writing of this type in ages so my skills are very rusty!!

Thanks for the assist from South Africa and enjoy the rest of your weekend.

February 21st, 2015 11:51pm

Great!  I hope it is warm where you are.  We are freezing on the north end of the planet.
Free Windows Admin Tool Kit Click here and download it now
February 21st, 2015 11:53pm

It's 11 pm and I'm sitting here in shorts & no shirt.. :) So yes you could say it's warm. It topped out about 32 degrees Centigrade.

Where you at??

February 21st, 2015 11:59pm

In the land of perpetual snow and ice.  New York.  We had less snow and warmer temperatures when I was in Antarctica for two summers. It i
Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2015 12:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics