How to monitor windows script execution

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.
Who is Participating?
Sander StadSysteemontwikkelaar, Database AdministratorCommented:
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

Chris DentPowerShell DeveloperCommented:

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.

NJThomsonAuthor Commented:
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.
NJThomsonAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.