Sign in to follow this  
Followers 0
PsaltyDS

Can't get .Count property?

3 posts in this topic

I'm pretty sure I've done this before and it worked, but I'm not seeing why it doesn't work now. I'm just listing services and trying to get the count from the collection. If I comment out the .Count reference, it works fine, but the .Count causes it to crash. The .Count is even listed as a property in MSDN for this type (ISWbemObjectSet), so what am I missing?

Local Const $wbemFlagReturnImmediately = 0x10
Local Const $wbemFlagForwardOnly = 0x20
Local $wbemFlags = BitOR($wbemFlagReturnImmediately, $wbemFlagForwardOnly)

Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
Local $sQuery = 'SELECT * FROM Win32_Service'
Local $colItems = $objWMIService.ExecQuery($sQuery, "WQL", $wbemFlags)
Local $sMsg = ""
If IsObj($colItems) Then
    $sMsg &= "Service count = " & $colItems.Count & @LF
    For $objItem In $colItems
        $sMsg &= "Name: " & $objItem.Name & @LF
        $sMsg &= "Started: " & $objItem.Started & @LF
        $sMsg &= "State: " & $objItem.State & @LF & @LF
    Next
Else
    $sMsg = "$colItems is not an object"
EndIf
MsgBox(64, "Results", $sMsg)

Above code yields:

>Running:(3.2.12.1):C:\Program Files\AutoIt3\autoit3.exe "C:\temp\Test.au3" 
C:\temp\Test.au3 (10) : ==> The requested action with this object has failed.:
$sMsg &= "Service count = " & $colItems.Count & @LF
$sMsg &= "Service count = " & $colItems.Count ^ ERROR

muttley


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites



I'm pretty sure I've done this before and it worked, but I'm not seeing why it doesn't work now. I'm just listing services and trying to get the count from the collection. If I comment out the .Count reference, it works fine, but the .Count causes it to crash. The .Count is even listed as a property in MSDN for this type (ISWbemObjectSet), so what am I missing?

Local Const $wbemFlagReturnImmediately = 0x10
Local Const $wbemFlagForwardOnly = 0x20
Local $wbemFlags = BitOR($wbemFlagReturnImmediately, $wbemFlagForwardOnly)

Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
Local $sQuery = 'SELECT * FROM Win32_Service'
Local $colItems = $objWMIService.ExecQuery($sQuery, "WQL", $wbemFlags)
Local $sMsg = ""
If IsObj($colItems) Then
    $sMsg &= "Service count = " & $colItems.Count & @LF
    For $objItem In $colItems
        $sMsg &= "Name: " & $objItem.Name & @LF
        $sMsg &= "Started: " & $objItem.Started & @LF
        $sMsg &= "State: " & $objItem.State & @LF & @LF
    Next
Else
    $sMsg = "$colItems is not an object"
EndIf
MsgBox(64, "Results", $sMsg)

Above code yields:

>Running:(3.2.12.1):C:\Program Files\AutoIt3\autoit3.exe "C:\temp\Test.au3" 
C:\temp\Test.au3 (10) : ==> The requested action with this object has failed.:
$sMsg &= "Service count = " & $colItems.Count & @LF
$sMsg &= "Service count = " & $colItems.Count ^ ERROR

muttley

It's your $wbemFlagForwardOnly flag, remove that and works fine.

WMI eliminates the memory for each object as the For Each statement processes an object. You cannot call the Count method for a collection when the wbemFlagForwardOnly flag was set on the call that obtained the collection.

source: http://msdn.microsoft.com/en-us/library/aa392301(VS.85).aspx

Share this post


Link to post
Share on other sites

It's your $wbemFlagForwardOnly flag, remove that and works fine.

source: http://msdn.microsoft.com/en-us/library/aa392301(VS.85).aspx

Eek! That day: "One day I'll have to look up those flags and see what the heck they do." ...has arrived.

I just plain didn't know that.

Thanks!

muttley


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0