Event Scripting with WMI
(Page 1 of 4 )
So you’re really starting to figure out this whole scripting thing. That’s great, but now you’re beginning to realize that you want your scripts to work more like full applications, right? Let’s learn a way to bridge the gap between scripting and full-blown application development.
We all know that you can create a script to perform a given list of procedures. That’s cool. We even know that we can schedule or loop them to make them recurring. That’s nifty too. But what you really want is to be able to write a script that reacts to something else!
How about a script that restarts your web server if it crashes? Or how about a script that maintains an order database every time you receive an email order? Now those would be useful scripts.
There’s just one problem. How can WSH know when those particular things occur? The answer is simple. It’s called Event Programming—and lucky for us, both VBScript and WMI are event-driven technologies!
In programming there are three basic types of elements: methods (including functions and subroutines), properties, and events. You’re already familiar with the first two. We use those every day.
An event typically has two parts: an event method and an event sink. We make a call to the event method to start the process off. The method wants to know if a particular event occurs so it drops an event sink in memory.
Think of the event sink like a Boolean flag. It has an initial value of False (i.e. the event hasn’t happened yet). The sink then waits in memory until the event occurs. At this point you can consider it to be True.
The event method checks back with the event sink periodically until either the event has occurred or until the request times out. It then returns a value indicating its exit code.
Okay, so it’s actually more detailed than that, but you get the idea.
Since VBScript is an event-driven language, events are actually very important to VBScript’s functionality. Unfortunately for us, very few events are directly available to us within the WSH environment. That’s different with WMI, however.
Next: Introduction to WMI >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer