The following script can be used to back up all network printers connected to Windows. Its been tested on Windows XP (SP2 and SP3) and Windows 7.
The script simply outputs to the command window the syntax for a restoration script. Running this script from the command line, you’ll be able to control where this output goes rather than just being stuck with wherever I guess you want it.
Simply paste the following code to a vbs file of your choice then run it using the following command:
cscript <scriptname>.vbs //nologo > <target-filename>.vbs
Its a bit of a fiddle but it does mean this script is fire and forget. You’ll never need to change it if you server changes its name etc.
'Script to automatically generate a printer recreation script. 'The output of this script should be piped out to a .vbs file 'using the //nologo switch
Wscript.Echo "'Automatically generated script to restore printers" Wscript.Echo "'This script should be executed to recreate network printers only" Wscript.Echo "" Wscript.Echo "Set objNetwork = CreateObject(""WScript.Network"")"
strComputer = "." strDefaultPrinter = "NotSet"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
For Each objPrinter in colPrinters If objPrinter.Attributes And 64 Then Wscript.Echo "' ** Local printer included for information only: " & objPrinter.Name Else strPrinterType = "objNetwork.AddWindowsPrinterConnection " Wscript.Echo strPrinterType & """" & objPrinter.Name & """" If objprinter.Default = True then StrDefaultPrinter = objPrinter.Name End If End If Next
If StrDefaultPrinter <> "NotSet" then Wscript.Echo "" Wscript.Echo "objNetwork.SetDefaultPrinter " & """" & StrDefaultPrinter & """" End If
Well, that’s a bit of a mouthful. Let’s break this script down so we know what’s going on.
The first portion of the script (I’m ignoring the preamble comment lines) is all about setting up variable and strDefaultPrinter which will be used later to set a default printer if its needed. We also make sure the script’s pointing at the current PC.
Next, we call the GetObject function to climb into the guts of Windows and set ourselves up for asking it a question.
The next line simply asks for a list of all installed printers.
The rest of the script steps through the list (For Each…. Next) and runs a couple of “If” statements to work out if the printer’s local (add a comment line) or networked (add a line to recreate the printer) then finally if it is a network printer, is it the default one.
When we’ve finished the loop, we drop out to a final If statement, this simply checks if the default printer was set by one of the network printers triggering the change. If not, it does nothing, if there is a default printer set, it adds the line.
Well, that’s it for now. Please slap a comment on the post if there’s any part you’d like walking through. As usual, this script represents a collection from a number of sources being skimmed for various lines of code.