Hey, Scripting Guy! How Can I Run a Script Any Time Office Outlook is Started?

Hey, Scripting Guy! How Can I Run a Script Any Time Office Outlook is Started?

  • Comments 2
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I run a script any time Outlook is started?

-- AT

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AT. Today is Wednesday, and Wednesday is a busy day here at TechEd IT Forum. For one thing, this afternoon the Scripting Guys will be making a guest appearance at the TechNet Magazine booth. What will they be doing there? Well, that’s a good question; just standing around most likely. But if you get a chance, swing by and say hello. After all, how often do you get to see a couple of Scripting Guys just standing around doing nothing.

We mean besides those of you who work at Microsoft.

Today is also the day for the IT Forum attendee party. Are the Scripting Guys planning on going to the party? Well, we’re not sure if they’ll let us, not after Scripting Guy Jean Ross started that drunken brawl during TechEd 2007 in Orlando.

Note. Did Jean really start a drunken brawl in Orlando? Oh, heavens no. After all, have you ever seen Scripting Guy Jean Ross? No one would dare pick a fight with her.

And, of course, after hanging around the TechNet Magazine booth and after going to the Attendee Party, well, then the fun really begins: we get the chance to sit down and write a script that can run a second script any time Outlook gets started on a computer. We can hardly wait.

You know what? We lied; we can’t wait at all. Here’s the script:

Set objShell = CreateObject("Wscript.Shell")

strComputer = "."

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

Set colMonitoredProcesses = objWMIService. _        
    ExecNotificationQuery("Select * from __InstanceCreationEvent " _ 
        & " Within 1 Where TargetInstance ISA 'Win32_Process' AND " & _
            "TargetInstance.Name = 'Outlook.exe'")

Do While True
    Set objProcess = colMonitoredProcesses.NextEvent
    objShell.Run "cscript.exe C:\Scripts\Test.vbs"
Loop

As you can see, this isn’t a particularly complicated script. We start out by creating an instance of the Wscript.Shell object; this is the object that enables us to run one script while inside another script. After creating the Wscript.Shell object we then connect to the WMI service on the local computer. And yes, we can use this script to monitor the use of Outlook.exe on a remote computer. If that’s what you had in mind, then simply assign the name of that remote computer to the variable strComputer:

strComputer = "atl-fs-001"

That brings us to this … interesting … line of code:

Set colMonitoredProcesses = objWMIService. _        
    ExecNotificationQuery("Select * From __InstanceCreationEvent " _ 
        & " Within 1 Where TargetInstance ISA 'Win32_Process' AND " & _
            "TargetInstance.Name = 'Outlook.exe'")

This is the spot in the script where we make our “event subscription;” in other words, this is where we tell the script what we want it to keep an eye out for. We won’t explain event subscriptions in any detail today; if you’d like more information, you might want to check out our Tales From the Script article on WMI events, or grab some popcorn and a soda and sit down and watch our Scripting Guys webcast on the same topic. What we will tell you is that this query requests notification any time a new instance of the __InstanceCreationEvent class is created; as the name implies, a new instance of this class is created any time a new instance of any WMI-compatible object (a process, a disk drive, a service, an event log event, whatever) is created.

Of course, we aren’t interested in every new WMI-compatible object that gets created. (Nor would we want to be: scores of new objects might be created on a computer every minute.) Instead, we want to be notified only when new objects meet two criteria:

The object in question is a process. That’s what the TargetInstance ISA 'Win32_Process' clause is for. (The TargetInstance, incidentally, is simply WMI’s way of referring to the new object that triggered our event monitoring script.)

The object in question has a Name equal to Outlook.exe. If a new process named Outlook.exe starts on a computer then the odds are pretty good that Outlook has just started on that computer.

After configuring our event subscription we then set up a Do While loop designed to run forever (or at least until True is no longer equal to True):

Do While True

Incidentally, this is one reason we recommend you run this script (and all monitoring scripts) in a command window under the CScript script host. That way, you can terminate the script by either pressing Ctrl+C within the command window or simply closing the command window.

Inside the loop, the first thing we do is execute this line of code:

Set objProcess = colMonitoredProcesses.NextEvent

This command tells the script to simply sit tight and wait until the next event (that is, the creation of a new process with the name Outlook.exe occurs). What if we never do start Outlook on this computer? That’s fine; the script will continue to sit patiently (and using minimal resources) until Outlook either is started or we terminate the script process.

Now, what happens if Outlook does get started? Well, in that case, we simply use the Wscript.Shell object and the Run method to run the script C:\Scripts\Test.vbs (and, again, we chose to run this script under CScript):

objShell.Run "cscript.exe C:\Scripts\Test.vbs"

After we start up Test.vbs we then loop around and wait for the next event notification.

That should do it for today. We’ll be back again tomorrow, although perhaps a little later than usual. That depends on whether we have to bail Jean out of some Spanish jail.

Again.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi, how do I change this from a loop to just running the script and closing itself?

  • thanks