KenE Posted February 9, 2006 Posted February 9, 2006 (edited) I don't know if this has been covered elsewhere, but even if it has, it's worth posting again. This allows you to completely automate the installation of a printer, network or otherwise, and retain any custom settings. (Windows 2000 & XP, maybe 2003? Only) See: www.robvanderwoude.com/2kprintcontrol.html This will work under almost any scripting language, but of course, AutoIt does it best! My code below does not retain printer settings, I never got around to completing that, but you can find all the info you need to add that functionality at the above link. ;Replace Printer Name with name of your choice, we use an asset tag number, must match .ini file $printer = "Printer Name" Call("InstallPort") Call("InstallPrinter") Func InstallPort() ;Install standard TCP/IP port for printer FileChangeDir(@AutoItExe) $Protocol = IniRead("Printers.ini", $printer, "Protocol", "") $Version = IniRead("Printers.ini", $printer, "Version", "") $HostName = IniRead("Printers.ini", $printer, "HostName", "") $IPAddress = IniRead("Printers.ini", $printer, "IPAddress", "") $HWAddress = IniRead("Printers.ini", $printer, "HWAddress", "") $PortNumber = IniRead("Printers.ini", $printer, "PortNumber", "") $SNMPCommunity = IniRead("Printers.ini", $printer, "SNMP Community", "") $SNMPEnabled = IniRead("Printers.ini", $printer, "SNMP Enabled", "") $SNMPIndex = IniRead("Printers.ini", $printer, "SNMP Index", "") RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" & $HostName, "Protocol", "REG_DWORD", $Protocol) RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" & $HostName, "Version", "REG_DWORD", $Version) RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" & $HostName, "HostName", "REG_SZ", $HostName) RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" & $HostName, "IPAddress", "REG_SZ", $IPAddress) RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" & $HostName, "HWAddress", "REG_SZ", $HWAddress) RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" & $HostName, "PortNumber", "REG_DWORD", $PortNumber) RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" & $HostName, "SNMP Community", "REG_SZ", $SNMPCommunity) RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" & $HostName, "SNMP Enabled", "REG_DWORD", $SNMPEnabled) RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" & $HostName, "SNMP Index", "REG_DWORD", $SNMPIndex) If $HostName = "" Then MsgBox(4096, "Error", "Invalid HostName read from Printers.ini for printer: " & $printer) Exit EndIf ;Necessary to use printer without rebooting RunWait(@ComSpec & " /c " & "NET STOP ""Print Spooler""", "", @SW_HIDE) RunWait(@ComSpec & " /c " & "NET START ""Print Spooler""", "", @SW_HIDE) EndFunc Func InstallPrinter() ;Install the printer FileChangeDir(@AutoItExe) $HostName = IniRead("Printers.ini", $printer, "HostName", "") $Description = IniRead("Printers.ini", $printer, "Description", "") $Driver = IniRead("Printers.ini", $printer, "Driver", "") RunWait(@ComSpec & " /c " & "RUNDLL32 printui.dll,PrintUIEntry /dl /n """ & $Description & """ /q", "", @SW_HIDE) RunWait(@ComSpec & " /c " & "RUNDLL32 printui.dll,PrintUIEntry /if /b """ & $Description & """ /f ""%windir%\inf\ntprint.inf"" /q /r """ & $HostName & """ /m """ & $Driver & """ /z /u", "", @SW_HIDE) EndFunc You'll also need a Printers.ini file in the same directory as the compiled script. Here's an example: [Printer Name] Protocol="00000001" Version="00000001" HostName="hostname" IPAddress="" HWAddress="" PortNumber="9100" SNMP Community="public" SNMP Enabled="00000001" SNMP Index="00000001" Description="HP LaserJet 4si Series" Driver="HP LaserJet 4si" To determine the correct settings for a TCP/IP port, install the printer once using the Windows wizard, then open regedit and browse to: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\[Hostname or IP] Copy the settings from this location to use un your script. To determine the driver name, same as above, install the printer, then look at the properties of the printer and copt the driver name from there. Not a local administrator on the machine? Compile and embed this in another script that uses the following code to run under an admin account: FileInstall("C:\AU3Include\Printers.exe", @TempDir & "\Printers.exe") FileInstall("C:\AU3Include\Printers.ini", @TempDir & "\Printers.ini") RunAsSet("Administrator", @Computername, "AdminPassword", 0) WinSetState("Setup Printers", "", @SW_HIDE) FileChangeDir(@TempDir) RunWait("Printers.exe", @TempDir, @SW_MAXIMIZE) FileDelete(@TempDir & "\Printers.exe") FileDelete(@TempDir & "\Printers.ini") Edited February 9, 2006 by KenE
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now