WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Determine if a WS and a Server are in the same AD Site

Keywords:   determine if a WS and a server are in the same AD Site

Here's a snippet of code I used to determine if a WS and a server are in the same AD Site. It's useful for WAN access and DFS structures. It looks up the IP address of the server and workstation, then reads Site info from AD, does the math to match subnets and determines if the machine exec'ing the code is in the same site as the servername supplied. It uses the ADSI extender and the IP Grabber. I think it's written generically for any AD domain, but you're on your own if it isn't.

Thought someone might have a use for it.

Jim Graczyk

===========================================
;get IP addrs for the server
ServerAddr=ipHost2Addr(servername, 30)

;get my ip address to give to the server
ips=ipGetAddress()
count=ItemCount(ips,@tab)
bestaddr=""
bestsubnet=""
for xx=1 to count
thisipstring=ItemExtract(xx,ips,@tab)
ip=ItemExtract(1,thisipstring,"|")
sub=ItemExtract(2,thisipstring,"|")
brd=ItemExtract(3,thisipstring,"|")
flags=ItemExtract(4,thisipstring,"|")
show=strcat(ip,@crlf,sub,@crlf,brd,@crlf,strreplace(flags," ",@crlf))
;fish out a usable IP address
;a) no better address already found
;b) ip address not 0.0.0.0
;c) LOOPBACK bot set
if bestaddr!="" then continue
if ip=="0.0.0.0" then continue
if ItemExtract(1,flags," ")=="DOWN" then continue
if ItemExtract(4,flags," ")=="LOOPBACK" then continue
bestaddr=ip
bestsubnet=sub
next

;bestaddr is the IP address
; get username and machine to give to the server

; identify network number for the PC running this app
; and the server serving the MDB file.

saddr=0
addr=0
for i = 4 to 1 by -1

staddr = itemextract(i,serveraddr,".") * 2** (32-(i*8))
saddr = saddr + staddr
taddr = itemextract(i,bestaddr,".") * 2** (32-(i*8))
addr = addr + taddr

next 

mynetworknum=addr
servernetnum=saddr

; Define some constants.
MANDATORY = 1
OPTIONAL = 2
MANANDOPT = 3


;look in AD for the subnets container and what site each is in
sAdsiPath = "LDAP://rootDSE"
sValue = dsGetProperty(sAdsiPath , "defaultNamingContext") 

sDomainDNS = "LDAP://%sValue%"

domain = dsGetProperty("LDAP://rootDSE" , "defaultNamingContext")
xn = itemcount(domain,",")

if xn > 2 
rootdomain=strcat(itemextract(xn-1,domain,","),",",itemextract(xn,domain,","))
else
rootdomain=domain
endif

ADSISubnetsPath=Strcat("LDAP://CN=Subnets,CN=Sites,CN=Configuration,",rootdomain)

class="subnet"

Subnets=dsGetChldPath(ADSISubnetsPath, class)

;message("Subnet",subnets)

Sites=""
foundme=""
FoundServer=""

For i = 1 to itemcount(subnets,@tab)

Subnet=itemextract(i,subnets,@tab)

site=dsgetproperty(subnet,"siteobject")
sites=iteminsert(site,-1,sites,@tab)

subnet=itemextract(1,subnet,",")
subnet=itemextract(2,subnet,"=")
subnet=strreplace(subnet,"\","")


site=itemextract(1,site,",")
site=itemextract(2,site,"=")


netaddr=itemextract(1,subnet,"/")
netmask = itemextract(2,subnet,"/")

for j = 1 to 4

octect=itemextract(j,netaddr,".")
num= octect * 2** (32-(j*8))
x=netnum[i,1]
netnum[i,1] = x + num

next

mask=0

for j = 1 to netmask
mask=mask+ 2**(32-j)
next

netnum[i,2]=mask

evalmynetnum = mynetworknum & netnum[i,2] 

if evalmynetnum == netnum[i,1]
foundme=site 
endif

evalServernetnum= servernetnum & netnum[i,2]

if evalServernetnum == netnum[i,1]
foundserver=site
endif

if Foundme <> "" && foundServer<>"" then break

next

if foundme == "" || foundserver == "" then fileislocal=@true

if foundme == foundserver then fileislocal=@true 

Article ID:   W15049
File Created: 2002:09:05:13:49:18
Last Updated: 2002:09:05:13:49:18