Hey, Scripting Guy! How can I write a script that will automatically share all the local printers on a computer?
— CD
Hey, CD. You know, when we redid the Hey, Scripting Guy! archive we created a new category devoted exclusively to printing. When we started to fill out the archive pages, however, we discovered – much to our surprise – that we hadn’t really answered that many printing-related questions. We thought we’d been answering a bunch of printing questions, but obviously we’d simply been hiding those questions in the closet and hoping that they’d go away.
Why have we been so reluctant to answer printing questions? After all, isn’t it really easy to manage printers in Windows XP or Windows Server 2003?
As a matter of fact, it is; the enhancements made to the WMI printing classes represent one of the real strengths of scripting in Windows XP and Windows 2003. The problem is that the vast majority of these enhancements represent capabilities not found in Windows 2000; to be honest, print management in Windows 2000 isn’t anything to write home about. (Or to write daily scripting columns about.) The solutions to most printing questions are XP/2003-specific: they simply don’t apply to Windows 2000. Because we prefer to offer generic solutions that work on most Windows platforms, we kind of hurry past printing questions and hope that no one notices.
But what the heck; we’ll go ahead and answer this one. Just keep in mind that, if you’re running Windows 2000, today’s column doesn’t apply to you. This is XP/2003 only. And, yes, we know that’s not fair. But that’s just the way it is.
Here’s a script that will share out all the local printers on a computer:
strComputer = “.” Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)Set colInstalledPrinters = objWMIService.ExecQuery _ (“Select * from Win32_Printer Where Network = FALSE”)
i = 1
For Each objPrinter in colInstalledPrinters objPrinter.Shared = TRUE objPrinter.ShareName = “Printer” & i objPrinter.Put_ i = i + 1 Next
The script begins by connecting to the WMI service, then issues this query to return a collection of all the local printers; that is a collection of all the printers that are connected to a local printer port:
Set colInstalledPrinters = objWMIService.ExecQuery _ (“Select * from Win32_Printer Where Network = FALSE”)
After issuing the query we set the value of a counter variable named i to 1; we’ll use this variable later on to give each of the local printers a unique share name. We then set up a For Each loop to loop through our collection of local printers.
And what do we do inside that loop? Well, for each printer we start off by setting the value of the Shared property to True; this will cause the printer to be shared. The Shared property, by the way, is a new addition to the Win32_Printer class; this property does not appear in the version of WMI found in Windows NT or Windows 2000, which explains why this script will not work on those platforms.
Because each shared printer requires a unique share name, we use this line of code to set the value of the ShareName property to the word Printer plus the value of our counter variable:
objPrinter.ShareName = “Printer” & i
The first time through the loop i is equal to 1, hence the ShareName will be set to Printer1. We then call the Put_ method to actually write the changes to the printer. Don’t leave out this line of code; if you do, your changes won’t be saved, and the printer will not be shared. We then increment the value of i by 1, and loop around again. That means the next printer in the collection will have the share name Printer2, the one after that will have the share name Printer3, and so on.
Like we said, this script works great, provided you’re running Windows XP or Windows Server 2003. If you’re running some other version of Windows – oh, look we’re out of time for today. Gotta go; bye!
0 comments