#190788 - 2008-12-03 12:17 AM
LDAP Query into array thanks
|
itdaddy
Starting to like KiXtart
Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
|
Hey Apronk and Kix gurus! thanks for all yoru help !
Long time to see I know you the man who can either help me or point me in the direction. This is what I want to do. Remember that program I made for my work well it is working great. The issue is I want to not have to update my array list I made the array variable is called $Branch for net bios names for each PC in our domain. Follow me. Well in our Active Directory we have 4 branch OUs and in the branch OUs we have the OU named Computer. So you have this for example:
4th\Computer ...pcs named ftran1, ftran2, fmsr1, fmsr2 etc..... 8th\Computer ...pcs named emsr1, emsr2, emort1, emort3 etc.... Point\Computer ...pcs named smsr1, smsr2,smsr3, etc.... Plover\Computer...pcs name pmsr1, pmsr2, ect....
in each OU you have their respective netbios names according to AD OU. So I want a program to reside on a domain controller with Admin domain rights it will have. and pull the names from these OUs into a text file or what have you into an array list $Branch. I want to just click it to update this file into what my program use a file call domain.kix and in there is a list of names that my array is defined by $Branch = "smsr1", "smsr2", "pmsr1", "pmsr2", etc....is this possible?? thanks alot for your help...
Edited by itdaddy (2008-12-04 08:30 PM)
_________________________
Robert A+, CCNA, MCP Network Admin Credit Union Wisconsin
|
|
Top
|
|
|
|
#190789 - 2008-12-03 03:49 AM
Re: Glenn help! You the man!
[Re: itdaddy]
|
Gargoyle
MM club member
   
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
|
While I am not Glenn, You can use the UDF fnLDAPQuery() to generate your array on the fly.
$Batch = fnLDAPQuery('computer','ou=Computer,ou=4th,dc=microsoft,dc=fabrikon,dc=com')
;fnLDAPQuery(WHAT, Optional FROM, Optional FILTER, Optional ORDER BY, Optional SCOPE, Optional USER, Optional PASSWORD)
You can find the udf here http://www.kixtart.org/UDF/UDF_lister.php?what=post&code=142043
_________________________
Today is the tomorrow you worried about yesterday.
|
|
Top
|
|
|
|
#190790 - 2008-12-03 10:53 AM
Re: Glenn help! You the man!
[Re: Gargoyle]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
Just to show a different approach:
Dim $OU1, $OU2, $x
$OU1 = GetComps("OU=Computers,OU=branch1,DC=domain,DC=local")
$OU2 = GetComps("OU=Computers,OU=branch2,DC=domain,DC=local")
For Each $x in $OU1
;Can list this to a text file too
? $x
Next
For Each $x in $OU2
;Can list this to a text file too
? $x
Next
Function GetComps($strAdsPath)
Dim $objAdsPath, $obj, $filter[0], $arrComps[]
$filter[0] = "Computer"
$objADsPath = GetObject("LDAP://"+$strAdsPath)
$objAdsPath.filter = $filter
For Each $obj in $objAdsPath
ReDim Preserve $arrComps[UBound($arrComps)+1]
$arrComps[UBound($arrComps)] = $obj.cn
Next
$GetComps = $arrComps
EndFunction
You can also then join $OU1 and $OU2 into 1 array, but I figured you would like to have them separate. But if you don't the fnLDAPQuery i the best option here. Also fnLDAPQuery is faster then this solution.
|
|
Top
|
|
|
|
#190799 - 2008-12-03 05:36 PM
Re: Glenn help! You the man!
[Re: itdaddy]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
That should do it
|
|
Top
|
|
|
|
#190807 - 2008-12-03 06:16 PM
Re: Glenn help! You the man!
[Re: Arend_]
|
itdaddy
Starting to like KiXtart
Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
|
What do you think? is this what you mean? I think so. But I want it to out put the lists to see if it pulls into arrays? you rock Apronk! l;)
;******************************************
;Creates array lists of each branch
;by pulling comuter names from each
;respective OU in an AD domain for
;specific programs to access
;respective $BranchX array variables
;once generated variable hold value in RAM
;to be used by programs.
;******************************************
Dim $Branch1, $Branch2, $Branch3, $Branch4
;fnLDAPQuery(WHAT, Optional FROM, Optional FILTER, Optional ORDER BY, Optional SCOPE, Optional USER, Optional PASSWORD)
$Branch1 = fnLDAPQuery('computer','ou=Computer,ou=4th,dc=microsoft,dc=fabrikon,dc=com')
$Branch2 = fnLDAPQuery('computer','ou=Computer,ou=8th,dc=microsoft,dc=fabrikon,dc=com')
$Branch3 = fnLDAPQuery('computer','ou=Computer,ou=plov,dc=microsoft,dc=fabrikon,dc=com')
$Branch4 = fnLDAPQuery('computer','ou=Computer,ou=Stevens,dc=microsoft,dc=fabrikon,dc=com')
;display each $Branch variable array list to test to see if works
For Each $x in $Branch1
;Display array list as test
? $x
Next
For Each $x in $Branch2
;Display array list as test
? $x
Next
For Each $x in $Branch3
;Display array list as test
? $x
Next
For Each $x in $Branch4
;Display array list as test
? $x
Next
Function fnLDAPQuery($What,Optional $From,Optional $Filter,Optional $OrderBy,Optional $Scope,
Optional $User,Optional $Pswd)
Dim $oCon,$oCMD,$oRS,$sQ,$aR,$C,$R
$sQ="<"+Iif($From="","LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext"),
$From)+">;"+$Filter+";"+Iif(VarType($What)>8192,Join($What,','),$What)+";"+
Iif($Scope<>"base" AND $Scope<>"onelevel","subtree",$Scope)
$oCon=CreateObject("ADODB.Connection")
$oCon.Provider="ADsDSOObject"
$oCon.Properties("Encrypt Password").Value=1
$oCon.Properties("ADSI Flag").Value=1
If $User AND $Pswd
$oCon.Properties("User ID").Value=$User
$oCon.Properties("Password").Value=$Pswd
EndIf
$oCon.Open("Active Directory Provider")
$oCMD=CreateObject("ADODB.Command")
$oCMD.ActiveConnection=$oCon
$oCMD.CommandText=$sQ
$oCMD.Properties("Page Size").Value=1000
$oCMD.Properties("Timeout").Value=30
$oCMD.Properties("Cache Results").Value=0
If InStr($OrderBy,"distinguishedName")
$oRS=CreateObject("ADODB.Recordset")
$oRS.CursorLocation=3
$oRS.Sort=$OrderBy
$oRS.Open($sQ,$oCon,0,1,1)
Else
If $OrderBy
$oCMD.Properties("Sort On").Value=$OrderBy
EndIf
$oRS=$oCMD.Execute
EndIf
If @ERROR Exit @ERROR EndIf
If $oRS.BOF AND $oRS.EOF Exit @ERROR EndIf
$aR = $oRS.GetRows()
Dim $aFR[Ubound($aR,2),Ubound($aR,1)]
For $R=0 to Ubound($aR,2)
For $C=0 to Ubound($aR,1)
$aFR[$R,$C]=$aR[$C,$R]
Next
Next
$fnLDAPQuery=$aFR
EndFunction
Edited by Benny69 (2008-12-04 04:43 AM) Edit Reason: Added Code Tags
_________________________
Robert A+, CCNA, MCP Network Admin Credit Union Wisconsin
|
|
Top
|
|
|
|
#190809 - 2008-12-03 06:23 PM
Re: Glenn help! You the man!
[Re: itdaddy]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
Yeah, but then with code tags
|
|
Top
|
|
|
|
#190810 - 2008-12-03 06:46 PM
Re: Glenn help! You the man!
[Re: Arend_]
|
itdaddy
Starting to like KiXtart
Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
|
Apronk, I hate asking more questions but this is what I have. I have a DomainAD.cmd file with kix32 DomainAD.kix (name of file below) to run this and nothing outputs to screen? I even have a pause at end of my cmd file. What did I do wrong? I am runnin this on a file server with domain rights? Is the order of my ldap query right (the parameters?) I use funny as a fake domain name for security but this is how it goes: the levels are funny.priv then funny ou, then branch name then Computers final OU with the list of computers inside? is that right order?
Dim $Branch1, $Branch2, $Branch3, $Branch4
;fnLDAPQuery(WHAT, Optional FROM, Optional FILTER, Optional ORDER BY, Optional SCOPE, Optional USER, Optional PASSWORD)
$Branch1 = fnLDAPQuery('SRV-DC1','ou=funny,ou=ForthAve,ou=Computers,dc=microsoft,dc=funny,dc=priv')
$Branch2 = fnLDAPQuery('SRV-DC1','ou=funny,ou=EighthSt,ou=Computers,dc=microsoft,dc=funny,dc=priv')
$Branch3 = fnLDAPQuery('SRV-DC1','ou=funny,ou=PostRd,ou=Computers,dc=microsoft,dc=funny,dc=priv')
$Branch4 = fnLDAPQuery('SRV-DC1','ou=funny,ou=ParkDr,ou=Computers,dc=microsoft,dc=funny,dc=priv')
;*****************************Display Test ****************************************************
For Each $x in $Branch1
;Display array list as test
? $x
Next
For Each $x in $Branch2
;Display array list as test
? $x
Next
For Each $x in $Branch3
;Display array list as test
? $x
Next
For Each $x in $Branch4
;Display array list as test
? $x
Next
;***********************LDAP Function ******************************************************
Function fnLDAPQuery($What,Optional $From,Optional $Filter,Optional $OrderBy,Optional $Scope,
Optional $User,Optional $Pswd)
Dim $oCon,$oCMD,$oRS,$sQ,$aR,$C,$R
$sQ="<"+Iif($From="","LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext"),
$From)+">;"+$Filter+";"+Iif(VarType($What)>8192,Join($What,','),$What)+";"+
Iif($Scope<>"base" AND $Scope<>"onelevel","subtree",$Scope)
$oCon=CreateObject("ADODB.Connection")
$oCon.Provider="ADsDSOObject"
$oCon.Properties("Encrypt Password").Value=1
$oCon.Properties("ADSI Flag").Value=1
If $User AND $Pswd
$oCon.Properties("User ID").Value=$User
$oCon.Properties("Password").Value=$Pswd
EndIf
$oCon.Open("Active Directory Provider")
$oCMD=CreateObject("ADODB.Command")
$oCMD.ActiveConnection=$oCon
$oCMD.CommandText=$sQ
$oCMD.Properties("Page Size").Value=1000
$oCMD.Properties("Timeout").Value=30
$oCMD.Properties("Cache Results").Value=0
If InStr($OrderBy,"distinguishedName")
$oRS=CreateObject("ADODB.Recordset")
$oRS.CursorLocation=3
$oRS.Sort=$OrderBy
$oRS.Open($sQ,$oCon,0,1,1)
Else
If $OrderBy
$oCMD.Properties("Sort On").Value=$OrderBy
EndIf
$oRS=$oCMD.Execute
EndIf
If @ERROR Exit @ERROR EndIf
If $oRS.BOF AND $oRS.EOF Exit @ERROR EndIf
$aR = $oRS.GetRows()
Dim $aFR[Ubound($aR,2),Ubound($aR,1)]
For $R=0 to Ubound($aR,2)
For $C=0 to Ubound($aR,1)
$aFR[$R,$C]=$aR[$C,$R]
Next
Next
$fnLDAPQuery=$aFR
EndFunction
_________________________
Robert A+, CCNA, MCP Network Admin Credit Union Wisconsin
|
|
Top
|
|
|
|
#190812 - 2008-12-03 06:49 PM
Re: Glenn help! You the man!
[Re: Arend_]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
This will be the working code:
;******************************************
;Creates array lists of each branch
;by pulling comuter names from each
;respective OU in an AD domain for
;specific programs to access
;respective $BranchX array variables
;once generated variable hold value in RAM
;to be used by programs.
;******************************************
Dim $Branch1, $Branch2, $Branch3, $Branch4
;fnLDAPQuery(WHAT, Optional FROM, Optional FILTER, Optional ORDER BY, Optional SCOPE, Optional USER, Optional PASSWORD)
$Branch1 = fnLDAPQuery('Name','LDAP://ou=Computer,ou=4th,dc=microsoft,dc=fabrikon,dc=com','(&(objectClass=Computer)(Name=*))')
$Branch2 = fnLDAPQuery('Name','LDAP://ou=Computer,ou=8th,dc=microsoft,dc=fabrikon,dc=com','(&(objectClass=Computer)(Name=*))')
$Branch3 = fnLDAPQuery('Name','LDAP://ou=Computer,ou=plov,dc=microsoft,dc=fabrikon,dc=com','(&(objectClass=Computer)(Name=*))')
$Branch4 = fnLDAPQuery('Name','LDAP://ou=Computer,ou=Stevens,dc=microsoft,dc=fabrikon,dc=com','(&(objectClass=Computer)(Name=*))')
For Each $x in $Branch1
;Display array list as test
? "1:"+$x
Next
For Each $x in $Branch2
;Display array list as test
? "2:"+$x
Next
For Each $x in $Branch3
;Display array list as test
? "3:"+$x
Next
For Each $x in $Branch4
;Display array list as test
? "4:"+$x
Next
Function fnLDAPQuery($What,Optional $From,Optional $Filter,Optional $OrderBy,Optional $Scope,Optional $User,Optional $Pswd)
Dim $oCon,$oCMD,$oRS,$sQ,$aR,$C,$R
$sQ="<"+Iif($From="","LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext"),$From)+">;"+$Filter+";"+Iif(VarType($What)>8192,Join($What,','),$What)+";"+Iif($Scope<>"base" AND $Scope<>"onelevel","subtree",$Scope)
$oCon=CreateObject("ADODB.Connection")
$oCon.Provider="ADsDSOObject"
$oCon.Properties("Encrypt Password").Value=1
$oCon.Properties("ADSI Flag").Value=1
If $User AND $Pswd
$oCon.Properties("User ID").Value=$User
$oCon.Properties("Password").Value=$Pswd
EndIf
$oCon.Open("Active Directory Provider")
$oCMD=CreateObject("ADODB.Command")
$oCMD.ActiveConnection=$oCon
$oCMD.CommandText=$sQ
$oCMD.Properties("Page Size").Value=1000
$oCMD.Properties("Timeout").Value=30
$oCMD.Properties("Cache Results").Value=0
If InStr($OrderBy,"distinguishedName")
$oRS=CreateObject("ADODB.Recordset")
$oRS.CursorLocation=3
$oRS.Sort=$OrderBy
$oRS.Open($sQ,$oCon,0,1,1)
Else
If $OrderBy
$oCMD.Properties("Sort On").Value=$OrderBy
EndIf
$oRS=$oCMD.Execute
EndIf
If @ERROR Exit @ERROR EndIf
If $oRS.BOF AND $oRS.EOF Exit @ERROR EndIf
$aR = $oRS.GetRows()
Dim $aFR[Ubound($aR,2),Ubound($aR,1)]
For $R=0 to Ubound($aR,2)
For $C=0 to Ubound($aR,1)
$aFR[$R,$C]=$aR[$C,$R]
Next
Next
$fnLDAPQuery=$aFR
EndFunction
|
|
Top
|
|
|
|
#190813 - 2008-12-03 06:50 PM
Re: Glenn help! You the man!
[Re: Arend_]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
I assumed Gargolye's input vars were correct, but ASSUME makes an ASS out of U and ME
|
|
Top
|
|
|
|
#190816 - 2008-12-03 08:58 PM
Re: Glenn help! You the man!
[Re: itdaddy]
|
itdaddy
Starting to like KiXtart
Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
|
Apronk
like this? wasnt sure what whent in Computer or name if it was just or did the ou Computers go there? and I put the AD dc in place of name is this allw rong? I am confused on what goes in what place remeber the ad hierarchy goes like ths funnycu.priv funncu forthave Computers (repeat branch names and ou computers)
Dim $Branch1, $Branch2, $Branch3, $Branch4
;fnLDAPQuery(WHAT, Optional FROM, Optional FILTER, Optional ORDER BY, Optional SCOPE, Optional USER, Optional PASSWORD)
$Branch1 = fnLDAPQuery('srv-dc1','LDAP://ou=Computers,ou=forthAve,dc=microsoft,dc=funnycu,dc=priv','(&(objectClass=Computers)(srv-dc1=*))')
$Branch2 = fnLDAPQuery('srv-dc1','LDAP://ou=Computers,ou=EighthSt,dc=microsoft,dc=funnycu,dc=priv','(&(objectClass=Computers)(srv-dc1=*))')
$Branch3 = fnLDAPQuery('srv-dc1','LDAP://ou=Computers,ou=PostRd,dc=microsoft,dc=funnycu,dc=priv','(&(objectClass=Computers)(srv-dc1=*))')
$Branch4 = fnLDAPQuery('srv-dc1','LDAP://ou=Computers,ou=PostRd,dc=microsoft,dc=funnycu,dc=priv','(&(objectClass=Computers)(srv-dc1=*))')
For Each $x in $Branch1
;Display array list as test
? "1:"+$x
Next
For Each $x in $Branch2
;Display array list as test
? "2:"+$x
Next
For Each $x in $Branch3
;Display array list as test
? "3:"+$x
Next
For Each $x in $Branch4
;Display array list as test
? "4:"+$x
Nex
_________________________
Robert A+, CCNA, MCP Network Admin Credit Union Wisconsin
|
|
Top
|
|
|
|
#190817 - 2008-12-03 09:09 PM
Re: Glenn help! You the man!
[Re: itdaddy]
|
itdaddy
Starting to like KiXtart
Registered: 2006-12-19
Posts: 145
Loc: Wisconsin
|
apronk Here is the exact code I am using expect for bogous domain name funncu. It runs with no errors but no pull for AD?? what did I do wrong i have in a cmd file kix32 DomainAD.kix and runs but no output and no errors. Do I have some of the names wrong??? sorry to be so stupid but I am really trying..
Dim $Branch1, $Branch2, $Branch3, $Branch4
;fnLDAPQuery(WHAT, Optional FROM, Optional FILTER, Optional ORDER BY, Optional SCOPE, Optional USER, Optional PASSWORD)
$Branch1 = fnLDAPQuery('srv-dc1','LDAP://ou=funncu,ou=ForthAve,ou=Computers,dc=microsoft,dc=funncu,dc=priv','(&(objectClass=Computer)(srv-dc1=*))')
$Branch2 = fnLDAPQuery('srv-dc1','LDAP://ou=funncu,ou=EighthSt,ou=Computers,dc=microsoft,dc=funncu,dc=priv','(&(objectClass=Computer)(srv-dc1=*))')
$Branch3 = fnLDAPQuery('srv-dc1','LDAP://ou=funncu,ou=PostRd,ou=Computers,dc=microsoft,funncu,dc=priv','(&(objectClass=Computer)(srv-dc1=*))')
$Branch4 = fnLDAPQuery('srv-dc1','LDAP:/ou=funncu,ou=ParkDr,ou=Computers,dc=microsoft,dc=funncu,dc=priv','(&(objectClass=Computer)(srv-dc1=*))')
For Each $x in $Branch1
;Display array list as test
? "1:"+$x
Next
For Each $x in $Branch2
;Display array list as test
? "2:"+$x
Next
For Each $x in $Branch3
;Display array list as test
? "3:"+$x
Next
For Each $x in $Branch4
;Display array list as test
? "4:"+$x
Next
Function fnLDAPQuery($What,Optional $From,Optional $Filter,Optional $OrderBy,Optional $Scope,Optional $User,Optional $Pswd)
Dim $oCon,$oCMD,$oRS,$sQ,$aR,$C,$R
$sQ="<"+Iif($From="","LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext"),$From)+">;"+$Filter+";"+Iif(VarType($What)>8192,Join($What,','),$What)+";"+Iif($Scope<>"base" AND $Scope<>"onelevel","subtree",$Scope)
$oCon=CreateObject("ADODB.Connection")
$oCon.Provider="ADsDSOObject"
$oCon.Properties("Encrypt Password").Value=1
$oCon.Properties("ADSI Flag").Value=1
If $User AND $Pswd
$oCon.Properties("User ID").Value=$User
$oCon.Properties("Password").Value=$Pswd
EndIf
$oCon.Open("Active Directory Provider")
$oCMD=CreateObject("ADODB.Command")
$oCMD.ActiveConnection=$oCon
$oCMD.CommandText=$sQ
$oCMD.Properties("Page Size").Value=1000
$oCMD.Properties("Timeout").Value=30
$oCMD.Properties("Cache Results").Value=0
If InStr($OrderBy,"distinguishedName")
$oRS=CreateObject("ADODB.Recordset")
$oRS.CursorLocation=3
$oRS.Sort=$OrderBy
$oRS.Open($sQ,$oCon,0,1,1)
Else
If $OrderBy
$oCMD.Properties("Sort On").Value=$OrderBy
EndIf
$oRS=$oCMD.Execute
EndIf
If @ERROR Exit @ERROR EndIf
If $oRS.BOF AND $oRS.EOF Exit @ERROR EndIf
$aR = $oRS.GetRows()
Dim $aFR[Ubound($aR,2),Ubound($aR,1)]
For $R=0 to Ubound($aR,2)
For $C=0 to Ubound($aR,1)
$aFR[$R,$C]=$aR[$C,$R]
Next
Next
$fnLDAPQuery=$aFR
EndFunction
Edited by itdaddy (2008-12-03 09:12 PM)
_________________________
Robert A+, CCNA, MCP Network Admin Credit Union Wisconsin
|
|
Top
|
|
|
|
#190818 - 2008-12-03 09:15 PM
Re: Glenn help! You the man!
[Re: itdaddy]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4567
Loc: USA
|
I think you could also use an array for your branches.
(untested and I've never used fnLDAPQuery... )
Dim $Branches[3],$branch,$i,$x
$Branches[0]=fnLDAPQuery('computer','ou=Computer,ou=4th,dc=microsoft,dc=fabrikon,dc=com')
$Branches[1]=fnLDAPQuery('computer','ou=Computer,ou=8th,dc=microsoft,dc=fabrikon,dc=com')
$Branches[2]=fnLDAPQuery('computer','ou=Computer,ou=plov,dc=microsoft,dc=fabrikon,dc=com')
$Branches[3]=fnLDAPQuery('computer','ou=Computer,ou=Stevens,dc=microsoft,dc=fabrikon,dc=com')
;display each $Branch variable array list to test to see if works
For Each $branch in $branches
$i=$i+1
For each $x in $branch
;Display array list as test
? "" + $i + ":" + $x
Next
Next
Edited by Allen (2008-12-03 09:23 PM)
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 611 anonymous users online.
|
|
|