Jump to content

Map a Windows network printer silently - WMI, RunDLL32, PrintUI.dll


Colyn1337
 Share

Recommended Posts

Use these functions to capture and restore windows network printers. This script was used in a Win XP/7 environment running 2008 R2 servers.

Global Const $PrinterList = (@DesktopDir & "printerlist.csv")

Func PrinterBackup()

Local $csvfile

ConsoleWrite($PrinterList & @LF)

$csvfile = FileOpen($PrinterList, 10)
; Pull default printer and write it to the first line of the file
$objWMI = ObjGet("winmgmts:.rootCIMV2")
$colPrinters = $objWMI.ExecQuery("SELECT * FROM Win32_Printer", "WQL", 0x10 + 0x20)
If IsObj($colPrinters) Then
For $objPrinter In $colPrinters
If $objPrinter.Default = "True" And $objPrinter.Network = "-1" Then
ConsoleWrite("Writing printer " & $objPrinter.Name & @CR)
Sleep(500)
FileWriteLine($PrinterList, $objPrinter.Name & @CR)
EndIf
Next
EndIf
; Then grab the rest of the printers, if any
$objWMI = ObjGet("winmgmts:.rootCIMV2")
$colPrinters = $objWMI.ExecQuery("SELECT * FROM Win32_Printer", "WQL", 0x10 + 0x20)
If IsObj($colPrinters) Then
For $objPrinter In $colPrinters
If $objPrinter.Network = "-1" And $objPrinter.Default = ("") Then
ConsoleWrite("Writing printer " & $objPrinter.Name & @CR)
Sleep(500)
FileWriteLine($PrinterList, $objPrinter.Name & @CR)
EndIf
Next
EndIf

FileClose($csvfile)

If FileExists($PrinterList) = 0 Then
TrayItemSetState($ItemPrinterRestore, $TRAY_DISABLE)
ElseIf FileExists($PrinterList) <> 0 Then
TrayItemSetState($ItemPrinterRestore, $TRAY_ENABLE)
EndIf

EndFunc ;==>PrinterBackup

Use this function to restore printers....

Global Const $PrinterList = (@DesktopDir & "printerlist.csv")

Func PrinterRestore()

Local $PrinterArray[1], $x, $csvfile

If FileExists($PrinterList) Then
$csvfile = FileReadLine($PrinterList)
If Not $csvfile = ("") Then
_FileReadToArray($PrinterList, $PrinterArray)
For $x = 1 To 1
RunWait(@ComSpec & " /c " & "rundll32 printui.dll,PrintUIEntry /in /n" & $PrinterArray[$x], "", @SW_HIDE)
RunWait(@ComSpec & " /c " & "rundll32 printui.dll,PrintUIEntry /y /n" & $PrinterArray[$x], "", @SW_HIDE)
Next
For $x = 2 To $PrinterArray[0]
RunWait(@ComSpec & " /c " & "rundll32 printui.dll,PrintUIEntry /in /n" & $PrinterArray[$x], "", @SW_HIDE)
Next
EndIf
EndIf

EndFunc ;==>PrinterRestore
Edited by Colyn1337
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...