{"id":54,"date":"2010-12-13T13:20:24","date_gmt":"2010-12-13T13:20:24","guid":{"rendered":"http:\/\/blogs.oucs.ox.ac.uk\/darrencollins\/?p=54"},"modified":"2024-04-10T10:06:47","modified_gmt":"2024-04-10T09:06:47","slug":"automate-ip-printer-port-and-driver-install","status":"publish","type":"post","link":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/2010\/12\/13\/automate-ip-printer-port-and-driver-install\/","title":{"rendered":"Automate IP printer port and driver install"},"content":{"rendered":"<p>I&#8217;ve just had a nice little scripting challenge to solve:<\/p>\n<p>ICTST have all moved recently to nice offices off of the High Street. \u00a0We have a nice new printer here and no print server, so we needed to automate the installation of a local printer IP port and the driver for the printer.<\/p>\n<p>After a bit of testing I came up with the following method. \u00a0I&#8217;ve used WMI to create a local IP port and then used PrintUI.dll to deliver the driver and setup the printer on the new IP port. \u00a0I could have used various WMI queries to do all of it, but I like the fact that <a href=\"http:\/\/download.microsoft.com\/download\/8\/2\/c\/82cffcfa-56f6-4fc3-bfa5-80cd84793871\/PrintUIUsersGuide.doc\">PrintUI.dll<\/a> was created for just this purpose and it does it very well indeed.<\/p>\n<p>As I&#8217;ve scripted it below, the script needs to be in the same folder as the driver including the .inf file. \u00a0It has been tested on Windows XP SP3 only so far.<\/p>\n<p>The script also outputs a message box if it finds a printer already on the IP address port specified.<\/p>\n<p>I&#8217;ve generalised some of the details:<\/p>\n<div style=\"width: 650px;overflow: auto\">\n<pre>Set wshShell = CreateObject(\"WScript.Shell\")\r\nstrCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, \"\")\r\nstrIPAddress = \"111.1.222.333\"\r\nstrComputer = \".\"\r\nstrPrinterName = \"RICOH MFP Office 2nd Floor\"  '## Can be any text you want, this shows print dialogs.\r\nstrDriverName = \"RICOH Aficio MP C3300 PCL 6\"  '## MUST be the exact text as in the printer driver .inf file.\r\nstrLocation = \"New Office 2nd Floor\"           '## Can be anything\r\nstrInfFile = strCurrDir &amp; \"OEMSETUP.INF\"       '## the full path to the printer driver .inf file.  In my example the VBScript was in the same folder as the driver &amp; inf file.\r\n\r\nSet objWMIService = GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\r\nSet colPrinters = objWMIService.ExecQuery(\"Select * From Win32_Printer Where PortName = 'IP_\" &amp; strIPAddress &amp; \"' \")\r\nFor Each objPrinter in colPrinters\r\n    MsgBox \"Unable to install printer, printer already found on port 'IP_\" &amp; strIPAddress &amp; \"'.\" &amp; VbCrlf &amp; VbCrlf &amp; \"Found: \u00a0\" &amp; objPrinter.DeviceID, vbExclamation + vbSystemModal, \"Printer Port already assigned\"\r\n    WSCript.Quit 114001\r\nNext\r\n\r\nSet objNewPort = objWMIService.Get(\"Win32_TCPIPPrinterPort\").SpawnInstance_\r\nobjNewPort.Name = \"IP_\" &amp; strIPAddress\r\nobjNewPort.Protocol = 1\r\nobjNewPort.HostAddress = strIPAddress\r\nobjNewPort.PortNumber = \"9100\"\r\nobjNewPort.SNMPEnabled = False\r\nobjNewPort.Put_\r\n\r\nwshShell.Run \"rundll32 printui.dll,PrintUIEntry \/if \/b \"\"\" &amp; strPrinterName &amp; \"\"\" \/f \"\"\" &amp; strInfFile &amp; \"\"\" \/r \"\"IP_\" &amp; strIPAddress &amp; \"\"\" \/m \"\"\" &amp; strDriverName &amp; \"\"\"\", 1, True\r\n\r\nSet colPrinters = objWMIService.ExecQuery(\"Select * From Win32_Printer Where DeviceID = '\" &amp; strPrinterName &amp; \"' \")\r\nFor Each objPrinter in colPrinters\r\n    objPrinter.Location = strLocation\r\n    objPrinter.Put_\r\nNext\r\nWScript.Quit 0<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve just had a nice little scripting challenge to solve: ICTST have all moved recently to nice offices off of the High Street. \u00a0We have a nice new printer here and no print server, so we needed to automate the &hellip; <a href=\"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/2010\/12\/13\/automate-ip-printer-port-and-driver-install\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":68,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8374],"tags":[8382,8383,8384,8380,8381],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-work","tag-print","tag-printer","tag-printui-dll","tag-script","tag-vbscript"],"_links":{"self":[{"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":16,"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/posts\/54\/revisions\/294"}],"wp:attachment":[{"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs-new.it.ox.ac.uk\/darrencollins\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}