Tuesday, June 29, 2010

SCOM: Agent Queue Size Script

A while ago I needed a script to adjust the SCOM Agent Queue size to make sure no auditing events were lost in case of a link failure between DC's and OpsMgr MS's.
So I created this script to do this for me.

If supports launching it from a Agent Task . The script gives a return code and quits before restarting the Health Service with a scheduled job, using 'at'.
Option Explicit

Dim blnForceRestart
Dim lngValue
Dim strComputer
Dim strManagementGroup

Const HKEY_LOCAL_MACHINE = &H80000002
blnForceRestart = False
strComputer = "."

Call Main

Sub Main()

Call GetParams()
WScript.Echo "Executing " & WScript.ScriptName
Call RegChange()

End Sub

Sub RegChange()
Dim objReg
Dim lngCurrentValue
Dim strKeyPath
Dim strValueName

Set objReg = GetObject("winmgmts:\\" & strComputer &"\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Services\HealthService\Parameters\Management Groups\" & strManagementGroup
strValueName = "MaximumQueueSizeKb"

objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, lngCurrentValue

If IsNull(lngCurrentValue) Then
WScript.Echo "An error occured while reading registry key."
WScript.Quit 201
End If

If CLng(lngCurrentValue) <> lngValue Then
objReg.SetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, lngValue
WScript.Echo strValueName & ": " & lngCurrentValue & " changed to " & lngValue
Call ScheduleRestartHealthService()
WScript.Echo "Current value '" & lngCurrentValue & "' matches parameter value: " & lngValue
If blnForceRestart Then
WScript.Echo "Restart of HealthService forced."
Call ScheduleRestartHealthService()
End If
End If
End Sub

Sub ScheduleRestartHealthService()
Dim dtmTime
Dim dtmScheduleTime
Dim objShell
Dim intMinutesDelay
Dim intReturn

Set objShell = CreateObject("Wscript.Shell")
dtmTime = Now()
If Second(dtmTime) < 50 Then
intMinutesDelay = 1
intMinutesDelay = 2
End If
dtmScheduleTime = FormatDateTime(DateAdd("n",intMinutesDelay,dtmTime),4)
WScript.Echo "Scheduling a HealthService restart for " & dtmScheduleTime
intReturn = objShell.Run("at " & dtmScheduleTime & " cmd /c " & Chr(34) &_
"net stop healthservice && net start healthservice",0,False)
If intReturn > 0 Then WScript.Quit Clng(intReturn + 500)
End Sub

Sub GetParams()
If Wscript.Arguments.Named("mgmtgrp") <> "" Then
strManagementGroup = Wscript.Arguments.Named("mgmtgrp")
WScript.Echo "Missing 'mgmtgrp' argument"
WScript.Quit 101
End If

If WScript.Arguments.Named("sizekb") <> "" Then
lngValue = CLng(WScript.Arguments.Named("sizekb"))
lngValue = 15360 'Default value
WScript.Echo "Using default Queue Size, " & lngValue & " kB."
End If

If WScript.Arguments.Named.Exists("forcerestart") Then
blnForceRestart = True
End If
End Sub

0 reacties:

Post a Comment