Jump to content
Sign in to follow this  
manicjonah

Compare Printer Drivers Between Two Computers

Recommended Posts

manicjonah

I went looking for a script that would allow me to compare the list of installed drivers on Computer A with a list of installed drivers from Computer B and found that someone had created a very similar script using AutoIT.  So today I downloaded AutoIt and plugged this guy's code into it to see what it's all about.  I really like what he's done but I need to change it and the change is a little over my head.  I'm hoping one of the resident experts here on the forum can help me out.

As I mentioned, the primary purpose of the script is to compare the printers that are installed on Computer A with the printers that are installed on Computer B.  I would like to modify it so that it actually compares the printer drivers, not the installed printers.  

Your help is certainly appreciated.

#include <Array.au3>

$configFile = @ScriptDir & "\" & "PrinterCompare.ini"

Local $sMaster = InputBox("Reference Server", "Enter the name of the server you will compare others to.  Use a period if you want to use the local computer. Do not use quotes.", "", " M")
If $sMaster = "" Then Exit
Local $sCompare = InputBox("Comparrison Servers", "Enter the name of the server you will compare to the Reference server.  Separate multiple computers in 'CompareServers' with a Pipe symbol '|'",""," M")
If $sCompare = "" Then Exit
IniWrite($configFile, "General", "MasterServer", $sMaster)
IniWrite($configFile, "General", "CompareServers", $sCompare)
$masterServer = IniRead($configFile, "General", "MasterServer", ".")
$compareServers = IniRead($configFile, "General", "CompareServers", "")



$aCompare = StringSplit($compareServers, "|", 2)

Global $aPrintersMaster
$aPrintersMaster = _GetPrinters($masterServer)

For $compare In $aCompare

    If Not (_ComparePrintes($masterServer, $compare)) Then MsgBox(0,"","Skipping server: " & $compare)
Next

FileDelete ( "PrinterCompare.ini" )

Func _ComparePrintes($masterServer, $compareServer)
    ;MsgBox(0,"",$masterServer & " " & $compareServer)
    Local $aPrintersCompare
    $aPrintersCompare = _GetPrinters($compareServer)

    ;If we have an issue, skip this computer
    If Not IsArray($aPrintersCompare) Then Return 0

    Global $nomatch = ""
    Local $missingPrinters = "Printers on " & $compareServer & " but not on " & $masterServer & ":" & @CRLF
    Local $missingPrinters2 = "Printers on " & $masterServer & " but not on " & $compareServer & ":" & @CRLF

    For $x = 1 To (UBound($aPrintersCompare, 1) - 1)
        $match = _ArraySearch($aPrintersMaster, $aPrintersCompare[$x][0], 0, 0, 0, 0, 1, 0)
        If $match > 0 Then
            If ($aPrintersMaster[$match][1] <> $aPrintersCompare[$x][1]) Or ($aPrintersMaster[$match][2] <> $aPrintersCompare[$x][2]) Then
                $nomatch &= $masterServer & ": " & $aPrintersMaster[$match][0] & " Driver: " & $aPrintersMaster[$match][1] & " Port: " & $aPrintersMaster[$match][2] & @CRLF
                $nomatch &= $compareServer & ": " & $aPrintersCompare[$x][0] & " Driver: " & $aPrintersCompare[$x][1] & " Port: " & $aPrintersCompare[$x][2] & @CRLF & @CRLF

            EndIf
        Else
            $missingPrinters &= $aPrintersCompare[$x][0] & @CRLF
            ;msgbox(0,"","Could not find printer " & $aPrintersCompare[$x][0])
        EndIf

    Next
    For $x = 1 To (UBound($aPrintersMaster, 1) - 1)
        $match = _ArraySearch($aPrintersCompare, $aPrintersMaster[$x][0], 0, 0, 0, 0, 1, 0)
        If $match < 0 Then
            $missingPrinters2 &= $aPrintersMaster[$x][0] & @CRLF
        EndIf
    Next



    _MsgBoxScrollable(0, "Missing Printers", $missingPrinters & @CRLF & @CRLF & $missingPrinters2)

    _MsgBoxScrollable(0, "Printers with different configuration", $nomatch)
    Return 1
EndFunc   ;==>_ComparePrintes



Func _GetPrinters($computer = ".")
    Global $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $computer & "\root\cimv2")
    If @error Then
        MsgBox(0, "WMI Query Error", "Error connecting to the server " & $computer & ": " & Hex(@error, 8))
        Return 0
    EndIf

    Global $colItems = $objWMIService.ExecQuery("Select * from Win32_Printer")

    Global $result = ""

    Local $aPrinters[1][3]
    $aPrinters[0][0] = 0

    For $objItem In $colItems
        $aPrinters[0][0] += 1
        ReDim $aPrinters[UBound($aPrinters, 1) + 1][3]

        $aPrinters[UBound($aPrinters, 1) - 1][0] = $objItem.Name
        $aPrinters[UBound($aPrinters, 1) - 1][1] = $objItem.DriverName
        $aPrinters[UBound($aPrinters, 1) - 1][2] = $objItem.PortName


    Next
    ;MsgBox(0, "Information", $result)
    Return $aPrinters

EndFunc   ;==>_GetPrinters



Func _MsgBoxScrollable($mb_Icon, $mb_Title, $mb_Text, $mb_Time = '')
    ;This function is from: http://www.autoitscript.com/forum/topic/48485-how-do-i-make-a-messagebox-scollable/page__hl__msgbox++scroll
    ;It was modified slightly to help fix the size of the box
    Local $StrnLenText = LongestStringLen($mb_Text)
    ;Msgbox(0,"",$StrnLenText)
    Local $NumberOfLines = (UBound(StringSplit($mb_Text, @CRLF)) - 1) * 6.5
    If (160 + $NumberOfLines) >= @DesktopHeight Then $NumberOfLines = @DesktopHeight - 200
    If $StrnLenText + 150 >= @DesktopWidth - 200 Then $StrnLenText = @DesktopWidth - 200
    Local $Button1Txt = "OK"
    Local $Button2Txt = "Cancel"
    Local $MsgValue = 0
    Local $Timer = ''
    Local $ScrollLabel1 = -1, $ScrollLabel2 = -1
    Local $iMsgBox = GUICreate($mb_Title, $StrnLenText + 150, 100 + $NumberOfLines, -1, -1, 0x00400000, 0x00000008)

    Local $DefaultEditStyle = 0
    If $NumberOfLines >= (@DesktopHeight - 200) Or $StrnLenText + 150 >= 200 Then $DefaultEditStyle = 3150016
    Local $Edit = GUICtrlCreateEdit($mb_Text, 60, 10, $StrnLenText + 80, $NumberOfLines + 30, _
            BitOR($DefaultEditStyle, 64 + 128 + 2048 + 4), 0x990)
    Local $IconID = GUICtrlCreateIcon(@SystemDir & "\User32.dll", $mb_Icon, 10, 10, 35, 35)
    $Button1 = GUICtrlCreateButton($Button1Txt, 10 + ($StrnLenText / 2), 45 + $NumberOfLines, 60 + StringLen($Button1Txt), 25)
    $Button2 = GUICtrlCreateButton($Button2Txt, 80 + ($StrnLenText / 2), 45 + $NumberOfLines, 60 + StringLen($Button2Txt), 25)



    GUISetState()
    If $mb_Time <> 0 Then $Timer = TimerInit()
    ControlFocus($iMsgBox, "", $IconID)

    While 1
        $imsg = GUIGetMsg()
        Select
            Case $imsg = $Button1
                $MsgValue = 6
                ExitLoop
            Case $imsg = $Button2
                $MsgValue = 7
                ExitLoop
            Case $imsg = $ScrollLabel1
                ControlSend($iMsgBox, "", $Edit, "{PgDn}")
                ControlFocus($iMsgBox, "", $IconID)
            Case $imsg = $ScrollLabel2
                ControlSend($iMsgBox, "", $Edit, "{PgUp}")
                ControlFocus($iMsgBox, "", $IconID)
            Case $mb_Time <> 0
                If TimerDiff($Timer) / 1000 >= $mb_Time Then ExitLoop
        EndSelect
    WEnd
    GUIDelete($iMsgBox)
    Return $MsgValue
EndFunc   ;==>_MsgBoxScrollable

Func LongestStringLen($sText)
    ;Used by _MsgBoxScrollable()
    Local $BiggestStr = 0
    Local $sSplit = StringSplit($sText, @CRLF)

    For $i = 1 To UBound($sSplit) - 1
        If StringLen($sSplit[$i]) > $BiggestStr Then $BiggestStr = StringLen($sSplit[$i])
    Next

    Return $BiggestStr * 5 + 50
EndFunc   ;==>LongestStringLen

Thanks for the help,

MJ

Share this post


Link to post
Share on other sites
JLogan3o13

Are you looking to compare just the driver name, or the version of the driver? And are you looking to specify a printer and have it compare the driver on two different machines, or check a bunch of machines to see if they have the same driver as your baseline machine?


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
manicjonah

JLogan3o13 we meet again!  Thanks for your reply.  I am very simply looking to compare the list of installed printer drivers on machine A with the list on machine B.  The very top portion of the script includes my additions - prompting the user to name a reference machine and then prompting him to name other computers to compare to the reference machine.  Driver version would be great but I didn't get that far.

The script is supposed to help me out of situations like the one I was in yesterday when I added a new server to a terminal server farm only to find out that it was short several print drivers we had no record of ever installing.  I'm told by the senior admins that they used to compare the drivers manually but I just don't have time for that . . . thus the script ;)

Once I have this figured out (with your help I hope) I'll probably create something similar to compare programs installed on a reference machine with programs installed on another machine.  We were also missing an application on that server that another Admin assured us had been added to the image a long time ago.

Thanks again for the reply.  Any help you can give me is appreciated.

MJ

Share this post


Link to post
Share on other sites
JLogan3o13

I'll take a look at integrating the new section into your code when I get a chance, but would suggest you start by looking at the Win32_PrinterDriver class instead of Printer. Something like this should give you an idea of what information you can pull from that class:

#include <MsgBoxConstants.au3>

$oWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$aItems = $oWMI.ExecQuery("SELECT * FROM Win32_PrinterDriver", "WQL", 0x10 + 0x20)

If IsObj($aItems) then
   For $driver In $aItems
       MsgBox($MB_OK, $driver.Name, "Caption: " & $driver.Caption & @CRLF & "Config File: " & $driver.ConfigFile & @CRLF & "Description: " & $driver.Description & _
       @CRLF & "File Path: " & $driver.FilePath & @CRLF & "Start Mode: " & $driver.StartMode & @CRLF & "Status: " & $driver.Status & @CRLF & _
       "Version: " & $driver.Version)
   Next
Else
   Msgbox($MB_ICONERROR,"Print Drivers","Objects Found for Print Driver class")
Endif

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
manicjonah

Thanks again JLogan3o13!  I'll have a look at what the code you posted does.  I'm looking forward to seeing the driver information integrated into the script I posted too.

Thanks!

MJ

Share this post


Link to post
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
Sign in to follow this  

×