Home » Scripts (Page 6)
Category Archives: Scripts
Monitor file and directory size
This script will monitor file and directory size. It will generate performance data back to the management server. You can view this information under performance view in Operators Console or/and in reports. This first example will monitor the size C:\pagefile.sys
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\pagefile.sys")
CreatePerfData "File","File Size",objFile.Path,objFile.SizeSub CreatePerfData(strObjectName,strCounterName,strInstanceName,numValue)
   Set objPerfData = ScriptContext.CreatePerfData
   objPerfData.ObjectName = strObjectName
   objPerfData.CounterName =strCounterName
   objPerfData.InstanceName = strInstanceName
   objPerfData.Value = numValue
   ScriptContext.Submit objPerfData
End Sub
This example will monitor the size of all subfolders under C:\Â
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\")
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
CreatePerfData "File","File Size",objSubfolder.Path,objSubfolder.Size
Next
Sub CreatePerfData(strObjectName,strCounterName,strInstanceName,numValue)
                  Set objPerfData = ScriptContext.CreatePerfData
                  objPerfData.ObjectName = strObjectName
                  objPerfData.CounterName =strCounterName
                  objPerfData.InstanceName = strInstanceName
                  objPerfData.Value = numValue
                  ScriptContext.Submit objPerfData
End Sub
- Insert the script as a new script in Administrator Console
- Create a event rule that run the script every X minute
- Commit configuration change and wait until the script has run
- Open Operator Console, click performance, click you server and choose your new FILE performance objects, click draw graph
There is a guide about how to create performance data reports at momresources.org
Monitor recovery mode
This script will check recovery mode of a MS SQL database. Its pretty simple to modify to check other databases or all databases. This example will check if the Onepoint database is running with SIMPLE recovery mode. If it does, a local event will be generated. You can then create a event rule to collect that event and generate an alert in MOM.
set cn = Nothing
set rs = nothingstrSQLServer = "localhost"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=" & strSQLServer & ""
strSQLQuery = "select name, databasepropertyex(name, 'recovery') as RECOVERY from sysdatabases WHERE NAME = 'OnePoint'"
Set rs = cn.execute(strSQLQuery)
strDBName = rs("Name")
strMode = rs("Recovery")
If strMode = "SIMPLE" then
 Const EVENT_WARNING = 2
 Set objShell = CreateObject("Wscript.Shell")
 objShell.LogEvent 2, "The " & strDBName & " database is running in simple recovery mode"
End If
- Create a new script and paste this source
- Create a rule that run the script every X minute
- Create a rule that collects and generate alerts on event ID 2 from WSH in the application log.
Run tasks with extended permissions
Tasks is programs that you have access to directly from Operators Console. It is basic commands like ping but also more advanced commands for Active Directory troubleshooting. After you import more management packs you will get more tasks. You can also create your own tasks in Administrator console, management packs and tasks.
 When you run a task from Operators console it runs only the current user permissions. Often the operator have very restricted permissions. But there is always a more trusted group of operators with a little bit more permissions, they use to have a “admin” account too, to do some basic administered tasks.  Unfortunately you cannot use the non-admin account to run operators console and the “little-admin” account to run tasks, at least not with basic MOM functions.
If your operators have one regular account named Kalle and one named kalle-admin you can change the command line for tasks to this
cmd /C "runas.exe /noprofile /user:%username%-admin@domain.com "mmc compmgmt.msc -s /computer:"$TargetComputer$""
Then the problem will be run as a user named kalle-admin (if you are login as kalle) and you will then be prompt to input password.
But if you need to change both login and password it is a little more complicated. You then will need a script that first ask for username and password, and then it starts the software with those credentials. This script will do that for you
strUserName = InputBox(“Please input your administration username?”, “UserName”)
strComputer = Wscript.Arguments(0)
dim OShell
Set objShell = Wscript.CreateObject(“WScript.Shell”)
objshell.run “runas.exe /noprofile /user:” & strUserName & ” ” & Chr(34) & “mmc compmgmt.msc /s /computer:” &strComputer & ” ” & Chr(34)
Â
To run this you will have to change the command line to scriptname.vbs $Computer Name$
Restart a service if a process is missing
This is a script that will restart a service if a process is missing. I this example the DFS service will be restarted if not notepad.exe is running. Maybe not a very likely scenario :)Â
strProcess = "notepad.exe" 'Name of the process
strServiceName = "DFS" 'Name of the service strComputer = "."Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objShell = CreateObject("Wscript.Shell")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcess & "'")
If colProcesses.Count < 1 Then
Const EVENT_WARNING = 2
Set objShell = CreateObject("Wscript.Shell")
objShell.LogEvent 2, strProcess & " is not running. The following process will be restarted: " & strServiceName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = '" & strServiceName & "'")
For Each objService in colListOfServices
If objService.State = "Running" Then
objService.StopService
strNow = now
Do Until DateDiff("s", strNow, now) > 10
Loop
End IF
objService.StartService
Next
End If
Discovery and installation of agenter with ManualMC.txt
MOM 2005 use the file ManualMC.txt as a alternative to add computers that will be monitored. MOM will install agent on all computers that are in the file, but it can be boring mission to keep that file updated all the time. I have done a script that will quest Active Directory for computers in a OU. The result will then be written to ManualMC.txt. You can also add some machines in the script, so all computers don’t need to be in the same OU. Â Setup this script as a schedule task to run sometime every day.
Next time MOM run discovery a agent will be installed on all machines, or they will be placed under pending actions. That depends on your global settings. If you don’t want a machine to have a agent anymore, you can delete the name from ManualMC.txt and MOM will uninstall the agent at next discovery.
Machines in ManualMC.txt will be excluded if there is a exclude rule in the discovery settings.
You will find ManualMC.txt here %SYSTEM ROOT%\Program Files\Microsoft Operations Manager 2005\
This will be a default function in SCOM 2007. Before your start working with the script you will need to change
-
LDAP path
-
Path to the ManualMC.txt file
-
Computer name for computers that always will be included. If you want to add more than two you can add more Computer(X) lines, and also add the first Dom Computer (2) number.
'===========================================
'Machines that always will be included
'===========================================
Dim Computers(2)
Computers(0) = "APP03.DOMAIN.LOCAL"
Computers(1) = "APP02.DOMAIN.LOCAL"
Computers(2) = "APP01.DOMAIN.LOCAL"
'===========================================
'Add machines from list above
'===========================================
Dim i
Dim OutStr
Const ForAppending = 8
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile _
("C:\manualmc.txt ", ForWriting)
For i = 0 to UBound(Computers)
objFile.WriteLine Computers(i)
Next
'===========================================
'Query Active Directory for computer objects
'===========================================
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Fabrikam,DC=europe,DC=fabrikam,DC=net' " _
& "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
'===========================================
'Add machines from Active Directory result
'===========================================
Do Until objRecordSet.EOF
objFile.WriteLine objRecordSet.Fields("Name").Value
objRecordSet.MoveNext
Loop
'===========================================
'Close file
'===========================================
objFile.Close
Uppdatera custom fields
When you start understand how MOM 2005 works you will most likely implement new functions and rebuild some default functions. A good way to do this is by script. MOM support both VBscript, Jscript and Perl script. In this guide I will show you what you can do with alert custom fields . The included script will take information from an alert, ask questions against AD and update the custom fields.
You can download the guide here
Ping script
This script ping different machines. If a machine dont answer a local event will ge generated. If you set strLog to 1 a event will be generated for every successful ping. You will need to create a parameter named Hosts that contains all hosts you want to ping.
'
' If strLog = "1" then a event will be generated for
' success ping.
'
' This script use one parameter called "Hosts".
' Value for this can be like this
' ping.sunet.se,10.1.1.0
'
' Version 2.0
aMAchines = Split(ScriptContext.Parameters.Get("Hosts"), ",")
strLog = "0"For Each machine in aMachines
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& machine & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then Const EVENT_WARNING = 2
Set objShell = CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_WARNING, _
"Computer " & machine & " is not reachable (" & strLog & ")"
ELSE
IF strLog = 1 Then
Const EVENT_SUCCESS = 0
Set objShell = CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, _
"Computer " & machine & " is reachable"
End If
End If
Next
Next
Â
Â
Check if there are any *.err files
This script checks if there is any .err files in the directory. If there is any, a event is generated in the local event viewer.
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.getfolder("C:\")
For Each File in Folder.Files
If fso.GetExtensionName(File)="err" Then Const EVENT_WARNING = 2
Set objShell = CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_WARNING, _
"There are error files in " & folder
End If
Next
Count number of instances
This script checks if notepad.exe is running more that 4 times. If it is running more than 4 times a event is generated. You can then collect those event and get an alert in operators console.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objShell = CreateObject("Wscript.Shell")Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'notepad.exe'")
If colProcesses.Count < 4 Then
Const EVENT_WARNING = 2
Set objShell = CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_WARNING, _
"There are less then 4 instances of notepad.exe running"
End If
Tool for documentation of MOM 2005
Sydi-mom.vbs is a vbscript that you can use to create a simple document that describes your MOM environment. The script needs word. You can run it from a XP workstation against your MOM server.
Forward Alerts by E-mail
Discovery Sources in Service Manager 2010
Recent Comments