Jump to content

WMI Scripting help


Recommended Posts

Hi,

My first post, and it's because I can't understand simple logic! :D

I am trying to convert a simple piece of WMI Code to use in AutoIT. However, I just can't seem to work out what I need to change. I've never done programming, and am still trying to get to grips with when to use the Do, For, While commands.

What I am trying to do, is to write a small application that detects when a computer has connected or disconnected from a network using any of it's interfaces so that other settings can be effected. From the Microsoft Scripting Guy Column here, there are two scripts that can used.

This is for when a disconnect is detected

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\wmi")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * from MSNdis_StatusMediaDisconnect") 

Do While True 
    Set strLatestEvent = colMonitoredEvents.NextEvent 
    Wscript.Echo "A network connection has been lost:"
    WScript.Echo strLatestEvent.InstanceName, Now
    Wscript.Echo 
Loop

This is for detecting when a connection is made

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\wmi")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * from MSNdis_StatusMediaConnect") 

Do While True 
    Set strLatestEvent = colMonitoredEvents.NextEvent 
    Wscript.Echo "A network connection has been made:"
    WScript.Echo strLatestEvent.InstanceName, Now
    Wscript.Echo 
Loop

This is my AutoIT code, which i've tried to write which doesn't work

; Generated by AutoIt Scriptomatic
$APP = "NetSentry"
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$objNicMonitorService = ObjGet("winmgmts:\\" & $strComputer & "\root\WMI")
$colItems = $objNicMonitorService.ExecQuery("SELECT * FROM MSNdis_StatusMediaDisconnect")

If IsObj($colItems) Then
For $objItem in $colItems
While 1 
    $strLatestEvent = $ColItems.NextEvent 
    $Output = "A network connection has been lost:" & @CRLF
    $Output = $Output & $strLatestEvent.InstanceName
    TrayTip($APP,"Disconnect Detected. Updating Networks", 5)
WEnd
Next
Else
EndIf

My Questions

1) What is the AutoIT equivalent of Do While True ?

2) How can I write a WQL query to poll for both events so that it can run in a loop. I've tried various WMI queries, and it just doesn't work. I don't think WMI can query two differenct 'classes' simulataneously.

3) You guys are great! I'm just not clever enough to transplant the knowledge I've tried to learn while searching the other posts.

Link to comment
Share on other sites

My Questions

1) What is the AutoIT equivalent of Do While True ?

While True...WEnd.

VBScript still uses While WEnd but M$ recommends using Do While...Loop as While can be used at top or at the bottom as Do...Loop While. The latter is the same as AutoIt's Do...Until

2) How can I write a WQL query to poll for both events so that it can run in a loop. I've tried various WMI queries, and it just doesn't work. I don't think WMI can query two differenct 'classes' simulataneously.

Never seen it done to know. :D

3) You guys are great! I'm just not clever enough to transplant the knowledge I've tried to learn while searching the other posts.

I cannot do transplants but would recommend a copy of AutoIt's version of Scriptomatic from Scripts'n'Scraps forum for WMI stuff. Also download a copy of VBScript's Helpfile at M$ (Script56.chm) for the help with understanding the loops and the rest.

:wacko:

Link to comment
Share on other sites

While True...WEnd.

VBScript still uses While WEnd but M$ recommends using Do While...Loop as While can be used at top or at the bottom as Do...Loop While. The latter is the same as AutoIt's Do...Until

Never seen it done to know. :D

I cannot do transplants but would recommend a copy of AutoIt's version of Scriptomatic from Scripts'n'Scraps forum for WMI stuff. Also download a copy of VBScript's Helpfile at M$ (Script56.chm) for the help with understanding the loops and the rest.

:wacko:

Thanks for the info. Will try to research and resolve. If anybody else has ideas on the Looping two WMI queries, would appreciate it!

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