Home » Scripts (Page 5)

Category Archives: Scripts

Update Custom Fields in Ops Mgr 2007

I have built a script to update alert customfields in Ops Mgr 2007. I have done this for MOM 2005 too, more info here. In MOM 2005 I did it with a couple of rules. In Ops Mgr 2007 I solve it with a schedule task. My first idea was to run it as a command notification, but I ran into a permission issues. You can setup this script as a schedule task, and run it every X minute. It will load Ops Mgr command shell snap-in and then get all new alerts (ResolutionState = 0). The script wíll then get the computername from the alert and run some Active Directory queries based on that computername. The result from these queries will be input in the alert custom fields. Some alerts does not have a netbioscomputername attribute, therefor these alerts will not be updated, for example some Active Directory alerts.

You can customize the script any way you want. With this example I want to show you one way to do it, with some example attributes. Download the script here.

Custom Fields in Ops Mgr

Script That Returns Performance Data

I have made a script that will search for a word in a logfile and send the result back as performance data to Ops Mgr 2007.

With default settings will the script look in the IIS log files (C:\windows\system32\LogFiles\W3SVC1\) for the last hour and search for the word “POST”. It will then return how many times that word is in the file as performance data. 

To use this script you can create a new  Collection Rules/Probe Based/Script (Performance), paste the script into the “script box”, input suitable performance mapping information. If you change the “Value” (default $Data/Property[@Name=’PerfValue’]$) you must also change this in the script. Remeber to configure your IIS to generate a logfile for every hour, or modify the script.

I have configure my script to run every 70 minutes synchronize at 00:00. It will then run 10 minutes past every hour and tot up the hour before.

You can download the script here. Send me a e-mail if you have any feedback or suggestions.

Script: Monitor Process

This is a script that will monitor if a process is running, and if it is running, for how long it has been been running. Default settings is monitoring notepad.exe and will generate a local event if it has been running for more than 15 minutes. You can implement this script with timed commands/execute a script

Download the script here

Script: Check Account Status

This is two simple script to check if a account is enable or disable. If enable, a local event will be created. In this example I check if the Guest account is enable, if it is a local event will be generated. You can then pickup that event with a event rule.

strUser = "LDAP://cn=Guest,cn=Users,dc=contoso,dc=internal"Set objUser = GetObject _
  (strUser)
 
If objUser.AccountDisabled = FALSE Then
 Const EVENT_WARNING = 2
 Set objShell = Wscript.CreateObject("Wscript.Shell")
 objShell.LogEvent EVENT_WARNING, _
        "The account " & strUser & " is no longer disable. Please investigate."
End If

If you want to check all accounts in a OU you can use the this script. It will check a OU named Service_accounts
set objOU = GetObject _
    ("LDAP://OU=Service_Accounts,DC=contoso,DC=internal")
 
ObjOU.Filter= Array("user")
For Each objUser in ObjOU
 
 strUser = "LDAP://" + objUser.distinguishedName
 Set objUser = GetObject _
   (strUser)
 
 If objUser.AccountDisabled = FALSE Then
 Const EVENT_WARNING = 2
 Set objShell = Wscript.CreateObject("Wscript.Shell")
 objShell.LogEvent EVENT_WARNING, _
        "The account " & strUser & " is no longer disable. Please investigate."
End If
Next

Script that Returns Multiple Lines

I saw a couple of questions about a script searching for a word in a logfile and then create a event with the line including the “word” but also the two lines before that. This script search for the word “Warning” in the file logfile.txt. If found it will generate a local event with the line includning Warning but also the two lines before that.

Const ForReading = 1
Const EVENT_WARNING = 2Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "Warning"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("logfile.txt", ForReading)

strLineNo = 1

Do Until objFile.AtEndOfStream

     strSearchString = objFile.ReadLine
     Set colMatches = objRegEx.Execute(strSearchString)

    strCacheC = strCacheB
  strCacheB = strCacheA
  strCacheA = strSearchString

   
 If colMatches.Count > 0 Then
        For Each strMatch in colMatches  
          
 Set objShell = CreateObject("Wscript.Shell")
 objShell.LogEvent EVENT_WARNING, _
 "Warning from logfile: " & vbCr &_
 "BEGINNING OF LINES" & vbCr &_
 "" & vbCr &_
  strCacheC & vbCr &_
         strCacheB & vbCr &_
         strCacheA & vbCr &_
 "" & vbCr &_
         "END OF LINES."
        Next

 End If   

strLineNo = strLineNo + 1

Loop
objFile.Close

Check last modified for a directory

I have done a simple script to check last modified on all files in a directory. This example will generate a local event if files are older then 2 months.

strComputer = "."
Const EVENT_WARNING = 2
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile In colFileList
    Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objFile = objFSO.GetFile(objFile.Name)
 
 Set objShell = CreateObject("Wscript.Shell")
 If DateDiff("m", objFile.DateLastModified, Now) > 2 Then
 objShell.LogEvent EVENT_WARNING, _
 "objFile is older than 2 months. Please investigate. The file is: " & objFile
 end If
Next

For more settings, please see this post.

For more settings, please see post.

Verify that a File Exists

This is a simple script that will check if a file exists, if not it will generate a warning event in the local event viewer. You can pick up that event with a rule from MOM. In this example, the script verify if C:\pagefile.sys exists.

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

strFile = “C:\pagefile.sys”

If objFSO.FileExists(strFile) Then
    Set objFolder = objFSO.GetFile(strFile)
Else
    Const EVENT_WARNING = 2
    Set objShell = CreateObject(“Wscript.Shell”)
    objShell.LogEvent 2, strFile & ” is not accessible.”
End If

Check regkey and service startup mode

Last week me and some friends found a strange mismatch with a server that has remote desktop enable and the terminal service application computer group. I have done a script that will check a regkey (not the one in this example) and a service startup type. If the startup type is not auto, the script will change it and generate a information event in the local event viewer. It is a little bit strange script, but I am sure someone more that us can use it 🙂

The event that is generated in the local event viewer has event id 0 and the type is information

Unfortunately my WordPress theme dont really like long lines in scripts, so I have uploaded it as a file instead. You can download it here

Check two or more performance counters

I have done a script that will check two performance counters and generate an alert if both are over threshold values. It simple to add more performance counters and change the threshold values. I have test it on Windows Server 2003 and Window XP 32bit. Its fairly basic, but it will show you a way to generate an alert based on two or more performance counters.

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
set objRefresher = CreateObject(“WbemScripting.SWbemRefresher”)
Set colItems = objRefresher.AddEnum _
(objWMIService, “Win32_PerfFormattedData_PerfOS_System”).objectSet
objRefresher.Refresh
For i = 1 to 2
For Each objItem in colItems

strProc = objItem.Processes
strCPUQL = objItem.ProcessorQueueLength

If strProc > 40 AND strCPUQL > 0 Then
Const EVENT_WARNING = 2
Set objShell = CreateObject(“Wscript.Shell”)
objShell.LogEvent 2, “Number of processer: ” & strProc & “. Processor Queue Lenght: ” & strCPUQL
End If
objRefresher.Refresh
Next
Next

1. Create a new script and paste the script source
2. Create a new event rule to run the script every X minute as respond
3. Create a new event rule to collect event ID 2 and event type of Warning
4. Commit configuration change

Monitor if a share is accessible

This script will check if a file in a share is accessible, if not, it will generate a local event.

strFile = "\\ntlcs\share\file_check.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFile) Then

Else
   Const EVENT_WARNING = 2
    Set objShell = CreateObject("Wscript.Shell")
    objShell.LogEvent 2, strFile & " is not accessible."
End If

  1. Create a new script and paste the script
  2. Create a new event rule to run the script every X minute as respond
  3. Create a new event rule to collect event ID 2 and event type of Warning
  4. Commit configuration change