Jump to content
Sign in to follow this  
rklingaman

For Next statement in Printer Script going slow

Recommended Posts

rklingaman

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

Share this post


Link to post
Share on other sites
rklingaman

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.

Share this post


Link to post
Share on other sites
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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  

×