rklingaman Posted July 7, 2010 Share Posted July 7, 2010 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 More sharing options...
rklingaman Posted July 8, 2010 Author Share Posted July 8, 2010 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 More sharing options...
water Posted July 8, 2010 Share Posted July 8, 2010 (edited) 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 July 8, 2010 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now