joeware - never stop exploring... :)

Information about joeware mixed with wild and crazy opinions...

So I used Serverless Binding with ADSI (or .NET), now what DC am I talking to??

by @ 6:33 pm on 10/1/2009. Filed under tech

This is something that people occasionally want to do. There are two basic answers that I am aware of. The first I always remember right off since I am an LDAP API coder, is to look at the dnsHostName attribute of the rootdse of the server you are connected to. That is what AdFind and AdMod do when you see the lines

Using server: JOEWARE-DC1.joeware.local:389
Directory: Windows Server 2003

The other way which is ADSI specific and I spent an hour trying to recall today when asked is to use the ADSI GetOption method (IADsObjectOptions::GetOption) to retrieve the ADS_OPTION_SERVERNAME value. I actually have this in an example in my book that lists ACEs in an ACL.

Examples:

VBScript:

Const ADS_OPTION_SERVERNAME=0
‘****************************************************************************
‘Bind to object
‘****************************************************************************
Out "Opening object – " & strLDAPPath
Set objObject = GetObject(strLDAPPath)
strDC = objObject.GetOption(ADS_OPTION_SERVERNAME)

PowerShell (no not me, Brandon gave this to me…)

$dcobject = [adsi]"$Ldap"
$dc = $dcobject.Invoke("GetOption",0)

[ Correction: Quick thanks to Mike for pointing out Brandon’s typo so I could correct it. Brandon obviously meant $dcobject= and not $object= in line 1. He is very sorry to everyone for the typo and he will buy you a cup of coffee the next time he sees you all.  ;o) ]

.NET (again not me, but from a post by Mr. DS.NET programming… Joe Kaplan)

const int ADS_OPTION_SERVERNAME = 0; 
object server = entry.Invoke("GetOption", new object[] {ADS_OPTION_SERVERNAME});

 

  joe

Rating 3.00 out of 5

2 Responses to “So I used Serverless Binding with ADSI (or .NET), now what DC am I talking to??”

  1. Mike Shepard says:

    I think you meant $dcobject in the first line of the PowerShell example:

    $dcobject = [adsi]”$Ldap”
    $dc = $dcobject.Invoke(“GetOption”,0)

  2. Slipsec says:

    In powershell V1 that’s:

    $foo.psbase.Invoke(“GetOption”,0)

[joeware – never stop exploring… :) is proudly powered by WordPress.]