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
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)
Please log in to answer
Posted by:
andler
19 years ago
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 :-)
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.
so that the conversation will remain readable.