Windows Scripting
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsSystem AdministrationWindows Scripting

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old May 27th, 2008, 09:21 PM
javaman javaman is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 13 javaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 56 sec
Reputation Power: 0
WMI - Windows events

I've read about the Windows (or any operating) system; about how Windows is the central program collecting all events and distributing it to the appropriate applications.
Since I only know J/VB-Script, is there anyway for me to access these events. I'm trying to have my script wake up whenever a new file is created or when a file is deleted.

Reply With Quote
  #2  
Old May 28th, 2008, 12:28 AM
Nilpo's Avatar
Nilpo Nilpo is online now
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
There are no WSH (VBScript) events for this directly, but you can monitor file creation and deletion using WMI events.

Monitoring file creation using __InstanceCreationEvent:
vb Code:
Original - vb Code
  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "root\cimv2")
  3.  
  4. Set colEvents = objWMIService.ExecNotificationQuery _
  5.     ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
  6.         & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
  7.         & "TargetInstance.GroupComponent= " _
  8.         & "'Win32_Directory.Name=""c:\test""'")
  9.  
  10. Do While True
  11.     Set objEvent = colEvents.NextEvent()
  12.     WScript.Echo "A new file was just created:", _
  13.         objEvent.TargetInstance.PartComponent
  14.     Exit Do
  15. Loop

Monitoring file deletion using __InstanceDeletionEvent:
vb Code:
Original - vb Code
  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "root\cimv2")
  3.  
  4. Set colEvents = objWMIService.ExecNotificationQuery _
  5.     ("SELECT * FROM __InstanceDeletionEvent WITHIN 10 WHERE " _
  6.         & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
  7.         & "TargetInstance.GroupComponent= " _
  8.         & "'Win32_Directory.Name=""c:\test""'")
  9.  
  10. Do While True
  11.     Set objEvent = colEvents.NextEvent()
  12.     WScript.Echo "A file was just deleted:", _
  13.         objEvent.TargetInstance.PartComponent
  14.     Exit Do
  15. Loop
__________________
Click the image if at any point you don't like my decision.

Scripting problems? Windows questions? Ask the Windows Guru!


Last edited by Nilpo : June 14th, 2008 at 03:10 AM. Reason: Edited to correct code syntax.

Reply With Quote
  #3  
Old June 6th, 2008, 02:11 AM
javaman javaman is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 13 javaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 56 sec
Reputation Power: 0
Thank You! Just to get this straight, does the fact that the NextEvent() function never returned make this different than continuous looping, which eats up processor space?

Also, where can I get a full listing of the available events? I also want to know when the computer goes into or out of standby mode, so that my script will stop.

Reply With Quote
  #4  
Old June 6th, 2008, 10:36 AM
Nilpo's Avatar
Nilpo Nilpo is online now
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
The NextEvent method simply waits for a NotificationQuery to return an event notification. It will sit idle while it waits.

The nice thing about WSH is that when a script sits idle it does not eat up any CPU cycles.

As for a complete list of events, you should consult the MSDN library or download a WMI explorer.

I'll see if I can find a way to monitor system state changes for you.

Reply With Quote
  #5  
Old June 6th, 2008, 11:04 AM
javaman javaman is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 13 javaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 56 sec
Reputation Power: 0
Thank you very much!
One more thing though. Now that you bring up the waiting for a function to finish, is there a way to run a function and move on without waiting for a return? Using "void func()", doesn't do the job, although it has no reason to wait.
I was trying to have my script running until the computer goes into standby. It's no use if all it does is wait for the standby. My last resort would be to run a separate page with wShell.run and not have it wait.

Reply With Quote
  #6  
Old June 9th, 2008, 03:51 PM
Nilpo's Avatar
Nilpo Nilpo is online now
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
Quote:
Originally Posted by javaman
Thank you very much!
One more thing though. Now that you bring up the waiting for a function to finish, is there a way to run a function and move on without waiting for a return? Using "void func()", doesn't do the job, although it has no reason to wait.
I was trying to have my script running until the computer goes into standby. It's no use if all it does is wait for the standby. My last resort would be to run a separate page with wShell.run and not have it wait.
Both examples above stop after the first instance. I've used the Exit Do statement to end the loop that watches for notification queries.

Reply With Quote
  #7  
Old June 12th, 2008, 04:00 AM
javaman javaman is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 13 javaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 56 sec
Reputation Power: 0
Why do you need to loop in the first place, if the script is anyhow waiting for the function to return?
Anyhow, this doesn't help me since I am trying to run the script until the computer goes into standby. I need the script to go on doing other things while listening for that event. Is it possible at all for two functions to run at the same time?

Reply With Quote
  #8  
Old June 12th, 2008, 10:33 AM
Nilpo's Avatar
Nilpo Nilpo is online now
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
Quote:
Originally Posted by javaman
Why do you need to loop in the first place, if the script is anyhow waiting for the function to return?
You don't need to loop. I did that so that other readers could see the construct they would need to use to monitor every event.
Quote:
Originally Posted by javaman
Anyhow, this doesn't help me since I am trying to run the script until the computer goes into standby. I need the script to go on doing other things while listening for that event. Is it possible at all for two functions to run at the same time?
There are two types of events: synchronous and asynchronous. So, yes, you can sometimes have a script that does two things at once. Let me play with this a bit and see if I can get it working using standard VBS event routines.

Reply With Quote
  #9  
Old June 14th, 2008, 03:07 AM
Nilpo's Avatar
Nilpo Nilpo is online now
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
In VBScript, events are handled asynchronously. This means that a script will continue to execute in the background while it waits for an event to occur. You can effectively have a script perform two operations at the same time: one set of instructions are carried out upon execution, and a second set of instructions are performed in response to any events.

This can also be accomplished when using WMI events. The examples that I've provided above use traditional WMI events. These events are carried out synchronously (or semisynchronously depending on the event) meaning that the script will site idle while waiting for events and cannot carry out any other instructions.

To overcome this, we can use the same notification query that we've used above, but instead, use a method that carries out the query asynchronously. Let's look at how the code changes.
vb Code:
Original - vb Code
  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "root\cimv2")
The code begins in the same manner by connecting the the WMI Service object.
vb Code:
Original - vb Code
  1. Set objSink = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")
Next, we need to create a sink object. This is the object that will be returned by the asynchronous event monitor. It is provided by the WbemScripting object. (WMI's scripting interface.) You'll notice a slight variation in this declaration. Notice that I'm using the WScript object's CreateObject method rather than VBScript's. This allows me to specify an additional parameter called prefix. You'll see how we use this shortly.
vb Code:
Original - vb Code