Torgeir
Wed Jul 28 11:42:36 CDT 2004
Net Coder wrote:
> I'm trying to bind to DC by site. The environment is a AD 2K3 domain
> with multiple sites.
>
> For example, when my application is started, it checks for the site of
> the computer where the application is running. Then, it looks for a DC
> in the site and then all DirectorySearcher and DIrectoryEntry operations
> will use a binding string aimed at that DC on the site.
>
> If the DC is not available, then a default server, the PDC emulator is
> used.
>
> How would I code in VB .Net the following:
>
> 1) Determine which site the current PC running my application is in.
> 2) Determine which domain controllers are in the site.
> 3) Bind directly to the domain controller on the site using
> DirectoryServices.DirectoryEntry or DirectoryServices.DirectorySearcher
>
> It's just that I don't trust the automatic DC selection when performing
> directory operations.
Hi
Step 1) and 2) with a VBScript:
'--------------------8<----------------------
' Get sitename of current computer
sSiteName = CreateObject("ADSystemInfo").SiteName
' Find domain controller in this site
Set oRootDSE = GetObject("LDAP://RootDSE")
Set oSubnetsCont = _
GetObject("LDAP://cn=Servers,cn=" & sSiteName & ",cn=sites," _
& oRootDSE.Get("configurationNamingContext") )
oSubnetsCont.Filter = Array("server")
sServerName = "" ' Init value
For Each oSubnet In oSubnetsCont
sServerName = oSubnet.Get("cn")
Exit For ' exit loop after first find
Next
If sServerName = "" Then
WScript.Echo "No server is defined in this site"
Else
WScript.Echo "Server defined in this site: " & sServerName
End If
'--------------------8<----------------------
--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx