Help a newbie with this VBScript

Discussion in 'Scripting' started by Jonas Sackerud, Apr 1, 2004.

  1. I'm trying to write a listener that detects when a USB drive is connected.
    The script does work as intended but the wmi-queries puts HEAVY load on the
    internal drives due to the loop.

    Could someone please give me any ideas to change the script or suggest
    another solution for my problem? (Excuse my bad code below, this is my first
    VBScript).

    'This stuff works but the payload on the harddrives is, eeeh, somewhat high
    :)
    '**** Variable declarations *******
    strComputer = "."
    IomegaModelID = "IC35L090 AVV207-0 USB Device" 'This is the identyfier for
    the Iomega disk
    driveFound = false
    doListen = true

    '***** Hardware query *************
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer _
    & "\root\cimv2")
    Set colDiskDrives = objWMIService.ExecQuery _
    ("Select Model, InterfaceType from Win32_DiskDrive")

    '***** Start listening for harddrive changes *******
    Do While doListen = true

    For each objDiskDrive in colDiskDrives

    Set colDiskDrives = objWMIService.ExecQuery _
    ("Select * from Win32_DiskDrive")

    if(InStr(objDiskDrive.InterfaceType, "USB")) Then
    Wscript.Echo "Found an USB device"
    End If

    if (objDiskDrive.Model = IomegaModelID) Then
    driveFound = true
    doListen = false
    End If

    Next

    Loop

    Wscript.Echo "The Iomega drive was connected. Exiting script"
     
    Jonas Sackerud, Apr 1, 2004
    #1
    1. Advertisements

  2. Jonas Sackerud

    Stivie S. Guest

    Hi Jonas

    for such purposes, you should use WMI events instead of using loops that run all the time. You can create a (temporary) WMI event provider that waits for an event to occurr (for example, an USB drive is connected), and then fires some action that you define

    Here some sample code that creates the WMI event provider that monitors the connection of a USB drive. The script only monitors the connection of the USB drive, not the deletion. It checks for events every 5 seconds ("WHITHIN 5..."). After an event is triggered (= the USB drive was connected), it just gives out a message at the console, then quits

    strComputer = ".
    IomegaModelID = "IC35L090 AVV207-0 USB Device
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"
    Set Events = GetObject("winmgmts:\\.\root\cimv2").ExecNotificationQuery("SELECT TargetInstance.Model "
    & "FROM __InstanceCreationEvent WITHIN 5 "
    & "WHERE TargetInstance ISA 'Win32_DiskDrive' and "
    & "TargetInstance.InterfaceType='USB' and ")
    & "TargetInstance.Model='" & IomegaModelID & "'"

    Set DiskEvent = Events.NextEven
    Wscript.Echo "USB drive connected: " & DiskEvent.TargetInstance.Model
     
    Stivie S., Apr 1, 2004
    #2
    1. Advertisements

  3. I am most thankful for your help Stivie! However, I would this listener to
    run forever. I plug/unplug this USB drive once a day. How shall I change
    your example script?

    Where can I find a complete API for this type of scripting along with
    reserved words, operators, etc? I'm a Java developer so I would get along
    with a proper API instead of wasting your time with these questions.

    Thanks again - Jonas

    run all the time. You can create a (temporary) WMI event provider that waits
    for an event to occurr (for example, an USB drive is connected), and then
    fires some action that you define.
    the connection of a USB drive. The script only monitors the connection of
    the USB drive, not the deletion. It checks for events every 5 seconds
    ("WHITHIN 5..."). After an event is triggered (= the USB drive was
    connected), it just gives out a message at the console, then quits.
    GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer &
    "\root\cimv2")
    GetObject("winmgmts:\\.\root\cimv2").ExecNotificationQuery("SELECT
    TargetInstance.Model " _
     
    Jonas Sackerud, Apr 1, 2004
    #3
  4. Jonas Sackerud

    Stivie S. Guest

    Hi Jonas

    you can easily modify the script to run indefinitely - until you terminate it manually or log off. Just put a "DO...LOOP" statement around the line that gathers the event. Here's how

    strComputer = ".
    IomegaModelID = "IC35L090 AVV207-0 USB Device
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"
    Set Events = GetObject("winmgmts:\\.\root\cimv2").ExecNotificationQuery("SELECT TargetInstance.Model "
    & "FROM __InstanceCreationEvent WITHIN 5 "
    & "WHERE TargetInstance ISA 'Win32_DiskDrive' and "
    & "TargetInstance.InterfaceType='USB' and ")
    & "TargetInstance.Model='" & IomegaModelID & "'"

    D
    Set DiskEvent = Events.NextEven
    Wscript.Echo "USB drive connected: " & DiskEvent.TargetInstance.Mode
    LOO

    You can read a nice description of scripting WMI events in the Windows 2000 Scripting Guide on TechNet. Here is the link to the WMI event documentation
    http://www.microsoft.com/resources/...000/server/scriptguide/en-us/sas_wmi_kzcp.msp

    When you want to run this WMI event consumer all the time, then I would recommend that you install a permanent event consumer. The MSDN library contains nice documentation how you create and compile a permanent event consumer using the "ActiveScriptEventConsumer". In short: you create a MOF file that contains the code for your event consumer, then compile this MOF using "mofcomp.exe". By this, the event consumer will be compiled in your WMI repository so that you do not have to start the script yourself. Here's the link to the MSDN documentation
    http://msdn.microsoft.com/library/d...sdk/wmi/running_a_script_based_on_an_event.as

    Hope this helps...
     
    Stivie S., Apr 1, 2004
    #4
  5. I can't thank you enough Stivie. This saves me both time and money. Good
    work!

    it manually or log off. Just put a "DO...LOOP" statement around the line
    that gathers the event. Here's how:
    GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer &
    "\root\cimv2")
    GetObject("winmgmts:\\.\root\cimv2").ExecNotificationQuery("SELECT
    TargetInstance.Model " _
    2000 Scripting Guide on TechNet. Here is the link to the WMI event
    documentation:
    recommend that you install a permanent event consumer. The MSDN library
    contains nice documentation how you create and compile a permanent event
    consumer using the "ActiveScriptEventConsumer". In short: you create a MOF
    file that contains the code for your event consumer, then compile this MOF
    using "mofcomp.exe". By this, the event consumer will be compiled in your
    WMI repository so that you do not have to start the script yourself. Here's
    the link to the MSDN documentation:
     
    Jonas Sackerud, Apr 2, 2004
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.