PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 17 - Default Printer

Contents of Guy's Scripting Ezine No 17 -  Default Printer

This week's secret

This weeks secret is that it is you the reader, who drives me to master scripting.  You ask questions that I had never thought of, you want scripts to do things that I had never imagined.  As I love to learn, keep those questions coming!

ˆ

Printer Scripts - Part 2.

Last week's burning question was 'Can you use a Computer Start Up script to assign a printer to the machine'  The answer still seems to be NO.  Why I keep asking the question is because:

a)  One of my clients asked me to create such a script.

b) On the face of it seems a reasonable request.

c) No-one has produced absolute proof that it cannot be done.

Setting the default printer

I have had several requests for a script which will set the default printer.  The scenario is this, you press the print button in PowerPoint and instead of getting a beautiful colour picture, you get a black and white laser print out.  The cause of the problem was that the wrong printer was set as the default.  To prevent having to manually set the default printer every time you use a machine, let us create a script which sets the default printer and assign it at logon.

Copy the following code.  Now open Notepad, paste in the text, when you have changed the UNC path, save the file with a .vbs extension.  Example DefaultPrinter.vbs. 

Change "\\ServerName\PrinterName" to reflect a real server and printer on your network.  If you get an 800 WSH error message then you probably forgot to change the ServerName or PrinterName.

 

'  Example VBScript to set the local Default Printer
'  Guy Thomas February 2004.
'  ******************************
Option Explicit
Dim objPrinter
Set objPrinter = CreateObject("WScript.Network")
objPrinter.SetDefaultPrinter "\\ServerName\PrinterName"
' End of example VBScript

 

Learning Points

  • Trap: You change ServerName to the local machine name.  It should of course be the name of the network server (The name you used in the add printer script).  This is what I mean by a trap, you are sitting at a machine called XPSpecial you set the path to XPSpecial\HP6L instead, where the actual device is installed and shared.  For example of BigServer\HP6L.
  • Line 6 can you see what sort of Object is being created?  Yes we need the Network Object.
  • On line 7 the Method that is applied to the printer - is SetDefaultPrinter.
  • Challenge: you could add an extra line at the end
    WScript.Echo "Your DEFAULT printer  : " & UNCpath1
  • If you are a perfectionist you could add WScript.Quit

 

ˇ

A script to enumerate your printers.

On the surface, this script just displays the network printers, it does not actually create anything new.  From a learning point of view we explore a rich seam of features.    The word Enumerate - meaning to count, while this not a word often used in conversation, there is a whole family of Enum methods which are useful in VBScripts.  As well as EnumPrinterConnections which I use in the script below, there is also EnumNetworkDrives.

The good news is that this script will run on any Windows machine, you do not have to alter any UNC paths.  Just copy and paste into notepad.  Save with a .VBS extension for example EnumPrinter.vbs.

 

 

' Example VBScript to display your  printers
' Guy Thomas February 2004.
' ******************************
Option Explicit
Dim oPrinter, oNetwork, iCount
Set oNetwork = WScript.CreateObject("WScript.Network")
Set oPrinter = oNetwork.EnumPrinterConnections
For iCount = 0 to oPrinter.Count - 1 Step 2
WScript.Echo "Printer Port " & oPrinter.Item(iCount) & _

" = " & oPrinter.Item(iCount+1)
Next
WScript.Quit
' End of example VBScript

 

Learning Points

  • Names in VBScript are case insensitive, so feel free to use capitals in the middle of a word if it makes more sense.
  • The key point in this script is the loop construction.  What happens is this:  The 'For .... Next', with its iCount counter, cycles through all the printers.
  • Step 2 puzzled me at first, but then I realized that each 'cycle' gets the port name - Line 9 Item(iCount), then the share name - Item(iCount+1); so that is why you need a value of 2 for the Step command.  
    Challenge: Try the script without Step 2 (or substitute Step 1), and note the different output, ending with the inevitable error message.
  • If you ever have to type the script form scratch, remember that Enum methods are plural methods EnumPrinterConections.
  • Incidentally I have chosen the short single letter prefix in the variables.  For example, oNetwork rather than objNetwork.  My hidden agenda is to give you variety so that you can choose a convention that suits you.

Summary

Where you, or your users, have multiple printers it makes sense to set one of the printers as the default.  Why leave it to chance when you can add a line to your logon script?

Enumerate means to count.  VBScript has a family of methods using variations of Enum.  While Enum does nothing in itself, it allows you to expose objects, test them, then decide which ones you wish to configure.

See more on Printer Logon Scripts here.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.