Page 1 of 1 1
Topic Options
#134839 - 2005-03-02 04:00 PM Querying the active directory
HeartCore Offline
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 Offline
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
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#134841 - 2005-03-03 08:45 AM Re: Querying the active directory
HeartCore Offline
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
#134842 - 2005-03-03 02:48 PM Re: Querying the active directory
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Well... you are not the first person to think of that approach. There must be a few dozen such topics already on this board. You can use the InContainer() UDF to determine the OU of the PC, and @Site to determine the site or one of the many IP Subnet UDFs.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#134843 - 2005-03-03 06:23 PM Re: Querying the active directory
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
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 = ""

? "Enter computer name: "
gets $comp

$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

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
else
? "Can't find " + $comp
endif

_________________________
Eric

Top
#134844 - 2005-03-04 10:13 AM Re: Querying the active directory
HeartCore Offline
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 Offline
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
#134846 - 2005-03-04 02:16 PM Re: Querying the active directory
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Yes those first lines are just making a connection and yes you only need them once.

The Execute() method of the Command object ($objCommand.Execute()) returns a recordset, which is not exactly an array.

The $OU variable holds the path to the OU the computer is in. So that when we search for printers, we can just target that OU. If we didn't find the computer in AD with the original query, then we have no OU to use with the printer query.

And I'm not sure i have ever tried "Select * ..." with the AD queries. But I'm fairly certain that will return quite a large recordset. If you know which fields you need, I'd suggest just SELECTing them individually
_________________________
Eric

Top
#134847 - 2005-03-07 08:59 AM Re: Querying the active directory
HeartCore Offline
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
#134848 - 2005-03-07 02:10 PM Re: Querying the active directory
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
What is $Location? Is the Location property populated for your printers in Active Directory?

Have you tried getting rid of that part of your query and just querying for printQueue?
_________________________
Eric

Top
#134849 - 2005-03-07 03:36 PM Re: Querying the active directory
HeartCore Offline
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
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.071 seconds in which 0.034 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org