Sign in to follow this  
Followers 0
pcmerc

Converting this vbs code to autoit

13 posts in this topic

Could I please get some help converting this code to autoit? I don't have enough experience with COM object programming to do it myself. I've been hacking at it but I'm sure I'm going about it all wrong.

This is what I need to convert:

On Error Resume Next

Const wbemFlagReturnImmediately = &h10

Const wbemFlagForwardOnly = &h20

arrComputers = Array("localhost")

int BytesSent = 0

For Each strComputer In arrComputers

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", _

wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems

BytesSent = BytesSent + objItem.BytesSentPersec

Next

Next

WScript.Echo BytesSent

This is what I've come up with so far:

Const $wbemFlagReturnImmediately = "&h10"

Const $wbemFlagForwardOnly = "&h20"

$arrComputers = _ArrayCreate("localhost")

$BytesSent = 0

;For Each strComputer In arrComputers

$objWMIService = ObjGet("winmgmts:\\" & "strComputer" & "\root\CIMV2")

$colItems = ObjGet($objWMIService&.ExecQuery "SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL"); _

; $wbemFlagReturnImmediately + $wbemFlagForwardOnly))

; For Each objItem In colItems

$BytesSent = $BytesSent + ObjGet("objItem.BytesSentPersec")

; Next

;Next

; WScript.Echo BytesSent

MsgBox(0, "Test", $BytesSent)

Any insight would extremely helpful as well as I'm trying to learn, Thanks!

Share this post


Link to post
Share on other sites



Global Const $wbemFlagReturnImmediately = 0x10
Global Const $wbemFlagForwardOnly = 0x20

$arrComputers = _ArrayCreate("localhost")
$BytesSent = 0

For $strComputer In $arrComputers
    $objWMIService = ObjGet("winmgmts:\\" & "strComputer" & "\root\CIMV2")
    $colItems = ObjGet($objWMIService&.ExecQuery "SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly))
        If @error Then
            MsgBox(16, "_FunctionName", "ExecQuery Error: SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface")
            Return
        EndIf
    For $objItem In $colItems
        $BytesSent = $BytesSent + ObjGet("objItem.BytesSentPersec")
    Next
Next

MsgBox(0, "Test", $BytesSent)

Didnt test it, but that should do it for ya.


Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt Wiki

Share this post


Link to post
Share on other sites

Thankyou. I'll test it & reply. I'm also going to study it so I can better understand the difference between autoit's com functionality as opposed to straight vbs.

Thanks again.

Global Const $wbemFlagReturnImmediately = 0x10
Global Const $wbemFlagForwardOnly = 0x20

$arrComputers = _ArrayCreate("localhost")
$BytesSent = 0

For $strComputer In $arrComputers
    $objWMIService = ObjGet("winmgmts:\\" & "strComputer" & "\root\CIMV2")
    $colItems = ObjGet($objWMIService&.ExecQuery "SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly))
        If @error Then
            MsgBox(16, "_FunctionName", "ExecQuery Error: SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface")
            Return
        EndIf
    For $objItem In $colItems
        $BytesSent = $BytesSent + ObjGet("objItem.BytesSentPersec")
    Next
Next

MsgBox(0, "Test", $BytesSent)

Didnt test it, but that should do it for ya.

Share this post


Link to post
Share on other sites

Ran with following errors.

C:\Test\test.au3(39,112) : ERROR: syntax error

$colItems = ObjGet($objWMIService&.ExecQuery "SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~^

C:\Test\test.au3(39,170) : ERROR: ObjGet() [built-in] called with wrong number of args.

$colItems = ObjGet($objWMIService&.ExecQuery "SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Test\test.au3(39,171) : ERROR: syntax error

$colItems = ObjGet($objWMIService&.ExecQuery "SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly))

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Test\test.au3(42,19) : ERROR: 'Return' not allowed from global scope.

Return

~~~~~~~~~~~~~~~~~~^

C:\Test\test.au3 - 4 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Post the _ArrayCreate function for me if you would.

Edit: Corrected some of your code... wasnt really paying attention earlier, sorry. :lmao: There's no need to use ObjGet again with $colItems. As soon as I get that function, I'll post the script.

Edit2: Found the function. This code returns 0 on my machine.

Global Const $wbemFlagReturnImmediately = 0x10
Global Const $wbemFlagForwardOnly = 0x20

$arrComputers = _ArrayCreate("localhost")
$BytesSent = 0

For $strComputer In $arrComputers
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
        If @error Then
            MsgBox(16, "_FunctionName", "ExecQuery Error: SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface")
        EndIf
    For $objItem In $colItems
        $BytesSent = $BytesSent + ObjGet("objItem.BytesSentPersec")
    Next
Next

MsgBox(0, "Test", $BytesSent)
Edited by RagnaroktA

Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt Wiki

Share this post


Link to post
Share on other sites

#include <Array.au3>

_ArrayCreate ( $v_Val1, [$v_Val2, [..., $v_Val21]] )

Share this post


Link to post
Share on other sites

Returns 0 when your downloading a file?

Share this post


Link to post
Share on other sites

Post the _ArrayCreate function for me if you would.

Edit: Corrected some of your code... wasnt really paying attention earlier, sorry. :lmao: There's no need to use ObjGet again with $colItems. As soon as I get that function, I'll post the script.

Edit2: Found the function. This code returns 0 on my machine.

Global Const $wbemFlagReturnImmediately = 0x10
Global Const $wbemFlagForwardOnly = 0x20

$arrComputers = _ArrayCreate("localhost")
$BytesSent = 0

For $strComputer In $arrComputers
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
        If @error Then
            MsgBox(16, "_FunctionName", "ExecQuery Error: SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface")
        EndIf
    For $objItem In $colItems
        $BytesSent = $BytesSent + ObjGet("objItem.BytesSentPersec")
    Next
Next

MsgBox(0, "Test", $BytesSent)

Using the changed code I get the following error now:

C:\Test\test.au3 (39) : ==> Variable must be of type "Object".:

$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

$colItems = $objWMIService^ ERROR

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Using the changed code I get the following error now:

C:\Test\test.au3 (39) : ==> Variable must be of type "Object".:

$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

$colItems = $objWMIService^ ERROR

It returned 0, but I wasnt downloading anything at all. Any time you get the "Variable must be of type: Object" error, it means that a variable is not being passed correctly. I would guess it's not getting a solid computer name from $strComputer. Try hardcoding it and see what it returns.

Is there a specific reason you're creating an array of computers from localhost? Why not just use @ComputerName?

Edited by RagnaroktA

Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt Wiki

Share this post


Link to post
Share on other sites

changed the following line

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

Error went away. I to am getting 0. When the script is run normally it returns the aggregated bytes sent from all interfaces. I guess it's possible that the code here

For $objItem In $colItems

$BytesSent = $BytesSent + ObjGet("objItem.BytesSentPersec")

would be responsible for returning the information we're looking for.

Where is the $ObjItem defined? Maybe we need to define that prior.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I cleaned it up a little bit more, try this and tell me what you find.

Global Const $wbemFlagReturnImmediately = 0x10
Global Const $wbemFlagForwardOnly = 0x20

$BytesSent = 0

$objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If @error Then
        MsgBox(16, "_FunctionName", "ExecQuery Error: SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface")
    EndIf
For $objItem In $colItems
    $BytesSent = $objItem.BytesSentPersec * 8 / 1000 
Next

MsgBox(0, "Test", $BytesSent)
Edited by RagnaroktA

Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt Wiki

Share this post


Link to post
Share on other sites

Ok, I'm getting data now. Thanks alot. Now I need to fine tune it to display it in bits per second. This should do it.

$BytesSent = $objItem.BytesSentPersec * 8 / 1000

Share this post


Link to post
Share on other sites

Ok, I'm getting data now. Thanks alot. Now I need to fine tune it to display it in bits per second. This should do it.

$BytesSent = $objItem.BytesSentPersec * 8 / 1000

Anytime, sorry for the mixups, it's been a long, tiring day.


Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt 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  
Followers 0