Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem With ADSI script

Status
Not open for further replies.

Jokada

Programmer
Apr 29, 2004
24
BE
Hi all
i hope you can help me out with this one.
I want to write all Printers of domain tke.intra to a txt file.
however it keeps giving an error on the description column that comes from the active directory.
When i want to write it in the file it keeps saying
"Typen unverträglich"
This means the type is not string or any kind. I tried to convert it with Cstr but that was a no go...
Any help would be very appreciated!

(you can test this file in you are on a domain, just change the tke and the intra)


Const ADS_SCOPE_SUBTREE = 2

MsgBox "haben Sie bitte 60 Secunden Geduld", VBInformation
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select printerName, drivername, ServerName, location, description from " _
& " 'LDAP://DC=tke,DC=intra' where objectClass='printQueue' "
objCommand.Properties("Page Size") = 2000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
arrPrinter = objRecordSet.GetRows()
objConnection.Close()

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
CurrTime = Now()
filename = "C:\Printers-" & Left(CurrTime, 10) & ".txt"
Set objTextFile = objFSO.OpenTextFile(filename, ForAppending, True)

MsgBox UBound(arrPrinter,2)
iprinterCount = 0
Description = ""
For intI = 0 to UBound(arrPrinter,2)

Description = (arrPrinter(0,IntI))
strLocation = Trim(arrPrinter(1, IntI))
strServerName = Trim(arrPrinter(2, IntI))
strDrivername = Trim(arrPrinter(3, IntI))
strPrinterName = Trim(arrPrinter(4, IntI))

'objTextFile.WriteLine (strPrinterName & ", " & strServerName & ", " & strDrivername & ", " & strLocation & ", " & Description)
objTextFile.WriteLine (strPrinterName & VBtab & strServerName & VBtab & strDrivername & VBtab & strLocation & VBtab)
objTextFile.Write ( Description)
'objTextFile.WriteLine (strServerName)

Next
MsgBox "fertig"
objTextFile.Close

 
Hello Jokada,

Description property is optional. Imagine if it returns a null. To avoid trouble, use the trick:
[tt]objTextFile.Write (Description & "")[/tt]

regards - tsuji
 
Hi tsuji

Yes i know it is optional but i also know that all the Printers in the domain have a description.
The funny thing is i can't convert it to a string and thats why he doesn't want to write it in the file.
I don't know what kind of information i'getting through

These solutions also give errors
Description = Trim((arrPrinter(0,IntI)))
Description = Cstr((arrPrinter(0,IntI)))

Any ideas?? Anyone?

regards
jokada
 
Jokada,

It's typical behavior of a null return.
Code:
a=null
on error resume next
wscript.echo a
if err<>0 then
    wscript.echo "wscript.echo null" & vbcrlf & _
        hex(err.number) & err.description
    err.clear
end if
b=cstr(a)
if err<>0 then
    wscript.echo "cstr(null)" & vbcrlf & _
        hex(err.number) & err.description
    err.clear
end if
You can device similar check that textstreamobject.write null produces the equally runtime error.

- tsuji
 
Further note:

A straightforward check would be to echo out typename
wscript.echo typename(Description)
A null would return a string "Null".

- tsuji
 
Hi tsuji
thanks for the reply
i do get indeed the first error when i runned your example

I tried a workaround but still the same error

If IsNull((arrPrinter(0,IntI))) Then
Description = "nullvalue"
Else
Description = arrPrinter(0,IntI)
'if i use here Description = Cstr(arrPrinter(0,IntI))
'it gives the same error here(this line): type mismatch
End If

if i leave the Cstr out the error is bach again when writing to the file
objTextFile.Write ( Description & "")

 
Jodaka,

Could you change "printerName" to "printerPath" and test it?

- tsuji
 
Further notes:

The reason I suggest you to change the printerName is that I read your recovery of data from the array in reserver order of the sql. This shouldn't be. Hence, if anything goes wrong with the "description", it is the first field retrieved. Furthermore, WinNT printerName in LDAP should better be printerPath.

- tsuji
 
tsuji,

thanks for the replies
I changed the PrinterName to PrinterPath like you said but then i get an error executing the statement. I found this URL on microsoft. Printerpath isn't mentionned in here.

For the reverse order of retrieving the data, i've also noticed it and i wasn't able to tell WHY. But the funny thing is when i leave the description out, its still in a weird order.
greets
 
Jokada,

I would take ms documentation seriously. But they are not always correct. I am thinking in fact .name (rather than .printerpath) as the exact replacement. You can try to look into the IADsPrintQueue sdk documentation.

Also, still wondering what happens to the reversed ordering!

- tsuji
 
tsuji,


thanks again for your comment
i noticed there are multiple PrintQueue Objects
There is a PrintQueue Object in ADSI for LDAP providers, whitch only has 2 interfaces (IADsPrintqueue and IADsPrintqueueOperations)
The other object is part of the ADSI Object Model for WinNT providers. this has 4 interfaces (IADs, IADsPrintqueue ,IADsPrintqueueOperations ,IADsPropertyList)

in my script i use :
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select printerName, drivername, ServerName, location, description from " _
& " 'LDAP://DC=tke,DC=intra' where objectClass='printQueue' "

I think this uses the ADSI for LDAP

now i have annother working! script as described here:
to get all users.

you'll see that they use the winNt object, but when i change the sFilter parameter to "Printqueue" or "Printer(s)" i cant read a damn thing from the AD...

I'm a bit confused why this tend to work and the other script doesn't.

Annother problem could be that the executor of the script only has user privileges to the domainserver, and therefor cant read the description. but then again why does it work to read all other information from the printers AND all user information???

Greets
jokada
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top