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
I think you meant $dcobject in the first line of the PowerShell example:
$dcobject = [adsi]”$Ldap”
$dc = $dcobject.Invoke(“GetOption”,0)
In powershell V1 that’s:
$foo.psbase.Invoke(“GetOption”,0)