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