jguinch

Printers Management UDF

58 posts in this topic




Thanks for sharing. Might be useful someday.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks,guy!It's useful for me!


董小姐,你微笑的时候很美,就像安河桥下,清澈的水...

Share this post


Link to post
Share on other sites

Can be very useful here too. Once in a while I try to automate something with print queues and ports. I will have it a go in a few days.

Thanks jguinch

Share this post


Link to post
Share on other sites

I just came across this UDF and it looks very promising. I'd like to have some documentation on it. For instance, when adding a printer to a computer and I know the driver is not present on the machine, do I use the _AddPrinter or do I use the _AddPrinterDriver followed by the _AddPrinter? Do I need to install the driver first? If so, why is the final parameter only limited to the OS options of Win9x, Win351, NT40 and Win2k?

I'm trying to automate adding printers in various locations that do not have servers. All the clients are 64-bit Windows 7 and all printers will be direct TCP/IP port printing to a Canon imageRUNNER (various models).


Who lied and told you life would EVER be fair?

Share this post


Link to post
Share on other sites

Thank you for your interest.

Yes, you must install the driver first (with _AddPrinterDriver).

The default value for $sVersion parameter is 3 : it seems Windows 2000 and upper (in your case, I think). You will rarely have to install an old driver on a recent System... So you can use the default value.

For the $sDriverPlatform value, the values can be Windows NT x86 or Windows x64. (depending of the type of driver that you want to install).



 

Share this post


Link to post
Share on other sites

I made a script to get a list of all printers then remove them all. which works for the most part, but some printers are not removed and get greyed out with a yellow triangle on their icon. the property page is blank.


What is what? What is what.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Biatu : what type are printers that have not been removed ? Shared ?

In _RemovePrinter, you must use serverprinterShare for shared printers.

 

Nope, they are not shared, or network printers. they are connected via USB port. I test various printers, and the printer list gets pretty full so I run the script.

The issue used to be printer jobs from other users, so i made a func to remove all jobs from all printers.

Then another dell status service was locking some printers (afaik) so i stop the service.

that didnt work completely,so now I restart the spooler service, which remove most. all except for the "Dell 5330 Mono Laser MFP"

The env is win7 enterprise x64.

Edited by Biatu

What is what? What is what.

Share this post


Link to post
Share on other sites

_AddPrinter is not working either, i keep getting error dialog...

Error Number: -2147352567

WinDescription: Exception occurred.

Description: Generic Failure

Source: $WbemObjectEx

#Include <Printers.au3>

$Name="PrintNet.1"
$Driver="Dell 2335dn MFP,3,Windows x64"
$File="C:\Test1.pcl";@ScriptDir&"\"&$Name&".pcl"
_AddPrinter($Name,$Driver,"C:\Test1.pcl")

Thanks


What is what? What is what.

Share this post


Link to post
Share on other sites

Hello Guys,

 

I am new to autoit. I want  to run a program called PrinterInfo.exe, search for the the specific word " STATUS_PAPEROUT", if found then send email to desired recipient.

So far I have only able to run program, take a window capture save the window capture and then email the window capture to the recipient. But this is not what i want.

 

I have read posts which suggest that I should use WinGetText to retrieve the word, however I have still no idea as to how to search for the specific word " STATUS_PAPEROUT" and if present then send email.

 

Please help

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I use this to determine the printer type:

http://<Printer IP>:5200/Printer.xml

then i use the printer's network interface for everything else.

Currently i am doing research with PCL, SNMP, etc.

Edited by Biatu

What is what? What is what.

Share this post


Link to post
Share on other sites

Biatu, you have to install the driver before installing the printer : it could be done with _AddPrinterDriver (look at the example above).

Next, you have to know the port on which you want to install the printer (if needed, create a network port with _AddTCPIPPrinterPort.

And finally, you can add the printer...

I just downlod the Dell 2335dn MFP driver, so the installation can be done like this :

#Include "printers.au3"

Local $sPrinterName = "My Dell 2335dn Printer"
Local $sDriverName = "Dell 2335dn MFP"
Local $sPortIP = "192.168.1.250"
Local $sPortName = "IP_192.168.1.250"
Local $iPortnumber = 9100

_AddPrinterDriver($sDriverName, "Windows x64", @ScriptDir & "\Printer\PCL\WINXP_VISTA_64\", @ScriptDir & "\Printer\PCL\WINXP_VISTA_64\sdf1m.INF")
_AddTCPIPPrinterPort($sPortName, $sPortIP, $iPortnumber)
_AddPrinter($sPrinterName, $sDriverName, $sPortName)

Share this post


Link to post
Share on other sites

Thank you jguinch, will check out func on monday.


What is what? What is what.

Share this post


Link to post
Share on other sites

If I remove a printer using your func will I still need to remove port, or is that auto?

Thanks


What is what? What is what.

Share this post


Link to post
Share on other sites

_RemovePrinter does not remove the port. You must do it after.

Same thing for the driver.

Tried that, gives error. So i left it out, and the port does not show up on ports list when new printer is added or on the existing ones. So i assume that it's removed automatically


What is what? What is what.

Share this post


Link to post
Share on other sites

Hmm, is this a global (all users) add/delete of local (current profile)?

Speaking exclusively of printserver printers that normally only add per user, unlike local printesr that default to all users. 

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

  • Similar Content

    • nikink
      By nikink
      Hi all,
      I have a bit of code that works on my old Win10 PC, that fails on my new Win10 PC, and I think the only significant difference is the version of Autoit - old PC has 3.3.12, new has 3.3.14.
      I couldn't find anything mentioned in the change logs though, so perhaps I'm wrong.
      Anyway, the code to replicate my issue is:
      Test('username', 'DOMAIN') ; THIS ERRORS: ;Test('localun', 'DOMAIN') ; THIS ERRORS: ;Test(' ', ' ') ; THIS ERRORS: ;Test('', '') ; THIS ERRORS: ;Test('localun', '') ; THIS ERRORS: ;Test('', 'DOMAIN') Func Test($un, $dom) $compName = 'PCNAME' $FullName = '.' $Description = '.' ; get the WIM object $objWMIService = ObjGet("winmgmts:\\" & $compName & "\root\cimv2") ; get default user full name and description $objAccount = $objWMIService.Get("Win32_UserAccount.Name='" & $un & "',Domain='" & $dom & "'") If IsObj($objAccount) Then $FullName = $objAccount.FullName $Description = $objAccount.Description EndIf ConsoleWrite($FullName & @CRLF) ConsoleWrite($Description & @CRLF) Return EndFunc  
      On my old PC this code will output just . and . for each of those line currently commented out. Which is fine.
      On my new PC any of those commented out lines of code cause an error, and the script won't even compile.
      $objAccount = $objWMIService.Get("Win32_UserAccount.Name='" & $un & "',Domain='" & $dom & "'") $objAccount = $objWMIService^ ERROR I'm very much a newb with the WMI stuff and objects, but it looks like the .Get property is failing when either $un or $dom aren't valid in v3.3.14, whereas in 3.3.12 the .Get would fail to return an object, which is then caught by the If statement.
      Am I on track with this? Is there some new/better way to code the example so that 3.3.14 will compile it?
    • willichan
      By willichan
      So, this is not one of my neatest scripts, but I haven't contributed much to the forum lately, and this was a help to me this week.
      I was migrating about 40 print queues from an old print server to a new one.  Since this entails removing the old printer and creating the new printer for each user on each PC for each print queue used (for about 350 users), doing this manually was out of the question.
      I just added this to the logon scripts to run once per user per machine.
      If anyone finds the need to make a similar migration, please feel free to use/modify this.
      Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ;don't want to crash out if there is a problem. #include "PrintMgr.au3" ;https://www.autoitscript.com/forum/topic/155485-printers-management-udf/ #include "ServiceControl.au3" ;https://www.autoitscript.com/forum/topic/6487-updated-service-control-funtions/ #include <array.au3> Global $sOldPrintServer = "oldservername" Global $sNewPrintServer = "newservername" Global $sDefaultPrinter = _CleanName(_GetDefaultPrinter()) ;If you change print queue names below, you may need to take that into account and modify $sDefaultPrinter as well. Global $aPrinters = _PrintMgr_EnumPrinter() If @error Then Exit Global $aReplaced[1] = [0] Global $sTempName ;remove printers that are mapped to old server For $iLoop = 1 To $aPrinters[0] If Not StringInStr($aPrinters[$iLoop], $sOldPrintServer) Then ContinueLoop ;not a printer we are concerned with $sTempName = $aPrinters[$iLoop] ConsoleWrite("Removing " & $sTempName & @CRLF) _PrintMgr_RemovePrinter($sTempName) $sTempName = _CleanName($sTempName) ;If you need to change a print queue name, modify $sTempName here before adding it to the array. _ArrayAdd($aReplaced, $sTempName) $aReplaced[0] = $aReplaced[0] + 1 Next ;Stop and re-start print spooler. This alleviated problems on a few machines. ConsoleWrite("Stopping Spooler" & @CRLF) _StopService(@ComputerName, "spooler") Sleep(1000) ConsoleWrite("Starting Spooler" & @CRLF) _StartService(@ComputerName, "spooler") Sleep(1000) ;remap to the new server (assuming all of the print queues have the same share name) For $iLoop = 1 To $aReplaced[0] Local $isdefault = False If $aReplaced[$iLoop] = $sDefaultPrinter Then $isdefault = True ConsoleWrite("Adding \\" & $sNewPrintServer & "\" & $aReplaced[$iLoop] & "(" & $isdefault & ")" & @CRLF) _PrintMgr_AddWindowsPrinterConnection("\\" & $sNewPrintServer & "\" & $aReplaced[$iLoop]) If $isdefault Then _PrintMgr_SetDefaultPrinter("\\" & $sNewPrintServer & "\" & $aReplaced[$iLoop]) Next Func _GetDefaultPrinter() ; CyberSlug - 18 Nov 2004 Local $key, $default, $defPrtNm If @OSType = "WIN32_WINDOWS" Then $key = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print\Printers" $defPrtNm = RegRead("HKEY_CURRENT_CONFIG\System\CurrentControlSet\Control\Print\Printers", "Default") Else ;WIN_NT type $key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers" $default = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device") $defPrtNm = StringLeft($default, StringInStr($default, ",") - 1) EndIf Return $defPrtNm EndFunc ;==>_GetDefaultPrinter Func _CleanName($sName) Local $sTempName = $sName $sTempName = StringReplace($sTempName, "\\" & $sOldPrintServer & "\", "") Return $sTempName EndFunc ;==>_CleanName Func MyErrFunc() ;just dump the info to the console and keep going Local $sErrNum Local $sMsg $sErrNum = Hex($oMyError.Number, 8) $sMsg = "Error Number: " & $sErrNum & @CRLF $sMsg &= "WinDescription: " & $oMyError.WinDescription & @CRLF $sMsg &= "Script Line: " & $oMyError.ScriptLine & @CRLF ConsoleWrite($sMsg) EndFunc ;==>MyErrFunc  
    • ModemJunki
      By ModemJunki
      Edit: I found out from looking into the post from coffeeturtle that not all motherboards support WMI temperature reading. You have to explore the WMI namespaces for your system!
      I (stupidly) fried a computer. It was an old Zotac small form factor device that was junked at work, so I turned it into a home theater PC.
      But I had put it in my "audio rack", which has glass doors and not very good ventilation. Normally I would open the doors while it was running and I use a media remote which would hibernate the system. At some point, it woke up by itself (probably Windows Update) and did not go to sleep again. Unfortunately this happened during the summer months while I was away on a small holiday and of course the glass doors were closed.
      When I finally noticed the system was on, it was super hot and the fan was at full blast. Dead. No POST, no green light on the mainboard even with a new power supply, old mechanical laptop hard drive gives errors when connected to another device. My next home project will be to make some appropriate ventilation in the audio cabinet.
      For the replacement I bought a used HP ultra small form factor machine and decided to start working on an app to monitor the ambient temperature sensor on the motherboard and shutdown the system if needed. I used some code for smooth label updates from here (needed updating to work with latest AutoIT) and temperature conversion from here (not really needed, only if you want temperatures in something other than C).
      Maybe I will work on this some more and make it configurable with an .INI file or even storing the settings in registry, but since I probably won't I thought I'd put it up as an example of what a non-expert can do with an afternoon of coding and ideas from the community here.
      This uses an ambient temperature sensor populated on HP machines in HP-specific WMI extensions but the WMI query can easily be changed to any available sensor including CPU or GPU.
      The watchdog monitors temperature, warns with S.O.S beeping if the set point is exceeded, and either shuts down if a timeout is reached while the temperatures are high or goes back to monitoring if the temperature goes lower then the set point, and logs events to the Windows application event logs.
      The GUI it puts up is very small at the top center of the screen (very small on a 4k display anyway).
      #requireadmin because of the WMI query.
      A timer for the WMI query because of prior experience using similar functions in WinPE to get model infos from HP WMI bios extensions.
      If you play around with this, please post your fixes/improvements/changes back to this thread.
      Have fun!
      Edit: Updated to show "Unable to query temperature" if the WMI query returns a null
      #AutoIt3Wrapper_Icon=temperature-2-multi-size.ico #RequireAdmin #NoTrayIcon #include <Temperature.au3> #include <FFLabels.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Global $winLogger = "C:\WINDOWS\system32\eventcreate.exe" ; application to use for event logging Global $MessageSource = "HP TempMon" Global $width = 185 Global $height = 15 Global $FontSize = 8 Global $FontFamily = 'Microsoft Sans Serif' Global $normClr = 0xFF000000 Global $warnClr = 0xFF0000 Global $WMInameSpace = "HP_BIOSNumericSensor" ;~ Global $WMIReadSensor = "Chassis Thermal Index" ; HP 8300 Global $WMIReadSensor = "System Ambient Temperature" ; HP Z400 Global $warnTmp = 46.1111111111 ; degrees C ;~ Global $warnTmp = 24 ; degrees C for testing Global $ovrtmpTime = 5 ; in minutes Global $hGUI = GUICreate("Board Temperature", $width, $height, -1, 0, BitOR($WS_SYSMENU, $WS_POPUP), BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE, $WS_EX_TOOLWINDOW)) Global $lb1 = _GUICtrlFFLabel_Create($hGUI, "", -1, -1, $width, $height, 8, -1, 0, 1, $normClr) GUISetState(@SW_SHOW) _TempSenseLoop() Func _TempSenseLoop() While 1 Sleep(250) $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE GUIDelete($hGUI) Return Case Else $s_ambTempC = _doQueryHPNumSens(5, $WMInameSpace, $WMIReadSensor, "100") ; + 100 Select Case $s_ambTempC = "" _GUICtrlFFLabel_SetTextColor($lb1, $normClr) _GUICtrlFFLabel_SetData($lb1, "Unable to query temperature") Case $s_ambTempC < $warnTmp _GUICtrlFFLabel_SetTextColor($lb1, $normClr) _GUICtrlFFLabel_SetData($lb1, "Board temperature: " & $s_ambTempC & "C" & "/" & Round(_CelsiusToFahrenheit($s_ambTempC), 0) & "F") Case Else _GUICtrlFFLabel_SetTextColor($lb1, $warnClr) _GUICtrlFFLabel_SetData($lb1, "Board temperature: " & $s_ambTempC & "C" & "/" & Round(_CelsiusToFahrenheit($s_ambTempC), 0) & "F") _doLog("System ambient temperature has exceeded " & $warnTmp & "C.", "WARN", $MessageSource) _AlarmMonitor($ovrtmpTime, $warnTmp) EndSelect EndSwitch WEnd EndFunc ;==>_TempSenseLoop Func _AlarmMonitor($min, $tmp) Local $Shutdown = 1 Local $s_ambTemp Local $freq = 3500 Local $shrt = 200 Local $long = 500 Local $timer = TimerInit() Local $wait = 1000 * ($min * 60) While TimerDiff($timer) < $wait $s_ambTemp = _doQueryHPNumSens(5, $WMInameSpace, $WMIReadSensor, "100") ; + 100 If $s_ambTemp >= $tmp Then _GUICtrlFFLabel_SetData($lb1, "WARNING! OVERTEMP!") Beep($freq, $shrt) Beep($freq, $shrt) Beep($freq, $shrt) Sleep(250) Beep($freq, $long) Beep($freq, $long) Beep($freq, $long) Sleep(250) Beep($freq, $shrt) Beep($freq, $shrt) Beep($freq, $shrt) _GUICtrlFFLabel_SetData($lb1, "Board temperature: " & $s_ambTemp & "C" & "/" & _CelsiusToFahrenheit($s_ambTemp) & "F") Sleep(2000) ElseIf $s_ambTemp < $tmp Then $Shutdown = 0 ExitLoop EndIf WEnd If $Shutdown = 1 Then _doLog("The system is shutting down due to overtemperature conditions. The temperature at the time of shutdown was " & $s_ambTemp & "C" & "/" & _CelsiusToFahrenheit($s_ambTemp) & "F", "ERRR", $MessageSource) Shutdown(24) ElseIf $Shutdown = 0 Then _doLog("System ambient temperature has been reduced below " & $warnTmp & "C.", "INFO", $MessageSource) EndIf EndFunc ;==>_AlarmMonitor Func _doQueryHPNumSens($min, $class, $name, $sleep) ; $sleep = milliseconds Local $s_tempReading = "" Local $s_HPBiosWMIService = "winmgmts:\\" & @ComputerName & "\Root\HP\InstrumentedBIOS" Local $objHPBiosWMIService = ObjGet($s_HPBiosWMIService) Local $s_biosQuery = $objHPBiosWMIService.ExecQuery("select * from " & $class & " where Name='" & $name & "'") $timer = TimerInit() ; we may need to wait for the HP WMI extensions to enumerate in WMI, in WinPE this takes some minutes. $wait = 1000 * ($min * 60) If Not IsObj($s_biosQuery) Then ; we do need to wait, put up a splash screen $s_WMISplash = 1 SplashTextOn("WMI", "Probing WMI (up to 10 minutes)...", 300, 50, -1, -1, 1) Sleep(1000) While TimerDiff($timer) < $wait Sleep(500) $objHPBiosWMIService = ObjGet($s_HPBiosWMIService) If IsObj($objHPBiosWMIService) Then Sleep(500) $colProdName = $s_biosQuery If IsObj($colProdName) Then For $oItem In $colProdName $s_tempReading = $oItem.CurrentReading Next EndIf EndIf WEnd ElseIf IsObj($s_biosQuery) Then ConsoleWrite($s_biosQuery & @CRLF) For $oItem In $s_biosQuery $s_tempReading = $oItem.CurrentReading Next Else $s_tempReading = 0 EndIf Sleep($sleep) Return $s_tempReading EndFunc ;==>_doQueryHPNumSens Func _doLog($message, $type, $msgsrc) Select Case $type = "INFO" $logTyp = "INFORMATION" Case $type = "WARN" $logTyp = "WARNING" Case $type = "ERRR" $logTyp = "ERROR" EndSelect If FileExists($winLogger) Then RunWait(@ComSpec & " /c " & $winLogger & " /L Application /T " & $logTyp & " /SO " & Chr(34) & $msgsrc & Chr(34) & " /ID 1000 /D " & Chr(34) & $message & Chr(34), "", @SW_HIDE) EndIf EndFunc ;==>_doLog  
       
       
      Temperature.au3
      FFLabels.au3
      HP_Temp.au3
    • ModemJunki
      By ModemJunki
      Hello,
      In Windows 10 PowerShell, one can do this to change the metric for a NIC in Windows 10:
      Get-NetAdapter | Where-Object -FilterScript {$_.InterfaceAlias -Eq "Ethernet 2"} | Set-NetIPInterface -InterfaceMetric 2 I know I can script the above PowerShell line (and it works!), but I wanted to try something I hadn't done before after looking into jguinch's most excellent Network configuration UDF. I wanted to make use of the SetIPConnectionMetric method in the WMI classes. There is an example VBscript here but this is not for Windows 10. Using AutoIT would also give better control over capturing error return codes than with PowerShell.
      But I cannot get my script to work! The return from SetIPConnectionMetric() is 0, which would indicate success. Yet the change does not happen. I also tried WMI methods using .put_ but this fails.
      Anyone more experienced than I have ideas to make this work?
      #RequireAdmin _SetNicInterfaceMetric2("Ethernet 2", "2") Func _SetNicInterfaceMetric2($NIC_NAME, $METRIC) Local $s_setIndx = 0 $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2") $colNICItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" & $NIC_NAME & "'", "WQL") If IsObj($colNICItems) Then For $objItem In $colNICItems $s_nicIndex = $objItem.Index Next ConsoleWrite("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = '" & $s_nicIndex & "'" & @CRLF) $colNAC = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = '" & $s_nicIndex & "'", "WQL") If IsObj($colNAC) Then For $objNetCard In $colNAC If $METRIC <> $objNetCard.IPConnectionMetric Then ConsoleWrite("Metric was set to " & $objNetCard.IPConnectionMetric & ". Setting to " & $METRIC & "." & @CRLF) $s_isSet = $objNetCard.SetIPConnectionMetric($METRIC) ConsoleWrite("SetIPConnectionMetric Result = " & $s_isSet & @CRLF) Else ConsoleWrite("Metric is already set to " & $METRIC & @CRLF) EndIf Next EndIf EndIf EndFunc ;==>_SetNicInterfaceMetric2  
    • AndyS19
      By AndyS19
      I have code that does a WMI SQL query to find all defined printers, and I want to parse the returned object in several places.  However, after parsing it the first time, all other times fail to find any printer objects.
      Here is my test code:
      test() Func test() Local $oPrinters, $oPrinter, $err, $cnt, $oP, $query $query = "SELECT * FROM Win32_Printer" $oPrinters = doQuery($query) $err = @error LogMsg("+++: $err = " & $err & ", isObj($oPrinters) = " & IsObj($oPrinters)) If ($err == 0) Then LogMsg("FIRST LOOP") ; <=== FIRST LOOP $cnt = 0 $oP = $oPrinters LogMsg("+++: isObj($oP) = " & IsObj($oP)) For $oPrinter In $oP $cnt += 1 LogMsg("+++: isObj($oPrinter): " & IsObj($oPrinter) & ", $oPrinter.Name ==>" & $oPrinter.Name & "<==") Next LogMsg("+++: Found " & $cnt & " printers") LogMsg("SECOND LOOP") ; <== SECOND LOOP $cnt = 0 $oP = $oPrinters LogMsg("+++: isObj($oP) = " & IsObj($oP)) For $oPrinter In $oP $cnt += 1 LogMsg("+++: isObj($oPrinter): " & IsObj($oPrinter) & ", $oPrinter.Name ==>" & $oPrinter.Name & "<==") Next LogMsg("+++: Found " & $cnt & " printers") EndIf EndFunc ;==>test Func doQuery($sQuery, $lnum = @ScriptLineNumber) #forceref $lnum LogMsg("+++:" & $lnum & ": doQuery(" & '"' & $sQuery & '"' & ") entered") Local $oWMIService, $oResults, $errstr Local $wbemFlags = BitOR(0x20, 0x10) ; $wbemFlagReturnImmediately and wbemFlagForwardOnly $oWMIService = ObjGet("winmgmts:\\" & "localhost" & "\root\CIMV2") If (IsObj($oWMIService)) Then $oResults = $oWMIService.ExecQuery($sQuery, "WQL", $wbemFlags) If (IsObj($oResults)) Then LogMsg("+++: doQuery() returns @error = 0, Good: returning the object") Return (SetError(0, 0, $oResults)) ;;; Good: return the object Else $errstr = "" _ & "WMI Query failed." & @CRLF _ & "This is the query:" & @CRLF _ & " " & $sQuery LogMsg("+++: ====>" & $errstr & "<===") LogMsg("+++: doQuery() returns @error = 1") Return (SetError(1, 0, $errstr)) ; Error: Query faled EndIf Else $errstr = "" _ & "WMI Output" & @CRLF _ & "No WMI Objects Found for class: " & @CRLF _ & "Win32_PrinterDriver" & @CRLF _ & "using this query:" & @CRLF _ & " " & $sQuery LogMsg("+++: ====>" & $errstr & "<===") MsgBox(0, "ERROR", $errstr) ; Error: Cannot get $oWMIService object Exit (1) EndIf EndFunc ;==>doQuery Func LogMsg($msg, $lnum = @ScriptLineNumber) ConsoleWrite("+++:" & $lnum & ": " & $msg & @CRLF) EndFunc ;==>LogMsg Parsing the returned $oPrinters object shows 5 printers:
      +++:15: FIRST LOOP +++:18: +++: isObj($oP) = 1 +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Microsoft XPS Document Writer<== +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Microsoft Office Document Image Writer<== +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Fax<== +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Canon MG7100 series Printer WS<== +++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Canon MG6100 series Printer WS<== +++:24: +++: Found 5 printers Parsing it again, shows no printers:
      +++:26: SECOND LOOP +++:29: +++: isObj($oP) = 1 +++:35: +++: Found 0 printers