#134839 - 2005-03-02 04:00 PM
Querying the active directory
|
HeartCore
Fresh Scripter
Registered: 2005-03-01
Posts: 17
|
Hi,
Hopefully this is the correct forum for this question, if not I ask the moderators kindly to slap me in the face and move the topic to an appropiate forum
Anyway, I'm working on a small project that involves optimizing logon procedures for roaming users. One thing that is on the wishlist of my boss, is when a user logs in on a computer, automatically, all printers that are linked to the same location are added to this users profile.
Collecting this info is where I'm stuck at the moment, I know there are various tools to query the active directory and I'm wondering about opinons of visitors here. What tool are you using for this? I only need to read info, I don't need to modify anything and I'm looking for a commandline tool which I can call from the logon/kix script.
Thanks
Henk
EDIT: Forgive my ignorance, enough topics on this board about this so I'm gonna spend the next few hours reading.
Edited by HeartCore (2005-03-02 04:14 PM)
|
|
Top
|
|
|
|
#134840 - 2005-03-02 05:21 PM
Re: Querying the active directory
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
You can try this..
Default Printer Configuration
What this does is list the printers into an INI file that the user can re-use if they happen to login to another workstation.
Kent
|
|
Top
|
|
|
|
#134841 - 2005-03-03 08:45 AM
Re: Querying the active directory
|
HeartCore
Fresh Scripter
Registered: 2005-03-01
Posts: 17
|
Thats one solution, thanks.
However, I was thinking about the following solution:
User logs in, the logon script retrieves the location info for the pc in active directory, then it retrieves all printernames that match the location of the pc also from active directory and installs them for the current user (using addprinterconnection).
Gz Henk
|
|
Top
|
|
|
|
#134844 - 2005-03-04 10:13 AM
Re: Querying the active directory
|
HeartCore
Fresh Scripter
Registered: 2005-03-01
Posts: 17
|
Very nice post, thanks a lot this gets me going!
|
|
Top
|
|
|
|
#134845 - 2005-03-04 01:41 PM
Re: Querying the active directory
|
HeartCore
Fresh Scripter
Registered: 2005-03-01
Posts: 17
|
Allright I've been messing around with this script. There's a couple of questions I have (I'll put them as comments in the code)
Quote:
This should get you started
Code:
break on
$objRootDSE = GetObject("LDAP://RootDSE") $strDomain = $objRootDSE.Get("DefaultNamingContext") $objConnection = CreateObject("ADODB.Connection") $objConnection.Open("Provider=ADsDSOObject;") $objCommand = CreateObject("ADODB.Command") $objCommand.ActiveConnection = $objConnection $OU = "" The lines above, they are just for making a connection to the directory right? So I should not have to use these lines more than once per script I assume?
? "Enter computer name: " gets $comp
$objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" + $strDomain + "'" + " WHERE cn='" + $comp + "'"
$objRecordSet = $objCommand.Execute() This line executes the query and puts the results in an array (objRecordSet) right?
While Not $objRecordSet.EOF $dn = $objRecordSet.Fields("distinguishedName").Value $OU = right($dn, len($dn) - instr($dn, ",")) $objRecordSet.MoveNext loop I don't understand exactly the purpose of this part, $OU becomes something like OU=Test Computer policy,OU=Computers CompanyName,DC=DomainName,DC=CompanyName,DC=nl . What is the reason for this part?
if $OU $objCommand.CommandText = "SELECT Name FROM 'LDAP://" + $OU + "'" + " WHERE objectCategory='printQueue'" $objRecordSet = $objCommand.Execute() While Not $objRecordSet.EOF ? $objRecordSet.Fields("Name").Value $objRecordSet.MoveNext loop Ok I got this part figured out I think. What I don't have clear, is the following. In the query command, the command is SELECT Name from, can I use this the same way as with Mysql for example select * from...? Or do you have to specify each field that you want to retrieve seperately? else ? "Can't find " + $comp endif
I've been tampering with your original script and have this so far:
Code:
break on $objRootDSE = GetObject("LDAP://RootDSE") $strDomain = $objRootDSE.Get("DefaultNamingContext") $objConnection = CreateObject("ADODB.Connection") $objConnection.Open("Provider=ADsDSOObject;") $objCommand = CreateObject("ADODB.Command") $objCommand.ActiveConnection = $objConnection $OU = "" $comp = @wksta $objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" + $strDomain + "'" + " WHERE cn='" + $comp + "'" $objRecordSet = $objCommand.Execute() While Not $objRecordSet.EOF $dn = $objRecordSet.Fields("distinguishedName").Value $OU = right($dn, len($dn) - instr($dn, ",")) $objRecordSet.MoveNext loop $objCommand.CommandText = "SELECT Location FROM 'LDAP://" + $strDomain + "'" + " WHERE cn='" + $comp + "'" $objRecordSet = $objCommand.Execute() While Not $objRecordSet.EOF $Location = $objRecordSet.Fields("location").Value ? $Location $objRecordSet.MoveNext loop
So this gives me the location of the workstation the user is logged on to. Now I can just retrieve all printqueue objects that match this location right? I should be able to do something like:
Code:
$objCommand.CommandText="Select Name from 'LDAP://" + $OU + "'" + " WHERE objectCategory='printqueue' AND Location='" + $Location + "'"
$objRecordSet.objCommand.Execute()
I'll figure this out, before the end of the day lol....
Tx Henk
|
|
Top
|
|
|
|
#134847 - 2005-03-07 08:59 AM
Re: Querying the active directory
|
HeartCore
Fresh Scripter
Registered: 2005-03-01
Posts: 17
|
Monday morning.....
Getting there slowly 
I'm getting stuck with these AD queries and I think I'm making errors in the LDAP:// part in the command. Suppose I want to check the value of location for all printers connected to a system here:
OU=servers, OU=Computers Group, DC=domname, dc=compname, dc=nl (In the root is OU 'Computers group', then servers and under servers are the computers.
Quote:
$LD="OU=servers, OU=Computers Group, DC=domname, dc=compname, dc=nl" "SELECT Name from 'LDAP://" + $LD + "'" + " WHERE objectCategory='printQueue' AND Location='" + $Location + "'" $objRecordSet = objCommand.Execute() While Not $objRecordSet.EOF $Test1 = $objRecordSet.Fields("Name").Value ? "printer found" $objRecordSet.MoveNext loop
The printer found message is never displayed. $Location var is correct, I tested that and I think something is not right in my query.
Tx Henk
|
|
Top
|
|
|
|
#134849 - 2005-03-07 03:36 PM
Re: Querying the active directory
|
HeartCore
Fresh Scripter
Registered: 2005-03-01
Posts: 17
|
I just solved it. $Location is referring to the field location. I was making mistakes with the (last) LDAP:// part, the objectCategory and a $ sign before an objCommand.
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|