Help with simple VBScript

Status
Not open for further replies.

Disco_Stu

Ars Scholae Palatinae
1,335
I have a basic VB script that maps users drives but now need to combine it with one that maps printers and I'm not sure how? Could someone please help me out with this?


Code:
'Map Drive
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath
strDriveLetter = "s:"
strRemotePath = "\\server\files"

Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
WScript.Quit

Code:
' Script to add printers during user logon
On Error resume Next ' ver important otherwise users will get errors on subsequent reconnections
' Create the variable to hold printer connections
Dim Our_Printers
' Create variables for each printer string
' You can create as many as you like here
Dim Printer_1, Printer_2
' Initialise our printers
Printer_1 = "\\print-server\printer_1" 
Printer_2 = "\\print-server\printer_2"

' Initialise the printer connections object
Set Our_Printers = CreateObject("WScript.Network") 
' Connect each of our printers
Our_Printers.AddWindowsPrinterConnection Printer_1 
Our_Printers.AddWindowsPrinterConnection Printer_2
WScript.Quit

This is me trying to put the two together but probably completely wrong. Per the script the On Error Resume Next is important for the Printer mapping part but I read that is non-global so I'm not sure where it should go.
Code:
Option Explicit
On Error resume Next
Dim objNetwork, strDriveLetter, strRemotePath
Dim objOur_Printers, strPrinter_1, strPrinter_2
strDriveLetter = "s:"
strRemotePath = "\\server\files"
strPrinter_1 = "\\print-server\printer_1" 
strPrinter_2 = "\\print-server\printer_2"
Set objNetwork = CreateObject("WScript.Network")
Set objOur_Printers = CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
objOur_Printers.AddWindowsPrinterConnection Printer_1 
objOur_Printers.AddWindowsPrinterConnection Printer_2
WScript.Quit
 

MirrorImage

Seniorius Lurkius
7
Noticed 2 things. One kinda jumped out at me, and the other I suspect is your bug.

What jumped out at me is having multiple variables set to "WScript.Network". While there are a few objects where you'd want to declare multiple instances for multiple functions, WScript.Network sounds like an interface for me. You could get away with only declaring one variable against it and then call it with both the mapping and printer functions. Purely semantics though.

Your bug is on lines 12 and 13. You've referenced the Printer variables by their old name (Printer_#), but you declared and set them with an "str" prefix (strPrinter_#).


As for On Error Resume Next, what that basically does is it switches the script to ignore runtime errors. Those misreferenced variables would qualify. As for why it's "needed" in your code, I would imagine it's because if a printer is already assigned on the computer, the Add...Connection function would fail and take the rest of the script with it.
 

Disco_Stu

Ars Scholae Palatinae
1,335
Thanks for the response. I redid it realizing I probably didn't need the separate Dim ojbOur_Printers and separate createobject. How does this look?

Code:
Option Explicit
On Error resume Next
Dim objNetwork, strDriveLetter, strRemotePath, strPrinter_1, strPrinter_2
strDriveLetter = "s:"
strRemotePath = "\\server\files"
strPrinter_1 = "\\server\printer" 
strPrinter_2 = "\\server\printer"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
objNetwork.AddWindowsPrinterConnection strPrinter_1 
objNetwork.AddWindowsPrinterConnection strPrinter_2
WScript.Quit
 

Disco_Stu

Ars Scholae Palatinae
1,335
Edit doesn't work. Anyway if anyone could step in here and do some editing for me I would greatly appreciate it.

Edit Edit, Forget it. I'm completely dumping that script for a simple one I found online which works without messing with variables.

Code:
On Error resume Next

dim objNetwork

set objNetwork = WScript.CreateObject("WScript.Network")

'map the S: drive to a network location

objNetwork.MapNetworkDrive "S:", "\\server\files"
 
objNetwork.AddWindowsPrinterConnection "\\server\Printer1"
objNetwork.AddWindowsPrinterConnection "\\server\Printer2"

'set default printer

objNetwork.SetDefaultPrinter "\\server\Printer1"
WScript.Quit
 
Status
Not open for further replies.