'From http://www.microsoft.com/technet/prodtechnol/sms/sms2003/opsguide/ops_9w9t.mspx?mfr=true ' and http://msdn2.microsoft.com/en-us/library/aa815049(VS.85).aspx if wscript.arguments.count < 1 or wscript.arguments.count > 2 then usage if instr(wscript.arguments(0),"?") > 0 then usage 'set oShell=createobject("wscript.shell") 'oShell.AppActivate ("Connect to Domain") 'oShell.Sendkeys "perriergroup.com" 'wscript.quit sQuery=wscript.arguments(0) if sQuery="AppActivate" then wscript.sleep 500 set oTmpShell=createobject("wscript.shell") sSendKeys=wscript.arguments(1)&"{ENTER}" if oTmpShell.AppActivate("Connect to Domain") then oTmpShell.Sendkeys sSendKeys end if wscript.quit end if if instr(sQuery,"=") < 1 then if instr(sQuery,"@") > 0 then if lcase(left(sQuery,5)) <> "smtp:" then sQuery="smtp:"&sQuery sQuery="proxyAddresses="&sQuery else sQuery="samAccountName="&sQuery end if end if if wscript.arguments.count = 2 then sDomain=wscript.arguments(1) if instr(sDomain,",") < 1 then sDomain=GetDomainSuffix(sDomain) end if else sDomain=GetDomain end if sDNSDomain=GetDNSDomain(sDomain) sADPath=ldapquery(sDomain,sQuery,"ADsPath")(0) if sADPath="Not Found" or instr(sADpath,",") < 1 then wscript.echo "The following query returned no results:"&vbclrf&" lcase(sDNSDomain) then oMMC.Show oMMC.UserControl=0 set oShell=createobject("wscript.shell") sExec="cscript """&wscript.scriptfullname&""" AppActivate "&sDNSDomain oShell.Run sExec,0,False oView.executeScopeNodeMenuItem "_DSADMIN_SELECT_DOMAIN",oNode wscript.sleep 500 oMMC.Hide Set oDOC = oMMC.Document set oNS=oDoc.ScopeNameSpace set oView=oDoc.Views.Item(1) Set oRoot = oNS.GetRoot Set oTop = oNS.GetChild(oRoot) set oNode = oNS.GetChild(oTop) if oNode.Name="Saved Queries" then set oNode=oNS.GetNext(oNode) oView.ActiveScopeNode=oNode end if 'oView.executeScopeNodeMenuItem('_DSADMIN_SELECT_DOMAIN',oNode) 'Escape embedded commas sADPath=replace(sADPath,"\,","~!~") aComps=split(sADPath,",") for iCCount=ubound(aComps) to 0 step -1 sComp=aComps(iCCount) 'unescape embedded commas sComp=replace(sComp,"~!~",",") sContainer="" sType="" if instr(sComp,"=") > 1 then sContainer=split(sComp,"=")(1) sType=lcase(split(sComp,"=")(0)) if len(sType) > 7 then if left(sType,7)="ldap://" then oView.ActiveScopeNode=oNode set oLeafNode=GetMMCLeafNode(oView,sContainer) oView.Select(oLeafNode) if oLeafNode.IsScopeNode then oView.ActiveScopeNode=oLeafNode else oView.DisplaySelectionPropertySheet end if end if elseif sType <> "dc" then set oNode=GetMMCNode(oNode,ONS,sContainer) oView.ActiveScopeNode=oNode end if end if next oMMC.UserControl=1 Function GetMMCNode(ByRef oGNParentNode,ByRef OGNNS,ByRef sGNNodeName) set oGNCurNode=oGNNS.GetChild(oGNParentNode) GMMCi=0 while true gMMCi=gMMCi+1 if gMMCi > 200000 then exit function on error resume next if lcase(oGNCurNode.name)=lcase(sGNNodeName) then Set GetMMCNode=oGNCurNode on error goto 0 exit function end if set oGNCurNode = oGNNS.GetNext(oGNCurNode) if err.number <> 0 then 'Last node reached if cstr(hex(err.number))="80070057" then GetMMCNode="Not Found" on error goto 0 exit function end if end if err.clear on error goto 0 wend set GetMMCNode=oGNCurNOde End Function Function GetMMCLeafNode(oGMLFView,sGMLFName) for each oGMLFNode in oGMLFView.ListItems if lcase(oGMLFNode.name)=lcase(sGMLFName) then set GETMMCLeafNode=OGMLFNode exit function end if next 'wscript.echo "here" End Function Function GetDomainSuffix(sDomain) 'Returns ldap domain suffix from dns style domain name sDomain (i.e. returns "dc=perriergroup,dc=com" from "perriergroup.com") 'Add .com if it is a netbios domain name if instr(sDomain,".") < 1 then sDomain=sDomain&".com" end if 'Get dc portion of binding string dc=split(sDomain,".") for x = 0 to ubound(dc)-1 sComp=scomp&"dc="&dc(x)&"," next GetDomainSuffix=scomp&"dc="&dc(x) End Function Function GetDomain 'Returns ADsPath to current domain 'defaultNamingContext 'schemaNamingContext set oRoot=GetObject("LDAP://rootDSE") GetDomain=oRoot.Get("defaultNamingContext") End Function Function GetDNSDomain(sGDD) sGDDResult=replace(sGDD,",dc=",".",1,-1,1) sGDDResult=replace(sGDDResult,"dc=","",1,-1,1) GetDNSDomain=sGDDResult End Function Function ldapquery(sSource,sQuery,sAttribute) on error resume next 'Returns result in aResult. Surrounds output in quotes and uses comma separation for multivalued results. ADS_CHASE_REFERRALS_SUBORDINATE = &H20 ADS_CHASE_REFERRALS_EXTERNAL = &H40 ADS_CHASE_REFERRALS_ALWAYS = ADS_CHASE_REFERRALS_SUBORDINATE Or ADS_CHASE_REFERRALS_EXTERNAL aAtts=split(sAttribute,",") iReturn=ubound(aAtts) if iReturn<0 then iReturn=0 Set oConn = WScript.CreateObject("ADODB.Connection") Set oRS = WScript.CreateObject("ADODB.Recordset") Set oCommand = WScript.CreateObject("ADODB.Command") oConn.Provider = "ADsDSOObject" oConn.Open "Active Directory Provider" Set oCommand.ActiveConnection = oConn oCommand.CommandText = ";("&sQuery&");"&sAttribute&";SubTree" oCommand.Properties("Page Size") = 99 oCommand.Properties("Chase referrals") = ADS_CHASE_REFERRALS_ALWAYS Set oRS = oCommand.Execute if oRS.recordcount<1 then ldapquery=array("Not Found") exit function end if oRS.movefirst redim Preserve aResult(oRS.recordcount-1) x=0 While Not oRS.EOF 'User for multi-attribute queries 'strTemp="""" 'for intI = 0 to iReturn ' if len(strTemp)>1 then strTemp=strTemp&",""" ' strTemp=strTemp&CSTR(oRS.Fields(intI).Value)&"""" 'next 'aResult(x)=strTemp aResult(x)=oRS.Fields(0).Value x=x+1 oRS.MoveNext Wend ldapquery=aResult End Function Sub Usage sUsage=vbcrlf&"Usage:" sUsage=sUsage&vbcrlf&vbcrlf&vbclrf&"cscript aduc.vbs [domain]" sUsage=sUsage&vbcrlf&vbcrlf&"for example: cscript aduc.vbs myloginid" sUsage=sUsage&vbcrlf&"or: cscript aduc.vbs ou=myou" sUsage=sUsage&vbcrlf&"or: cscript aduc.vbs ""cn=usera*"" ""dc=domain,dc=com""" sUsage=sUsage&vbcrlf&"or: cscript aduc.vbs me@domain.com" sUsage=sUsage&vbcrlf&vbcrlf&"If a leaf object is specified, the properties are displayed." sUsage=sUsage&vbcrlf&"Otherwise the object is highlighted in the navigation pane. " sUsage=sUsage&vbcrlf&"If no domain is specified, the current domain is used"&vbcrlf wscript.echo sUsage wscript.quit End Sub