Quick VBScript code help, adding network printers

Status
Not open for further replies.

Jonzo

Ars Scholae Palatinae
1,231
Hi guys, wondering if someone can help me out here for a second.. trying to write a VBScript that will add all shared printers from a given print server, to a local Windows XP Workstation. Sounds easy enough, except in this siutation when being executed from the Windows XP Workstation, the number of printers, and the the names of the printers on the Print Server would not be known. This is because we have thousands of PCs with hundreds of print servers across the country.

My question is, if I request input from the user (to get print server name), can I add ALL ("*") the printers on that print server to the PC I'm executing the script from? I know you can do it by specifying the printer name to add in the script, but the script wont know the printer name.

Any help is appreciated thanks.
 

Dac

Ars Legatus Legionis
12,495
Subscriptor
If you called this 'getprinters.cmd'

Code:
@net view \\%1 | find /I "Initrode" >netview.txt
@FOR /F "Tokens=1" %%I IN (netview.txt) DO @echo %%I

(the @ in front of commands, stop them echoing)

That generates a list of printers.

@net view \\%1 | find /I "Initrode" >netview.txt

pipes the output for net view, looks for Initrode (all our printernames have company_name (I've replaced that with Initrode)) and outputs only those lines to netview.txt.

@FOR /F "Tokens=1" %%I IN (netview.txt) DO @echo %%I

loops over netview.txt and takes the first space-or-tab delimited token, and outputs just that text string (which should be the name of the printer).

You could capture that to a plain text file, like this:

Code:
getprinters.cmd \\myprintserver >printers.txt

InitrodeP121
InitrodeP121_Duplex
InitrodeP122
InitrodeP122_Duplex
InitrodeP133
InitrodeP133_Duplex
etc

Or, modify the CMD file so that it calls a VBScript routine for each printer name.

addallprinters.cmd

Code:
@net view \\%1 | find /I "_" >netview.txt
@FOR /F "Tokens=1" %%I IN (netview.txt) DO @addprinter.vbs %%I

Where addprinter.vbs is your code to add a printer, to local workstation, from \\myprintserver\printerqueue.

There are many ways to go about doing this. I couldn't find a mechanism to do net view from VBScript.
 

Grimreaper

Smack-Fu Master, in training
97
Try this, replace printservername with your server's name:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")

Const ForReading = 1
Dim strServerName
Dim strListOfPrintersFile
Dim strCommandLine

strServerName = "printservername"
strListOfPrintersFile = objFSO.GetSpecialFolder(2) & "\printers.txt"
strCommandLine = "cmd.exe /c net view \\" & strServerName & " |find /I " & Chr(34) & "Print" & Chr(34) & " >" & Chr(34) & strListOfPrintersFile & Chr(34)

WshShell.Run strCommandLine, 0, True

Set PrintersFile = objFSO.OpenTextFile(strListOfPrintersFile, ForReading)
Do Until PrintersFile.AtEndOfStream
strPrinterName = PrintersFile.ReadLine
If strPrinterName <> "" Then
If Not Mid(strPrinterName, 1, 16) = "Shared resources" Then
strPrinterName = Mid(strPrinterName, 1, InStr(1, strPrinterName, " ") - 1)
WshNetwork.AddWindowsPrinterConnection strPrinterName
End If
End If
Loop
PrintersFile.Close
objFSO.DeleteFile strListOfPrintersFile
 
Status
Not open for further replies.