Help Menu

This help page is for version 5.1. The latest available help is for version 8.1.

Execute Script Monitor

The Execute Script Monitor allows you to write your own custom scripts in the VBScript, JavaScript or PowerShell languages. You can check anything that your script can access. This monitor makes use of the applicable scripting engine that is already installed on nearly all Windows computers.

The script window is where you enter your script. The script can do anything that can be done in the selected language (including creating external ActiveX/COM components) with all the standard restrictions. A good VBScript reference is available at: http://msdn.microsoft.com/en-us/library/d1wf56tt.aspx

There are two Test buttons. One will run the script within the Console. The other will send the script to the monitoring service that is monitoring the target computer (Central Monitoring Service or a Satellite) and run the script there. This helps find any problems that might come up from the script possibly running on a different machine, or running as a different user (the service Log As user).

Keep in mind that when the script runs, it might run on a different computer than where you are editing it. That means drive mappings, HKEY_CURRENT_USER registry hive, Internet Explorer settings and the currently running user will often be different.

IMPORTANT: Do not show any user interface elements in the script -- they will not be visible in the monitoring service and will block the script from ever completing.

Additional Script Elements

Besides the scripting language's own objects and elements, the following additional global variables and methods are available within each scripting environment:

VBScript & JavaScript:
ComputerName

PowerShell:
$mon.ComputerName

This read-only string variable is the name of the computer that the monitor is attached to.

Example:
   myStr = ComputerName
   $myStr = $mon.ComputerName

VBScript & JavaScript:
Details

PowerShell:
$mon.Details

This is a string value. This value is passed to any attached actions as the details for the action (ie the content of an email notification for example).

Example:
   Details = "Alert! Can't contact remote system"
   $mon.Details = "Alert! Can't contact remote system"

VBScript & JavaScript:
FireActions
SendNotification


PowerShell:
$mon.FireActions

This is a boolean value. Either FireActions or SendNotification can be used -- they are identical. If the value is set to True, actions attached to the monitor will fire. If it is False, they will not fire. The value defaults to False.

Example:
   FireActions = true
   $mon.FireActions = $true

VBScript & JavaScript:
GetStatID
RecordStat


PowerShell:
$mon.GetStatID $mon.RecordStat

GetStatID and RecordStat are used together to record integral data values to the database for reports.

GetStatID is a function that takes a single string value and returns an integer statID. The string value should be a useful name to you, such as the name of the thing you're probing with the script. Including the server/device name in the string would be a good idea if a similar script will run on multiple computers -- it will make it easier to choose the specific data that you want when you create reports.

Example:
   statID = GetStatID("ftpSvr1-myObject")
   $statID = $mon.GetStatID("ftpSvr1-myObject")

RecordStat is a method that takes two inputs -- the statID obtained from GetStatID above, and the integer value to record to the database. The time the value is recorded also gets saved to the database for use in line charts, etc.

Example:
   RecordStat statID, objectValue
   $mon.RecordStat($statID, $objectValue)

VBScript & JavaScript:
GetValue

PowerShell:
$mon.GetValue

This method takes a text name and returns the value that was stored earlier via the StoreValue call described below. If nothing was ever stored with that name, an empty string is returned.

Example:
   prevState = GetValue "LastState"
   $prevState = $mon.GetValue("LastState")

VBScript & JavaScript:
MachineID

PowerShell:
$mon.MachineID

Returns the numeric value that uniquely identifies this computer within the application. Useful in conjunction with the External API.

VBScript & JavaScript:
SendMail

PowerShell:
$mon.SendMail

This method sends an email message to the recipient that you choose.

Example:
      SendMail "to_address@host.com", "from_address@host.com", "Subject of message", "Body of email message"

   $mon.SendMail("to_address@host.com", "from_address@host.com", "Subject of message", "Body of email message")

VBScript & JavaScript:
Sleep

PowerShell:
Start-Sleep

This VBScript method takes a single integer value, which is the number of milliseconds that the script should stop and sleep. Be careful about using this: causing too many monitors to sleep for very long means other monitors may not get run.

Example:
   Sleep 1500

The PowerShell cmdlet takes two parameters. The first parameter specifies timer in seconds (-s) or milliseconds (-m) and the second is an integer that specifies period of time.

Example:
   Start-Sleep -s 10

VBScript & JavaScript:
StoreValue

PowerShell:
$mon.StoreValue

This method takes a text name, and a text value and stores it. This named value can be retrieved later (even when the script runs next) via GetValue. Note that these values will be persisted in the configuration database and kept in memory with the monitor, so they should be kept relatively small (a few hundred characters long or less).

Example:
   StoreValue "LastState", "1|15|OK"
   $mon.StoreValue("LastState", "1|15|OK")

Standard Configuration Options

Like all monitors, this monitor has standard buttons on the right for Adding Actions, setting Advanced Options and setting the Monitor Schedule.

Example VBScripts


Check a database value

Option Explicit
Dim objconnection
Dim objrecordset
Dim strDetails

Const adOpenStatic = 3
Const adLockOptimistic = 3

SendNotification = False

Set objconnection = CreateObject("ADODB.Connection")
Set objrecordset = CreateObject("ADODB.Recordset")

objconnection.Open _
 "Provider=SQLOLEDB;Data Source=<data_base_server>;" & _
 "Initial Catalog=<database_name>;" & _
 "User ID=<username>;Password=<password>;"

objrecordset.Open "SELECT COUNT(*) FROM <database_name>", _
 objconnection, adOpenStatic, adLockOptimistic

If objrecordset.RecordCount >= 1 Then
   objrecordset.MoveFirst
   'ensure there are at least 1000 rows
   If objrecordset.Fields(0) < 1000 Then
      SendNotification = True
      strDetails = "There are only " & objrecordset.Fields(0) & " rows in the table!"
   End If
Else
   strDetails = "CODE RED !!!! No rows found!"
   SendNotification = True
End If

Details = strDetails

Check files in a directory

dim highCount
highCount = 1000
Set fso = CreateObject("Scripting.FileSystemObject")
Set oSrcFolder = fso.GetFolder("\\server\dir\tocheck")
fileCount = oSrcFolder.Files.Count

if fileCount > highCount then
   SendNotification = True
else
   SendNotification = False
end if

Check the size of a specific file and record to a database

FileToCheck = "C:\Files\Backup\dump.db"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(FileToCheck) Then
   Set objFile = objFSO.GetFile(FileToCheck)

   statID = GetStatID(FileToCheck)
   RecordStat statID, objFile.Size

   If objFile.Size < 1000 Then
      SendNotification = True
      Details = FileToCheck & " is too small!"
   Else
      SendNotification = False
   End If
Else
   SendNotification = True
   Details = FileToCheck & " does not exist!"
End If

Check if the newest file is older than 6 hours old (to ensure new files are being created)

DirToCheck = "C:\Logs"
Dim fNewest
set oFolder=createobject("scripting.filesystemobject").getfolder(DirToCheck)
For Each aFile In oFolder.Files
   If fNewest = "" Then
      Set fNewest = aFile
   Else
      If fNewest.DateCreated < aFile.DateCreated Then
         Set fNewest = aFile
      End If
   End If
Next

if fNewest.DateCreated < (DateAdd("h",-6,Now())) then
   SendNotification = True
   Details = "NEWEST LOG FILE older than 6 hours (latest file " & fNewest.DateCreated & ")"
else
   SendNotification = False
end if

Launch a program and check the result code

Dim objShell
Set objShell = CreateObject("WScript.Shell")
'Spaces in the path below can cause trouble for the Run method
exitCode = objShell.Run("C:\Test\App.exe", 1, True)
Set objShell = Nothing

if (exitCode = 0) then 'assuming 0 means OK in this case
    FireActions = false
    Details = "Everything is OK"
Else
    FireActions = true
    Details = "Test app returned " + exitCode
End If

Check for text in a file

Option Explicit
Dim oFSO, sFile, oFile, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "\\machine\share\textfile.txt"
If oFSO.FileExists(sFile) Then
    Set oFile = oFSO.OpenTextFile(sFile, 1)
    Do While Not oFile.AtEndOfStream
        sText = oFile.ReadLine
        If Trim(sText) = "ERROR" Then
            SendNotification = True
        Else
            SendNotification = False
        End If
    Loop
    oFile.Close
Else
    SendNotification = True
End If

Thanks goes out to Seth Johnson at Williams for this

Example PowerShell


Check files in a directory

$mon.FireActions = $false
$mon.Details = ""
$highCount = 1000
$folder = "C:\Temp"
$files = Get-ChildItem $folder -Force

if ($files.Count -gt $highCount)
{
   $mon.FireActions = $true
   $mon.Details = "File Count is " + $files.Count
}

Your Script


If you would like to share your script, please contact us.

PA Server Monitor

Help Map
 
Power Admin Social Network Channels
site search

Download Trial
Buy Now