http://www.computerperformance.co.uk/
The following scripts demonstrate how to connect to a network shared printing device. In the first step, the script creates a Network Object. Next, the AddWindowsPrinterConnection method, one of the Network object's methods, performs the connection operation. The AddWindowsPrinterConnection method takes two parameters: the name you wish to call the printer and the Universal Naming Convention (UNC) path to the printing device.
// JScript. var net; net = new ActiveXObject("WScript.Network"); net.AddWindowsPrinterConnection
("\\\\ServerName\\PrinterName"); ' VBScript. Dim net Set net = CreateObject("WScript.Network") net.AddWindowsPrinterConnection
"\\ServerName\PrinterName"
The following script demonstrates how to set the desired default printing device. In the first step, the script creates a Network Object. Next, the SetDefaultPrinter method, one of the Network object's methods, performs the operation. The SetDefaultPrinter method takes a single parameter, the name of the printer, which is either the local printer name or a remote printer name using the Universal Naming Convention (UNC) path to the printing device.
// JScript. var net; net = new ActiveXObject("WScript.Network"); net.SetDefaultPrinter
("\\\\ServerName\\PrinterName"); ' VBScript. Dim net Set net = CreateObject("WScript.Network") net.SetDefaultPrinter
\\ServerName\PrinterName
In my example, the VBScript removes a mapped network printer called HPPrinter: Here is the key command: RemovePrinterConnection "\\ServerPrint1\HPPrinter"
Copy the script below (into memory). Now open Notepad, paste the text, when you have customised the script, save the file with a .vbs extension. For Example, RemPrinter.vbs
Note: You must change the strPrintPath to the name of print server and share on YOUR network, other wise the script will not work.
' VBScript
' Guy Thomas January 2004
' VBScript to Remove a Network Printer Dim WshPrinter, strPrintPath strPrintPath = "\\ServerPrint1\HPPrinter" Set WshPrinter = WScript.CreateObject("WScript.Network") WshPrinter.RemovePrinterConnection strPrintPath, true, true
RemovePrinterConnection - Is the method which removes the network printer.
strPrintPath is a variable to the UNC path of the printer.
true, true These switches force the removal of the printer.
CreateObject is the command which WSH uses to attach to the network.
To create a script for your print server, copy and paste the example above into notepad.exe.
Save the script with a .vbs extension, and double click the file to test it.
'Best Practice' dictates that we should take steps to control errors. Another reason for error handling is that it saves time troubleshooting when you can narrow down the problem to one section of the script. In practice you have three, choices, write a statement that says ignore errors and carry on processing, allow errors to surface naturally, or add error handling statements (best), which will give you valuable information on where in the script the error is arising.
What the 'On Error Resume Next' statement does is defer error handling, and suppress a system message. The script will carry on running but you may be storing up problems. There is counter statement called 'On Error GoTo 0' which turns off 'On Error Resume Next'. This means that you can have different error handling for different sections of you script.
On Error Resume Next Err.Clear Set objContainer = GetObject("LDAP://" & strOUContainer)
Note 1: Without an 'On Error Resume Next' command, any run-time error will result in pop-up message box with an error message and the code will execute no further than the error.
Use the Clear method to explicitly reset the Err object once an error has been handled. When you use deferred error handling with On Error Resume Next, you reset the err.raise to zero with a statement err.Clear.
Note 1: VBScript calls the Clear method whenever you Exit a Sub or Exit a function.
If Err.Number <> 0 Then Err.Clear On Error GoTo 0 Wscript.Echo "Edit the LDAP path to your OU " & strOUContainer Wscript.Quit End If
Note 1: This error handling routine uses the If ... Then ... End If construction.
Note 2: Here is a classic use of the Wscript.Echo method. The & strOUContainer is a neat touch to let you know in the message box exactly what is wrong with LDAP path.
Note 3: If you actually see this error it means you need to find the variable strOUContainer and change the value to your domain.
Note 4: These error handling statements occur all through the script, each with an appropriate message.
The fact that VBScript is an interpreted language rather than a compiled language means that you can expect more runtime errors. To trap these bugs you need to build in error handling statements which pinpoint coding errors such spelling mistakes in variables.
Thomas Jakobsen | Nordborgvej 6 | 4200 Slagelse | Tlf. 5853 0892 | Email: thomas@nordborgvej.dk