How to monitor windows script execution

Posted on 2009-04-15
Last Modified: 2013-11-10
While windows scripting plays a large role in using WMI to monitor a system, I need to know if there is a mechanism to track when a script (itself) was executed the result (succes, failed/reason).

My initial thought was that WMI itself should have classes to track execution, but I don't see any classes that obviously do that.
Question by:NJThomson
    LVL 70

    Expert Comment

    by:Chris Dent

    Error reporting like that must be performed within the script. Especially if it's success or failure, those terms have very little meaning outside the script.

    WMI provides access to data and can help make changes, but it isn't a managed environment that will report on threads executing against it.

    LVL 9

    Accepted Solution

    You could create a logfile function. I added some code of mine that I use to creates these files.
    You send the message you want to the function and it will create a logfile if there issn't one. It then writes a the message with the time it was created.

    $logdir = "yourpathforthelogfiles"
    Function Write-Logfile([string]$message){
    	$date = Get-Date -format "yyyyMMdd"
    	$logtime = Get-Date -Format "[HH:mm:ss]"
    	$logfile = $logdir + "\" + "logfile" + $date + ".log"
    	Add-Content $logfile "$logtime $message"
    Write-Logfile "your message"

    Open in new window


    Author Comment

    While creating a log file for a script to write to is useful for a script under control of the author, the intent of the question was for tracking the execution of 3rd party scripts and applications.

    In surfing other solutions I have found that WMI (through Win32_Process) and the management classes in .NET -  can execute any script or application (given sufficient privileges) which provides an opportunity to know what was executed, when, the execution time and the result. However, this still leaves scripts and applications that run under the windows scheduler that do not seem to be monitored and only controllable by WMI if the user doesn't modify the scheduled tasks manually on the target machine.

    Author Comment

    From the suggestion above on creating a log file through a script, it would appear that with the Scripting language's ability to run WMI queries and methods, one of which is to run an executable, then the answer would appear to be to have a script that posts a time prior to invoking the executable, then posting a time on completion along with the result (succeed, fail, etc.) - essentialy wrappering the execution.
    There does not appear to be a simple way to monitor scheduled executables.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
    How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now