Jump to content

How to get count of object from wmi query


Recommended Posts

I am querying WMI for current throughput of my NIC adapters.. Basically looking to make a lite, compiled (.exe) network monitor for WinPE3x (Win7 PE).. I intend on using this monitor during an MDT or SCCM deployment to ensure traffic is being moved at an adequate speed.

Problem:

1. I can not verify that the pulled object is valid, IsObject ALWAYs returns a "True" even if the Query results are empty.

2. Can not use ".count" property listed in the Win32_Class in on ms's site.

Any Help towards this project is much appreciated !

*FYI: I also want to make a similar monitor for transfer via USB.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=C:\support\script\network-icons\icons\Network ID SH.ico
#AutoIt3Wrapper_outfile=display-net-traffic.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Local $wbemFlagReturnImmediately= 0x10
Local $wbemFlagForwardOnly   = 0x20
Local $h_wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly
$s_ComputerName = "localhost"
$o_WMIService = ObjGet("winmgmts:\\" & $s_ComputerName & "\root\CIMV2")


SplashTextOn("Network Traffic Monitor","Network Traffic Monitor",300,100,0,0,9,"arial",10,600)

For $i = 0 To 1000
$o_NetTraffic = $o_WMIService.ExecQuery("SELECT NAME, BytesReceivedPersec, BytesSentPersec FROM Win32_PerfFormattedData_Tcpip_NetworkInterface WHERE Name LIKE '%Network%'", "WQL", $h_wbemFlags)

If IsObj($o_NetTraffic) Then

        For $o_item in $o_NetTraffic
            Round(FileGetSize("C:\Windows\UNSIPPS.exe")/1048576,2)
            $message = StringStripWS ( $o_item.Name, 3 ) & @CRLF & @CRLF & _
            Round($o_item.BytesReceivedPersec/1048576,2) & " MB/sec rece" & @CRLF & _
            Round($o_item.BytesSentPersec/1048576,2) & " MB/sec sent"
            ControlSetText("Network Traffic Monitor","","Static1", $message)
            Sleep(1000)
        Next
    ;Else
    ;   ControlSetText("Network Traffic Monitor","","Static1", "No Module Found...")
    ;   Sleep(5000)
    ;   ExitLoop
Else

    ControlSetText("Network Traffic Monitor","","Static1", "Object doesn't exist? ..")
    Sleep(5000)

EndIf

Next
Link to comment
Share on other sites

I am querying WMI for current throughput of my NIC adapters.. Basically looking to make a lite, compiled (.exe) network monitor for WinPE3x (Win7 PE).. I intend on using this monitor during an MDT or SCCM deployment to ensure traffic is being moved at an adequate speed.

Problem:

1. I can not verify that the pulled object is valid, IsObject ALWAYs returns a "True" even if the Query results are empty.

2. Can not use ".count" property listed in the Win32_Class in on ms's site.

Any Help towards this project is much appreciated !

*FYI: I also want to make a similar monitor for transfer via USB.

Local $wbemFlagReturnImmediately= 0x10
Local $wbemFlagForwardOnly   = 0x20
Local $h_wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly

; ...

$o_NetTraffic = $o_WMIService.ExecQuery("SELECT NAME, BytesReceivedPersec, BytesSentPersec FROM Win32_PerfFormattedData_Tcpip_NetworkInterface WHERE Name LIKE '%Network%'", "WQL", $h_wbemFlags)
Look up those flags. They are intended to improve performance by limiting certain functionality. For example, $wbemFlagForwardOnly strips out the ability to do anything with the returned collection except step through it once with a For/Next loop. By not having to support extra properties like .count, it's memory foot print is reduced.

MSDN: Making a Semisynchronous Call with VBScript:

To reduce the WMI memory resource usage when processing a large collection of objects, include the value of wbemFlagForwardOnly in the IFlags parameter. Using wbemFlagForwardOnly causes WMI to create a forward-only enumerator that does not allow rewinding the collection and accessing items again.

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.

:(
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
Link to comment
Share on other sites

updated code:

$o_NetTraffic = $o_WMIService.ExecQuery("SELECT NAME, BytesReceivedPersec, BytesSentPersec FROM" & _
    " Win32_PerfFormattedData_Tcpip_NetworkInterface" & _
    " WHERE Name LIKE '%" & $AdapterStr & "%'", "WQL", 0)
MsgBox(0,"",$o_NetTraffic.Count ;displays amount of elements in returned object.

That Worked ! Thanks for the quick response ! .. would you happen to know what Win32 class would store the current throughput of a USB transfer ? That is my next project.

Link to comment
Share on other sites

... would you happen to know what Win32 class would store the current throughput of a USB transfer ? That is my next project.

No idea, but there are smarter people on this forum who might. Maybe they'll speak up. Good luck with it.

:(

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