Mit diesem Monitortyp wird die Agentversion überprüft und eine Warnung generiert, wenn die Version eines Agents niedriger als die bereitgestellte Mindestversion ist.
' CompleteVersionString
' ---------------------
'
' Appends ".0" to the end of the version
'
' Parameters
' versionString: String to append the zero
' length: number of parts that are to be appended
'
' Return
' String with appended version parts set to 0
'
Sub CompleteVersionString(ByRef versionString, ByVal length)
Dim i
For i = 0 to length - 1
versionString = versionString & ".0"
Next
End Sub
' Main
' ---------------------
'
' Main subroutine of the script
'
Sub Main
SetLocale("en-us")
InitializeCommon(3000)
' check arguments
Dim oArgs
Set oArgs = WScript.Arguments
If oArgs.Count < 2 Then
ThrowScriptError "Invalid parameters passed to " & GetScriptName & ".", Null
End If
' process arguments:
' 0 - computerName
' 1 - requiredVersion
Dim computerName, requiredVersion
computerName = oArgs(0)
requiredVersion = oArgs(1)
' create MOM Script API object
Dim oAPI, oDiscoveryData, oInstance
Set oAPI = MOMCreateObject("MOM.ScriptAPI")
' create Registry object
Dim oReg, key, value, agentVersion
' suppress error
On Error Resume Next
Set oReg = MOMCreateObject("WScript.Shell")
key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup\"
value = "AgentVersion"
agentVersion = oReg.RegRead(key & value)
If Err.Number <> 0 Then
' registry does not exist. Possible cause is a 32-bit agent installed on 64-bit OS
Err.Clear
key = "HKEY_LOCAL_MACHINE\Wow6432Node\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup\"
agentVersion = oReg.RegRead(key & value)
If Err.Number <> 0 Then
' registry does not exist. Possible cause is a Management Server
Err.Clear
key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup\"
value = "ServerVersion"
agentVersion = oReg.RegRead(key & value)
If Err.Number <> 0 Then
ThrowScriptError "An error occurred while reading the registry: '" & key & value & "'", Err
End If
End If
End If
' resume error
On Error Goto 0
' get Agent version
If IsNull(agentVersion) = False And agentVersion <> "" Then
' create property bag
Dim oPropertyBag
Set oPropertyBag = oAPI.CreatePropertyBag()
If IsObjectUnallocated(oPropertyBag) = False Then
Dim agentVersionParts, requiredVersionParts, agentVersionLength, requiredVersionLength
agentVersionParts = Split(agentVersion, ".")
requiredVersionParts = Split(requiredVersion, ".")
agentVersionLength = GetArrayLength(agentVersionParts)
requiredVersionLength = GetArrayLength(requiredVersionParts)
' set the lengths of both equal by appending .0 at the end
If requiredVersionLength < agentVersionLength Then
Call CompleteVersionString(requiredVersion, agentVersionLength - requiredVersionLength)
requiredVersionParts = Split(requiredVersion, ".")
ElseIf requiredVersionLength > agentVersionLength Then
Call CompleteVersionString(agentVersion, requiredVersionLength - agentVersionLength)
agentVersionParts = Split(agentVersion, ".")
End If
agentVersionLength = GetArrayLength(agentVersionParts)
Dim requiresUpgrade
requiresUpgrade = "false"
' compare the respective parts
Dim i, agentPart, requiredPart
For i = 0 to agentVersionLength
' suppress error
On Error Resume Next
' check Agent version part
agentPart = Int(agentVersionParts(i))
If Err.Number <> 0 Then
agentPart = 0
Err.Clear
End If
' check required version part
requiredPart = Int(requiredVersionParts(i))
If Err.Number <> 0 Then
requiredPart = 0
Err.Clear
End If
' resume error
On Error Goto 0
If agentPart < requiredPart Then
' Agent Version is lower, so requires an upgrade
requiresUpgrade = "true"
Exit For
ElseIf agentPart > requiredPart Then
' Agent Version is higher, so does not require an upgrade
Exit For
End If
Next
' exit with property bag
Call oPropertyBag.AddValue("UpgradeRequired", requiresUpgrade)
Call oPropertyBag.AddValue("CurrentVersion", agentVersion)
Call oPropertyBag.AddValue("RequiredVersion", requiredVersion)
Call oAPI.Return(oPropertyBag)
WScript.Quit
Else
ThrowScriptError "Unable to create propertybag in script.", Null
End If
Else
ThrowScriptError "Agent Version not found in Registry.", Null
End If
End Sub
'Copyright (c) Microsoft Corporation. All rights reserved.
' Library Initializer
Sub InitializeCommon(iScriptEventID)
'set the script event id
SCRIPT_EVENT_ID = iScriptEventID
End Sub
' Check the list for null or empty
Function IsListEmpty(ByVal oList)
IsListEmpty = (IsNull(oList) = True) OR (oList.Count < 1)
End Function
' Check object for null, empty or nothing
Function IsObjectUnallocated(ByVal obj)
IsObjectUnallocated = IsNull(obj) OR IsEmpty(obj) OR (TypeName(obj) = "Nothing")
End Function
' Gets the length of an array
Function GetArrayLength(ByRef array)
GetArrayLength = UBound(array) + 1
End Function
' Get the Host Computer name
Function GetHostComputerName
Dim oNetwork
Set oNetwork = MOMCreateObject("WScript.Network")
GetHostComputerName = oNetwork.ComputerName
End Function
' Get the WMI object
Function GetWMIObject(ByVal namespace)
Dim oWMI
'get the object
On Error Resume Next
Set oWMI = GetObject(namespace)
If Err.Number <> 0 Or IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & namespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", Err
End If
On Error Goto 0
Set GetWMIObject = oWMI
End Function
' Get Registry object
Function GetRegistryObject(ByVal computerName)
Set GetRegistryObject = GetWMIObject("winmgmts:\\" & computerName & "\root\default:StdRegProv")
End Function
' Creates an event and sends it back to the mom server without stopping the script.
Function ThrowScriptErrorNoAbort(ByVal message, ByVal oErr)
' Retrieve the name of this (running) script
Dim scriptFileName
scriptFileName = GetScriptName
If Not IsNull(oErr) Then
message = message & " Cause: " & oErr.Description
end if
On Error Resume Next
Dim oAPITemp
Set oAPITemp = CreateObject("MOM.ScriptAPI")
If Err.Number = 0 Then
oAPITemp.LogScriptEvent scriptFileName, SCRIPT_EVENT_ID, SCOM_ERROR, message
End If
On Error Goto 0
End Function
' Creates an error event, sends it back to the mom server, and stops the script.
Function ThrowScriptError(Byval message, ByVal oErr)
On Error Resume Next
ThrowScriptErrorNoAbort message, oErr
WScript.Quit
End Function
' Gets the script name
Function GetScriptName
Dim oFSO
Set oFSO = MOMCreateObject("Scripting.FileSystemObject")
GetScriptName = oFSO.GetFile(WScript.ScriptFullName).Name
set oFSO = nothing
End Function
' Creates an object
Function MOMCreateObject(ByVal name)
'create the object
On Error Resume Next
Set MOMCreateObject = CreateObject(name)
If Err.Number <> 0 Then
ThrowScriptError "Unable to create COM object '" & name & "'", Null
End If
On Error Goto 0
End Function
' Log the message
Sub Log(message, severity)
Dim oTempAPI, scriptName
'get script name
scriptName = GetScriptName
'create a temp mom api object
'(avoids having to pass the object every time or using a global)
Set oTempAPI = MOMCreateObject("MOM.ScriptAPI")
'log event
On Error Resume Next
Call oTempAPI.LogScriptEvent(scriptName, SCRIPT_EVENT_ID, severity, message)
On Error Goto 0
End Sub