Jump to content

Adding a printer via login script


Recommended Posts

I'm running the following each time a user logs in:

Run("rundll32 printui.dll,PrintUIEntry /if /b ""HP Color LaserJet 5550 PS"" /f \\server\Shared\files\printers\HP5550\PS\hpc5550d.inf /r ""IP_192.xx.xxx.xxx"" /m ""HP Color LaserJet 5550 PS"" ",@SystemDir)

I have this in an autoit script to add this printer via logon script. How can I add a check to see if the printer has already been added so it doesn't add a new copy of the printer each time the user logs in? Any help would be appreciated.

Link to comment
Share on other sites

$objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$colPrinters = $objWMIService.ExecQuery("Select * from Win32_Printer") ; Query all printers installed.

For $objItem In $colPrinters
   $PrinterName = StringSplit($objItem.Name, "\") ; Split the server(s)/printer(s) name up.
   If NOT @Error Then ; Check if there is a "\" in the string.
      $iLastElement = UBound($PrinterName) - 1 ; Retrieve the printer name.
      If $PrinterName[$iLastElement] <> "HP Color LaserJet 5550 PS" Then ; Check if the printer is already installed.
         Run("rundll32 printui.dll,PrintUIEntry /if /b ""HP Color LaserJet 5550 PS " & _
             "/f \\server\Shared\files\printers\HP5550\PS\hpc5550d.inf /r " & _
             "IP_192.xx.xxx.xxx"" /m ""HP Color LaserJet 5550 PS"" ", @SystemDir)
         Exit ; Install the printer then exit so that the printer isn't installed again.
      EndIf
   EndIf
NextoÝ÷ Ù´ëÊØZ´Z½é÷öÖÞzvëhµë-Úk¢«,µ»­²ëÊáßîN¼­+¢¶¬íz»Þ®È¨«­¢+ØÀÌØí½©]5%MÉÙ¥ô=©Ð ÅÕ½ÐíÝ¥¹µµÑÌèÀäÈìÀäÈìÅÕ½ÐìµÀì
½µÁÕÑÉ9µµÀìÅÕ½ÐìÀäÈíɽ½ÐÀäÈí¥µØÈÅÕ½Ðì¤(ÀÌØí½±AÉ¥¹ÑÉÌôÀÌØí½©]5%MÉÙ¥¹áEÕÉä ÅÕ½ÐíM±Ð9µI=4]¥¸ÌÉ}AÉ¥¹ÑÈ]!I9µôÌäí!@
½±½È1ÍÉ)ÐÔÔÔÀALÌäìÅÕ½Ðì¤ìEÕÉäÑ¡Ìäí!@
½±½È1ÍÉ)ÐÔÔÔÀALÌäìÁÉ¥¹Ñȸ()%9½ÐÀÌØí½±AÉ¥¹ÑÉ̹
½Õ¹ÐQ¡¸ì%Ñ¡ÁÉ¥¹Ñȥ͸ÌäíÐ¥¹Íѱ±Ñ¡¸¥¹Íѱ°¥Ð¸(IÕ¸ ÅÕ½ÐíÉÕ¹±°ÌÈÁÉ¥¹ÑÕ¤¹±°±AÉ¥¹ÑU%¹ÑÉä½¥½ÅÕ½ÐìÅÕ½Ðí!@
½±½È1ÍÉ)ÐÔÔÔÀALÅÕ½ÐìµÀì|($ÅÕ½Ðì½ÀäÈìÀäÈíÍÉÙÈÀäÈíM¡ÉÀäÈí¥±ÌÀäÈíÁÉ¥¹ÑÉÌÀäÈí!@ÔÔÔÀÀäÈíALÀäÈí¡ÁÔÔÔÁ¹¥¹½ÈÅÕ½ÐìµÀì|(ÅÕ½Ðí%A|Ääȹáà¹ááà¹ááàÅÕ½ÐìÅÕ½Ðì½´ÅÕ½ÐìÅÕ½Ðí!@
½±½È1ÍÉ)ÐÔÔÔÀALÅÕ½ÐìÅÕ½ÐìÅÕ½Ðì°MåÍѵ¥È¤)¹%

I'm also looking into the AddPrinterConnection WMI so you don't have to use Run(), but it won't work for Windows 2000/NT and Windows 98/95 and I'm having slight trouble coding/understanding it ^^.

Edited by Larry

qq

Link to comment
Share on other sites

@Burrup

Excellent work on your second example. That seems perfect.

I however would love to create a WMI install script as I need to install a printer to roughly 25 machines here at one of my clients.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

I knew my SQL experience would come in handy sometime lol :D! I'll look into it when I get home JS :).

Excellent work compadres that did the trick! :lol: Now that script only worked if the printer port existed, so I did a little bit of research and found the code to add a tcp port. :nuke:

$objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")

$colPrinters = $objWMIService.ExecQuery("Select Name FROM Win32_Printer WHERE Name = ''HP Color LaserJet 5550 PS'") ; Query the 'HP Color LaserJet 5550 PS' printer.

$objNewPort = $objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_

$objNewPort.Name = "IP_192.xx.xxx.xxx"

$objNewPort.Protocol = 1

$objNewPort.HostAddress = "192.xx.xxx.xxx"

$objNewPort.PortNumber = "9999"

$objNewPort.SNMPEnabled = False

$objNewPort.Put_

If Not $colPrinters.Count Then ; If the printer isn't installed then install it.

Run("rundll32 printui.dll,PrintUIEntry /if /b ""HP Color LaserJet 5550 {Estimator}"" /f \\server\Shared\files\printers\HP5550\PS\hpc5550d.inf /r ""192.xx.xxx.xxx"" /m ""HP Color LaserJet 5550 PS"" ",@SystemDir)

EndIf

Now one last question, how can I query to see if the port exists? Then create it if it doesn't exist. :P

Link to comment
Share on other sites

Now one last question, how can I query to see if the port exists? Then create it if it doesn't exist. :P

This shows me all the printer names and ports on my computer:

On Error Resume Next

Const wbemFlagReturnImmediately = &h10

Const wbemFlagForwardOnly = &h20

arrComputers = Array("JEFF-DELL")

For Each strComputer In arrComputers

WScript.Echo

WScript.Echo "=========================================="

WScript.Echo "Computer: " & strComputer

WScript.Echo "=========================================="

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Printer", "WQL", _

wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems

WScript.Echo "Name: " & objItem.Name

WScript.Echo "PortName: " & objItem.PortName

WScript.Echo

Next

Next

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

; Generated by AutoIt Scriptomatic
$strComputer = InputBox("Printer Shares", "Enter IP or Server Name")

If Not @error Then
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $Output = ""
    $Output = $Output & "Computer: " & $strComputer  & @CRLF
    $Output = $Output & "==========================================" & @CRLF
    $objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PrinterShare", "WQL", _
                $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj ($colItems) then
        For $objItem In $colItems
            $Output = StringTrimRight (StringMid ($objItem.Dependent, StringInStr ($objItem.Dependent, '="') +2), 1)
            If MsgBox (1, "WMI Output", $Output) = 2 Then ExitLoop
            $Output = ""
        Next
    Else
        MsgBox (0, "WMI Output", "No WMI Objects Found for class: " & "Win32_PrinterShare")
    EndIf
EndIf

The above was transcribed by Gary. I can't test it because I don't have Admin rights on this computer :P.

Edited by Larry

qq

Link to comment
Share on other sites

$objNewPort.Put_

This command doesn't work when I run this script off a network share. Any ideas?

I am trying to run this script for non admin users by using runasset. I am using another script that all it does is call this script with admin credentials. Should I be using a specific macro that allows $objNewPort.Put_

to execute properly?

Attached is the error message.

error.bmp

Edited by giggity
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...