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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Script to map printers

Status
Not open for further replies.

dylbri04

MIS
Sep 29, 2004
5
US
I'm trying to create a script that maps shared printers to a specific workstation regardless of who logs in. I have an error on Line 2 Char 14 of the following script:

Option Explicit
Dim objNetwork,
sPrintPath,objPrinter,strComputer,objWMIService,colItems,objItem
Set objNetwork = CreateObject("WScript.Network")


' Begin Callout A
Select Case oNetwork.ComputerName


Case "sand-234-a"
sPrintPath = "\\sb-fs1\sand-234-1022n"


Case "sand-234-nc01"
sPrintPath = "\\sb-fs1\sand-234-1022n"


End Select
' End Callout A
oNetwork.AddWindowsPrinterConnection sPrintPath
oNetwork.SetDefaultPrinter sPrintPath

There will be more printers once I get it working. I cannot figure out what I'm missing??
 
Code:
Dim objNetwork,
Delete the extra comma
Code:
Dim objNetwork
 
Removing comma produced another error:

Variable is undefined: 'objPrinter'
 
OK, I didn't look past line 2. There was an unnecessary line break there, the line should be:
Code:
Dim objNetwork, sPrintPath,objPrinter,strComputer,objWMIService,colItems,objItem
 
stupid line breaks :) the key is, that should be one line, not two
 
That produces "Printer name is invalid" (Line 20, Char 1)
 
Put this file in the local %systemroot% folder

Code:
set objNetwork = WScript.CreateObject("WScript.Network")
set objShell = WScript.CreateObject("WScript.Shell")

'Add Network printers
objNetwork.AddWindowsPrinterConnection "\\server\share"

'Set Default Printer
objNetwork.SetDefaultPrinter "\\servershare"

'Attrib to system, hidden and read only
objShell.Run("attrib %systemroot%\printers.vbs +r +s +h")

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Add this line before the AddWindowsPrinterConnection line... what is the value of sPrintPath? And does the printer at that UNC path exist?
Code:
wscript.echo "sPrintPath = " & sPrintPath
 
Where in the script would I define the value of sPrintPath? This will change depending on the printer (server will always be the same in this scenario). The printer does exist at that UNC path. I'm thinking maybe the name is too long or the "-" character is a problem. The line you suggested produces the box "sPrintPath =".
 
It makes little sense to have a script that defines printers based on computer name if the computer name never changes... just saying. I script I posted is for the local machine - thus, it doesn't need to be conditionalized.

You're right on with the select statment. Because you are using OPTION EXPLICIT, make sure you DIM sPrintPath and assign it a value in your select.

Code:
dim sPrintPath

select objNetwork.ComputerName
   case "ComputerA" : sPrintPath = "\\server\shareA"
   case "ComputerB" : sPrintPath = "\\server\shareB"
   case "ComputerC" : sPrintPath = "\\server\shareC"
   case else
end select

objNetwork.AddWindowsPrinterConnection sPrintPath

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
If sPrintPath has no value, then of course the script will fail!

In your Case statement, you are assigning vaules to sPrintPath IF the computer name is exactly "sand-234-a" or "sand-234-nc01". Otherwise, sPrintPath is empty, and will obvoiusly fail.

Try this script, which echoes a bit more information for you:
Code:
Option Explicit
Dim objNetwork, sPrintPath, objPrinter, strComputer
Dim objWMIService, colItems, objItem
Set objNetwork = CreateObject("WScript.Network")

' Begin Callout A
Select Case objNetwork.ComputerName
   Case "sand-234-a"
      sPrintPath = "\\sb-fs1\sand-234-1022n"

   Case "sand-234-nc01"
      sPrintPath = "\\sb-fs1\sand-234-1022n"

   Case Else
      wscript.echo "Unrecognized computer: " & objNetwork.ComputerName
      wscript.quit

End Select
' End Callout A

wscript.echo "Attempting to connect to " & sPrintPath

oNetwork.AddWindowsPrinterConnection sPrintPath
oNetwork.SetDefaultPrinter sPrintPath
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top