Jump to content

For Next statement in Printer Script going slow


Recommended Posts

I have been working on a printer script and have it all working. Though when I pull a list of printers from a remote machine it goes very slowly before it populates the list.

I played with placing a message box in the script to find out what part of it was taking so long and came up with:

For $objPrinter in $colEvents

$result1 &= $objPrinter.DeviceID & '|'

Next

That particular part of the script is where it is taking so long. Any help and information will be appreciated.

Func GetPrinterList1()
    Local $result1, $colEvents, $objWMIService, $objWMIRemote
    
    ;MsgBox(1,"Test",$server & " " & $user & " " & $key) ;To Test if values are being set
    
    $objWMIRemote = ObjCreate("WbemScripting.SWbemLocator")
    
    $objWMIService = $objWMIRemote.ConnectServer _
        ($server, "root\CIMV2", $user, $key)
    
    $colEvents = $objWMIService.ExecQuery('Select * From Win32_Printer') ;Querys for list of printers
        
    For $objPrinter in $colEvents
        $result1 &= $objPrinter.DeviceID & '|'
    Next   
    
    ;MsgBox(1,"Test",$colEvents) ;To Test if values are being set
    
   $result1 = StringLeft($result1,StringLen($result1) - 1)
   
    Return $result1;return the list of printers
EndFunc
Link to comment
Share on other sites

Maybe if someone could explain exactly how the for next statement is working out the data so that I could come up with a more efficient way to split up the data and display it in a list box.

Just seems like something should be wrong when it can query the server almost instantly then takes 10-20 seconds before it displays the data.

Link to comment
Share on other sites

Based on WMI Scriptomatic I've rewritten your script and added a bit of code for time measurement. Runs in about 2 seconds for a PC with 10 printers.

Global $result1
Global $strComputer = "."   ; Please enter the remote computer name here
Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20

$start = TimerInit()
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
ConsoleWrite("ObjGet took " & TimerDiff($start) & " milliseconds" & @CRLF)
$start = TimerInit()
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
ConsoleWrite("ExecQuery took " & TimerDiff($start) & " milliseconds" & @CRLF)
$start = TimerInit()

For $objPrinter In $colItems
    $result1 &= $objPrinter.DeviceID & '|'
Next
ConsoleWrite("Loop took " & TimerDiff($start) & " milliseconds" & @CRLF)
$start = TimerInit()

$result1 = StringLeft($result1, StringLen($result1) - 1)
ConsoleWrite($result1 & @CRLF)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...