Search the Community
Showing results for tags 'printers'.
-
Hello. I did create these few functions several months ago. I post here, if it can interest someone. These functions based on WMI queries allow you to manage printers : add / delete printer, driver, port, or obtain configuration, set default printer ... I let you discover it with the code. Here is the list of the available functions : _PrintMgr_AddLocalPort _PrintMgr_AddLPRPort _PrintMgr_AddPrinter _PrintMgr_AddPrinterDriver _PrintMgr_AddTCPIPPrinterPort _PrintMgr_AddWindowsPrinterConnection _PrintMgr_CancelAllJobs _PrintMgr_CancelPrintJob _PrintMgr_EnumPorts _PrintMgr_EnumPrinter _PrintMgr_EnumPrinterConfiguration _PrintMgr_EnumPrinterDriver _PrintMgr_EnumPrinterProperties _PrintMgr_EnumPrintJobs _PrintMgr_EnumTCPIPPrinterPort _PrintMgr_Pause _PrintMgr_PortExists _PrintMgr_PrinterExists _PrintMgr_PrinterSetComment _PrintMgr_PrinterSetDriver _PrintMgr_PrinterSetPort _PrintMgr_PrinterShare _PrintMgr_PrintTestPage _PrintMgr_RemoveLocalPort _PrintMgr_RemoveLPRPort _PrintMgr_RemovePrinter _PrintMgr_RemovePrinterDriver _PrintMgr_RemoveTCPIPPrinterPort _PrintMgr_RenamePrinter _PrintMgr_Resume _PrintMgr_SetDefaultPrinter And some examples : #include <Array.au3> #include "PrintMgr.au3" _Example() Func _Example() ; Remove a printer called "My old Lexmark printer" : _PrintMgr_RemovePrinter("My old Lexmark printer") ; Remove the driver called "Lexmark T640" : _PrintMgr_RemovePrinterDriver("Lexmark T640") ; Remove the TCP/IP printer port called "TCP/IP" _PrintMgr_RemoveTCPIPPrinterPort("MyOLDPrinterPort") ; Add a driver, called "Samsung ML-451x 501x Series", and driver inf file is ".\Samsung5010\sse2m.inf" _PrintMgr_AddPrinterDriver("Samsung ML-451x 501x Series", "Windows NT x86", @ScriptDir & "\Samsung5010", @ScriptDir & "\Samsung5010\sse2m.inf") ; Add a TCP/IP printer port, called "MyTCPIPPrinterPort", with IPAddress = 192.168.1.10 and Port = 9100 _PrintMgr_AddTCPIPPrinterPort("MyTCPIPPrinterPort", "192.168.1.10", 9100) ; Add a printer, give it the name "My Printer", use the driver called "Samsung ML-451x 501x Series" and the port called "MyTCPIPPrinterPort" _PrintMgr_AddPrinter("My Printer", "Samsung ML-451x 501x Series", "MyTCPIPPrinterPort") ; Set the printer called "My Printer" as default printer _PrintMgr_SetDefaultPrinter("My Printer") ; Connect to the shared printer "\\192.168.1.1\HPDeskjetColor") _PrintMgr_AddWindowsPrinterConnection("\\192.168.1.1\HPDeskjetColor") ; List all installed printers Local $aPrinterList = _PrintMgr_EnumPrinter() _ArrayDisplay($aPrinterList) ; List all printers configuration Local $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration() _ArrayDisplay($aPrinterConfig) ; List all installed printer drivers Local $aDriverList = _PrintMgr_EnumPrinterDriver() _ArrayDisplay($aDriverList) ; Retrieve the printer configuration for the printer called "Lexmark T640" $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration("Lexmark T640") _ArrayDisplay($aPrinterConfig) ; Add a local printer port (for a file output) _PrintMgr_AddLocalPort("c:\temp\output.pcl") ; Remove the local port _PrintMgr_RemoveLocalPort("c:\temp\output.pcl") ; Enum a print job Local $aJobList = _PrintMgr_EnumPrintJobs() _ArrayDisplay($aJobList) EndFunc ;==>_Example Download link : PrintMgr_Example.au3 PrintMgr.au3
-
So, this is not one of my neatest scripts, but I haven't contributed much to the forum lately, and this was a help to me this week. I was migrating about 40 print queues from an old print server to a new one. Since this entails removing the old printer and creating the new printer for each user on each PC for each print queue used (for about 350 users), doing this manually was out of the question. I just added this to the logon scripts to run once per user per machine. If anyone finds the need to make a similar migration, please feel free to use/modify this. Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ;don't want to crash out if there is a problem. #include "PrintMgr.au3" ;https://www.autoitscript.com/forum/topic/155485-printers-management-udf/ #include "ServiceControl.au3" ;https://www.autoitscript.com/forum/topic/6487-updated-service-control-funtions/ #include <array.au3> Global $sOldPrintServer = "oldservername" Global $sNewPrintServer = "newservername" Global $sDefaultPrinter = _CleanName(_GetDefaultPrinter()) ;If you change print queue names below, you may need to take that into account and modify $sDefaultPrinter as well. Global $aPrinters = _PrintMgr_EnumPrinter() If @error Then Exit Global $aReplaced[1] = [0] Global $sTempName ;remove printers that are mapped to old server For $iLoop = 1 To $aPrinters[0] If Not StringInStr($aPrinters[$iLoop], $sOldPrintServer) Then ContinueLoop ;not a printer we are concerned with $sTempName = $aPrinters[$iLoop] ConsoleWrite("Removing " & $sTempName & @CRLF) _PrintMgr_RemovePrinter($sTempName) $sTempName = _CleanName($sTempName) ;If you need to change a print queue name, modify $sTempName here before adding it to the array. _ArrayAdd($aReplaced, $sTempName) $aReplaced[0] = $aReplaced[0] + 1 Next ;Stop and re-start print spooler. This alleviated problems on a few machines. ConsoleWrite("Stopping Spooler" & @CRLF) _StopService(@ComputerName, "spooler") Sleep(1000) ConsoleWrite("Starting Spooler" & @CRLF) _StartService(@ComputerName, "spooler") Sleep(1000) ;remap to the new server (assuming all of the print queues have the same share name) For $iLoop = 1 To $aReplaced[0] Local $isdefault = False If $aReplaced[$iLoop] = $sDefaultPrinter Then $isdefault = True ConsoleWrite("Adding \\" & $sNewPrintServer & "\" & $aReplaced[$iLoop] & "(" & $isdefault & ")" & @CRLF) _PrintMgr_AddWindowsPrinterConnection("\\" & $sNewPrintServer & "\" & $aReplaced[$iLoop]) If $isdefault Then _PrintMgr_SetDefaultPrinter("\\" & $sNewPrintServer & "\" & $aReplaced[$iLoop]) Next Func _GetDefaultPrinter() ; CyberSlug - 18 Nov 2004 Local $key, $default, $defPrtNm If @OSType = "WIN32_WINDOWS" Then $key = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print\Printers" $defPrtNm = RegRead("HKEY_CURRENT_CONFIG\System\CurrentControlSet\Control\Print\Printers", "Default") Else ;WIN_NT type $key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers" $default = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device") $defPrtNm = StringLeft($default, StringInStr($default, ",") - 1) EndIf Return $defPrtNm EndFunc ;==>_GetDefaultPrinter Func _CleanName($sName) Local $sTempName = $sName $sTempName = StringReplace($sTempName, "\\" & $sOldPrintServer & "\", "") Return $sTempName EndFunc ;==>_CleanName Func MyErrFunc() ;just dump the info to the console and keep going Local $sErrNum Local $sMsg $sErrNum = Hex($oMyError.Number, 8) $sMsg = "Error Number: " & $sErrNum & @CRLF $sMsg &= "WinDescription: " & $oMyError.WinDescription & @CRLF $sMsg &= "Script Line: " & $oMyError.ScriptLine & @CRLF ConsoleWrite($sMsg) EndFunc ;==>MyErrFunc