/build/static/layout/Breadcrumb_cap_w.png

Adding printers using VBscript

Hi

I want to add network printers using vbs

To make it easy to maintain I want to use an ini-file containing information about the available printers.
I was thinking of using AD, but it was difficult to resolve the computers location and their available printers from the information registered in AD.

I have made a script as you can see below, the problem is using a variable as argument to the function AddPrinter??

Can someone please help me.

br,
andler




ini-file

[Location]
NumberOfPrinters=3
Printer0="\\server\printer01"
Printer1="\\server\printer02"
Printer2="\\server\printer03"

----------------------------------------
Vbs-file

NumberOfPrinters = ReadIni("Printers.ini", "Location", "NumberOfPrinters")

For i=0 to NumberOfPrinters-1
strPrinter = ReadIni("Printers.ini", "Location", "Printer"&i)

AddPrinter strPrinter ' -> Returns error : "Printername is invalid" Code:80070709
AddPrinter "\\server\printer01" ' -> Adding the printer successfully

Next


Function AddPrinter(strPrntr)
objNetwork.AddWindowsPrinterConnection strPrntr
End Function


Function ReadIni(file, section, item)
''''''''''''''''''''''''''''''''''''''''''''
' Purpose:
' Returns a value from an ini file.
' Sample:
' strRes = readini("test.ini", "main", "user")
''''''''''''''''''''''''''''''''''''''''''''

Dim LineKey, line, ini, intEQpos

item=trim(ucase(item))
section=ucase(section)
ReadIni = ""

If FileSysObj.FileExists( file ) Then
Set ini = FileSysObj.OpenTextFile( file, 1, False)
Do While ini.AtEndOfStream = False
line = Ucase(trim(ini.ReadLine))
If line = "[" & section & "]" Then
line = Trim(ini.ReadLine)

Do While (Left(line, 1) <> "[")
intEQPos = Instr(1,Line,"=")-1
If intEQPos > 0 Then
LineKey = Left(Line,intEQPos)
Else
LineKey = ""
End If

If trim(ucase(LineKey))=item then
ReadIni = trim(Mid(Line, instr(1,Line,"=")+1))
Exit Do
End If

If ini.AtEndOfStream Then exit do
line = trim(ini.ReadLine)
Loop

Exit Do
End If
Loop
ini.Close
End If
End Function

0 Comments   [ + ] Show comments

Answers (1)

Posted by: andler 18 years ago
Orange Belt
0
Found my problem

The problem was quotes inside ini-file

Changed from

[Location]
NumberOfPrinters=3
Printer0="\\server\printer01"
Printer1="\\server\printer02"
Printer2="\\server\printer03"

to
[Location]
NumberOfPrinters=3
Printer0=\\server\printer01
Printer1=\\server\printer02
Printer2=\\server\printer03

and all is working fine :-)
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ