'*************************************************************************
' $ScriptName: "Microsoft.Windows.Server.Common"$
'
' Purpose: To have one place for common stuff across various BaseOS VBScripts
'
' $File: Microsoft.Windows.Server.Common.vbs$
'*************************************************************************
'---------------------------------------------------------------------------
' Returns WMI Instance requested. Tries to execute WMI query a N times.
'---------------------------------------------------------------------------
Function WMIGetInstanceExTryN(oWMI, ByVal sInstance, ByVal N)
Dim oInstance, nInstanceCount
Dim e, i
Set e = New Error
For i = 0 To i < N
On Error Resume Next
Set oInstance = oWMI.InstancesOf(sInstance)
e.Save
On Error Goto 0
If IsEmpty(oInstance) Or e.Number <> 0 Then
If i = N - 1 Then
ThrowScriptError "The class name '" & sInstance & "' returned no instances. Please check to see if this is a valid WMI class name.", e
End If
Else
On Error Resume Next
nInstanceCount = oInstance.Count
e.Save
On Error Goto 0
If e.Number <> 0 Then
If i = N - 1 Then
ThrowScriptError "The class name '" & sInstance & "' did not return any valid instances. Please check to see if this is a valid WMI class name.", e
End If
Else
Exit For
End If
End If
WScript.Sleep(1000)
Next
Set WMIGetInstanceExTryN = oInstance
End Function
'---------------------------------------------------------------------------
' Returns WMI Instance requested.
'---------------------------------------------------------------------------
Function WMIGetInstanceEx(oWMI, ByVal sInstance)
Dim oInstance, nInstanceCount
Dim e
Set e = New Error
On Error Resume Next
Set oInstance = oWMI.InstancesOf(sInstance)
e.Save
On Error Goto 0
If IsEmpty(oInstance) Or e.Number <> 0 Then
ThrowScriptError "The class name '" & sInstance & "' returned no instances. Please check to see if this is a valid WMI class name.", e
End If
'Determine if we queried a valid WMI class - Count will return 0 or empty
On Error Resume Next
nInstanceCount = oInstance.Count
e.Save
On Error Goto 0
If e.Number <> 0 Then
ThrowScriptError "The class name '" & sInstance & "' did not return any valid instances. Please check to see if this is a valid WMI class name.", e
End If
Set WMIGetInstanceEx = oInstance
End Function
'---------------------------------------------------------------------------
' Connect to WMI.
'---------------------------------------------------------------------------
Function WMIConnect(ByVal sNamespace)
Dim oWMI
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
End If
Set WMIConnect = oWMI
End Function
'---------------------------------------------------------------------------
' Returns WMI Instance requested.
'---------------------------------------------------------------------------
Function WMIGetInstance(ByVal sNamespace, ByVal sInstance)
Dim oWMI, oInstance
Set oWMI = WMIConnect(sNamespace)
Set oInstance = WMIGetInstanceEx(oWMI, sInstance)
Set WMIGetInstance = oInstance
End Function
'---------------------------------------------------------------------------
' Returns WMI Instance requested.
'---------------------------------------------------------------------------
Function WMIGetInstanceNoAbort(ByVal sNamespace, ByVal sInstance)
Dim oWMI, oInstance, nInstanceCount
On Error Resume Next
Set oWMI = GetObject(sNamespace)
If Not IsEmpty(oWMI) Then
Set oInstance = oWMI.InstancesOf(sInstance)
If Not IsEmpty(oInstance) And Err.Number = 0 Then
'Determine if we queried a valid WMI class - Count will return 0 or empty
nInstanceCount = oInstance.Count
If Err.Number = 0 Then
Set WMIGetInstanceNoAbort = oInstance
On Error Goto 0
Exit Function
End If
End If
End If
On Error Goto 0
Set WMIGetInstanceNoAbort = Nothing
End Function
'---------------------------------------------------------------------------
' Executes the WMI query and returns the result set.
'---------------------------------------------------------------------------
Function WMIExecQuery(ByVal sNamespace, ByVal sQuery)
Dim oWMI, oQuery, nInstanceCount
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
End If
On Error Resume Next
Set oQuery = oWMI.ExecQuery(sQuery)
e.Save
On Error Goto 0
If IsEmpty(oQuery) Or e.Number <> 0 Then
ThrowScriptError "The Query '" & sQuery & "' returned an invalid result set. Please check to see if this is a valid WMI Query.", e
End If
'Determine if we queried a valid WMI class - Count will return 0 or empty
On Error Resume Next
nInstanceCount = oQuery.Count
e.Save
On Error Goto 0
If e.Number <> 0 Then
ThrowScriptError "The Query '" & sQuery & "' did not return any valid instances. Please check to see if this is a valid WMI Query.", e
End If
Set WMIExecQuery = oQuery
End Function
'---------------------------------------------------------------------------
' Executes the WMI query and returns the result set, no abort version.
'---------------------------------------------------------------------------
Function WMIExecQueryNoAbort(ByVal sNamespace, ByVal sQuery)
Dim oWMI, oQuery
Set oWMI = GetObject(sNamespace)
Set oQuery = oWMI.ExecQuery(sQuery)
Set WMIExecQueryNoAbort = oQuery
End Function
'---------------------------------------------------------------------------
' Retrieves WMI property.
'---------------------------------------------------------------------------
Function GetWMIProperty(oWmi, sPropName, nCIMType, ErrAction)
Dim sValue, oWmiProp, oError
Set oError = New Error
' Check that object is valid.
If Not IsValidObject(oWmi) Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "Accessing property on invalid WMI object.", oError
If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()
GetWMIProperty = ""
Exit Function
End If
' Get properties...
On Error Resume Next
Set oWmiProp = oWmi.Properties_.Item(sPropName)
oError.Save
If oError.Number <> 0 Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" & sPropName & "'.", oError
If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()
End If
On Error Goto 0
If IsValidObject(oWmiProp) Then
sValue = oWmiProp.Value
If IsNull(sValue) Then ' If value is null, return blank to avoid any issues
GetWMIProperty = ""
Else
Select Case (oWmiProp.CIMType)
Case wbemCimtypeString, wbemCimtypeSint16, wbemCimtypeSint32, wbemCimtypeReal32, wbemCimtypeReal64, wbemCimtypeSint8, wbemCimtypeUint8, wbemCimtypeUint16, wbemCimtypeUint32, wbemCimtypeSint64, wbemCimtypeUint64:
If Not oWmiProp.IsArray Then
GetWMIProperty = Trim(CStr(sValue))
Else
GetWMIProperty = Join(sValue, ", ")
End If
Case wbemCimtypeBoolean:
If sValue = 1 Or UCase(sValue) = "TRUE" Then
GetWMIProperty = "True"
Else
GetWMIProperty = "False"
End If
Case wbemCimtypeDatetime:
Dim sTmpStrDate
' First attempt to convert the whole wmi date string
sTmpStrDate = Mid(sValue, 5, 2) & "/" & _
Mid(sValue, 7, 2) & "/" & _
Left(sValue, 4) & " " & _
Mid (sValue, 9, 2) & ":" & _
Mid(sValue, 11, 2) & ":" & _
Mid(sValue, 13, 2)
If IsDate(sTmpStrDate) Then
GetWMIProperty = CDate(sTmpStrDate)
Else
' Second, attempt just to convert the YYYYMMDD
sTmpStrDate = Mid(sValue, 5, 2) & "/" & _
Mid(sValue, 7, 2) & "/" & _
Left(sValue, 4)
If IsDate(sTmpStrDate) Then
GetWMIProperty = CDate(sTmpStrDate)
Else
' Nothing works - return passed in string
GetWMIProperty = sValue
End If
End If
Case Else:
GetWMIProperty = ""
End Select
End If
Else
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" & sPropName & "'.", oError
If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()
GetWMIProperty = ""
End If
If (ErrAction And ErrAction_Trace) = ErrAction_Trace Then _
WScript.Echo " + " & sPropName & " :: '" & GetWMIProperty & "'"
Err.Clear
End Function
'---------------------------------------------------------------------------
' Class for error handling.
'---------------------------------------------------------------------------
Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile
Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
End Class
'---------------------------------------------------------------------------
' Creates an event and sends it back to the mom server.
'---------------------------------------------------------------------------
Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
' Retrieve the name of this (running) script
Dim FSO, ScriptFileName
Set FSO = CreateObject("Scripting.FileSystemObject")
ScriptFileName = FSO.GetFile(WScript.ScriptFullName).Name
Set FSO = Nothing
If Not IsNull(oErr) Then _
sMessage = sMessage & ". " & oErr.Description
On Error Resume Next
Dim oAPITemp
Set oAPITemp = CreateObject("MOM.ScriptAPI")
oAPITemp.LogScriptEvent ScriptFileName, g_ErrorEventNumber, lsEventError, sMessage
On Error Goto 0
WScript.Echo sMessage
End Function
'---------------------------------------------------------------------------
' Creates an event and sends it back to the mom server.
'---------------------------------------------------------------------------
Function ThrowScriptError(Byval sMessage, ByVal oErr)
On Error Resume Next
ThrowScriptErrorNoAbort sMessage, oErr
Quit()
End Function
'---------------------------------------------------------------------------
' Creates automation objects and returns it.
'---------------------------------------------------------------------------
Function MomCreateObject(ByVal sProgramId)
Dim oError
Set oError = New Error
On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
oError.Save
On Error Goto 0
If oError.Number <> 0 Then
ThrowScriptError "Unable to create automation object '" & sProgramId & "'", oError
End If
End Function
'---------------------------------------------------------------------------
' Quits the script.
'---------------------------------------------------------------------------
Function Quit()
WScript.Quit()
End Function
'---------------------------------------------------------------------------
' Checks whether oObject is valid.
'---------------------------------------------------------------------------
Function IsValidObject(ByVal oObject)
IsValidObject = False
If IsObject(oObject) Then
If Not oObject Is Nothing Then
IsValidObject = True
End If
End If
End Function
'---------------------------------------------------------------------------
' Outputs arguments for debugging purposes
'---------------------------------------------------------------------------
Function TraceLogArguments
Dim oArgs
Set oArgs = WScript.Arguments
Dim i, sArgs
For i = 0 To oArgs.Count - 1
sArgs = sArgs & " {" & oArgs(i) & "}"
Next
TraceLogMessage "Arguments:" & sArgs
End Function
'---------------------------------------------------------------------------
' Verifies that number of arguments is correct
'---------------------------------------------------------------------------
Function VerifyNumberOfArguments(ByVal NumberOfArguments)
Dim oArgs
Set oArgs = WScript.Arguments
If oArgs.Count <> NumberOfArguments Then
Dim i, sArgs
For i = 0 To oArgs.Count - 1
sArgs = sArgs & " {" & oArgs(i) & "}"
Next
ThrowScriptError "Invalid number of arguments (" & oArgs.Count & " instead of " & NumberOfArguments & "). Arguments:" & sArgs, Null
End If
End Function
'---------------------------------------------------------------------------
' Outputs to file and echo for debugging purposes
'---------------------------------------------------------------------------
Function TraceLogMessage(ByVal sMessage)
WScript.Echo sMessage
If g_DebugFlag = True Then
' Retrieve the name of this (running) script
Dim FSO, ScriptFileName
Set FSO = CreateObject("Scripting.FileSystemObject")
ScriptFileName = FSO.GetFile(WScript.ScriptFullName).Name
Set FSO = Nothing
On Error Resume Next
Dim oAPITemp
Set oAPITemp = MOMCreateObject("MOM.ScriptAPI")
oAPITemp.LogScriptEvent ScriptFileName, g_TraceEventNumber, lsEventInformation, sMessage
On Error Goto 0
End If
End Function
'---------------------------------------------------------------------------
' Verifies the expression. If equals to False then generates an error and quits the script
' Usage:
' Verify Not WMISet Is Nothing, "WMISet is invalid!"
' Verify WMISet.Count = 1, "Invalid quantity of services with name 'Server' (qty = " & WMISet.Count & ")."
'---------------------------------------------------------------------------
Function Verify(ByVal bBool, ByVal sMessage)
If bBool = False Then
ThrowScriptError sMessage, Null
End If
End Function
Function GetRegistryKeyValue(ByVal keyPath, ByVal key)
Dim oReg, strKeyValue
On Error Resume Next
Err.Clear
Set oReg = MOMCreateObject("WScript.Shell")
strKeyValue = oReg.RegRead(keyPath & key)
If Err.Number <> 0 Then
strKeyValue = ""
End If
Err.Clear
' resume error
On Error Goto 0
GetRegistryKeyValue = strKeyValue
End Function
'---------------------------------------------------------------------------
' Function: ExpressedInMB
' Usage:
' Parameter (SizeInBytes)
' Returns the Size Expressed in MBytes
'---------------------------------------------------------------------------
Function ExpressedInMB(byref SizeInBytes)
Dim NumberSizeExpInMB
NumberSizeExpInMB = Round(SizeInBytes / DISKSIZE_BYTES_IN_MB, 0)
ExpressedInMB = NumberSizeExpInMB
End Function
'****************************************************************************************************************
' FUNCTION: GetClusterNameFromRegistry
' DESCRIPTION: Returns the cluster name of the given cluster virtual name
'****************************************************************************************************************
Function GetClusterNameFromRegistry
Function CheckCluster(sTargetComputer)
Dim oWMI, sInstance
Dim oInstances, nCount
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = WMIConnect("winmgmts:\\" + sTargetComputer + "\root\cimv2")
e.Save
On Error Goto 0
If e.Number <> 0 Then
CheckCluster = False
Else
CheckCluster = True
End If
sInstance = "Select ID, Name from Win32_ServerFeature where Name = 'Failover Clustering'"
On Error Resume Next
Err.Clear
Set oInstances = oWMI.ExecQuery(sInstance)
nCount = oInstances.Count
e.Save
On Error Goto 0
If nCount = 0 Or e.Number <> 0 Then
CheckCluster = False
Else
If Len(GetClusterNameFromRegistry()) > 0 Then
CheckCluster = True
Else
CheckCluster = False
End If
End If
End Function
Function GetClusterDiskCollection(sTargetComputer)
Dim oWMI, sInstance, oInstances, nCount
Dim e
Set e = New Error
On Error Resume Next
Err.Clear
Set oWMI = WMIConnect("winmgmts:\\" + sTargetComputer + "\root\MSCluster")
e.Save
On Error Goto 0
If e.Number <> 0 Then
Set GetClusterDiskCollection = Nothing
Exit Function
End If
sInstance = "Select Path, SerialNumber,VolumeGuid From MSCluster_DiskPartition"
On Error Resume Next
Set oInstances = oWMI.ExecQuery(sInstance)
e.Save
On Error Goto 0
If e.Number <> 0 Then
Set GetClusterDiskCollection = Nothing
Exit Function
End If
On Error Resume Next
nCount = oInstances.Count
e.Save
On Error Goto 0
If e.Number <> 0 Then
Set GetClusterDiskCollection = Nothing
Else
Set GetClusterDiskCollection = oInstances
End If
End Function
Public Function RemoveFirstZeroChar(sHex)
Dim sTempString
Dim iLen,iPos,i
iLen = 0
iPos = 0
iLen = Len(sHex)
For i = 1 To iLen
If (Mid(sHex,i,1) <> "0") Then
iPos = i - 1
Exit For
End If
Next
If ( 0 = iPos) Then
sTempString = sHex
Else
sTempString = Right(sHex,iLen - iPos)
End if
RemoveFirstZeroChar = sTempString
End Function
'Copyright (c) Microsoft Corporation. All rights reserved.
' Parameters that should be passed to this script
' 0 MPElement ID ($MPElement$)
' 1 Target Id for ME this rule is running against ($Target/Id$)
' 2 Computer (FQDN) that the Mount Point will be hosted on
' 3 Computer ID (Key) that the Mount Point will be hosted on
' 4 MP Element to be discovered (version-specific type)
Call Main()
Sub Main()
VerifyNumberOfArguments(5)
Dim oArgs
Set oArgs = WScript.Arguments
Dim SourceID, ManagedEntityId, TargetComputer, TargetComputerID, MPElementID, IsVirtualNode, IsCluster, objWMIColl
SourceID = oArgs(0)
ManagedEntityId = oArgs(1)
TargetComputer = oArgs(2)
TargetComputerID = oArgs(3)
MPElementID = oArgs(4)
Dim oAPI, oDiscoveryData
Set oAPI = MOMCreateObject("MOM.ScriptAPI")
Set objWMIColl = Nothing
IsCluster = CheckCluster(TargetComputer)
If IsCluster = True Then
Set objWMIColl = GetClusterDiskCollection(TargetComputer)
End If
On Error Resume Next
Err.Clear
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceID, ManagedEntityId)
If (Err.Number <> 0 ) Then
Quit()
End If
If DoDiscovery(TargetComputer, TargetComputerID, oDiscoveryData, MPElementID, IsCluster, objWMIColl) >= 0 Then
Call oAPI.Return(oDiscoveryData)
Else
Quit()
End If
On Error Goto 0
End Sub
Function DoDiscovery(ByVal sTargetComputer, ByVal sTargetComputerID, ByVal oDisc, ByVal sMPElementID, ByVal IsCluster, ByRef objWMIColl)
Dim WMISet, owObj, oInstance, sDeviceID, IsClusterDisk, sSerialNumber
DoDiscovery = -1
On Error Resume Next
Err.Clear
Set WMISet = WMIGetInstance("winmgmts:\\" + sTargetComputer + "\root\cimv2", "Win32_LogicalDisk WHERE (DriveType=3 or DriveType=6) and FileSystem!=null")
For Each owObj In WMISet
sDeviceID = GetWMIProperty(owObj, "DeviceID", wbemCimtypeUseDefault, ErrAction_Abort)
sSerialNumber = GetWMIProperty(owObj, "VolumeSerialNumber", wbemCimtypeUseDefault, ErrAction_Abort)
IsClusterDisk = CheckIsClusterDisk(sDeviceID, sSerialNumber, IsCluster, objWMIColl)
If (Err.number <> 0 ) Then
Exit Function
End If
If (Err.Number = 0 ) Then
Call oDisc.AddInstance(oInstance)
End If
End If
Next
If (Err.Number = 0 ) Then
DoDiscovery = 0
End If
On Error Goto 0
End Function
Function SwitchBoolean(bIn)
If LCase(bIn) = "false" Then
SwitchBoolean = "true"
Else
SwitchBoolean = "false"
End If
End Function
Function CheckIsClusterDisk(sDeviceID, sSerialNumber, IsCluster, objWMIColl)
Dim objItem, bFlag,sSerial
Dim sHexSerial
Dim sPath,strDeviceId
CheckIsClusterDisk = False
If (False = IsCluster) Then
Exit Function
End If
bFlag = False
sHexSerial = ""
sPath = ""
If ( IsValidObject(objWMIColl) ) Then
sSerial = LCase(RemoveFirstZeroChar(sSerialNumber))
strDeviceId = LCase(sDeviceID)
For Each objItem In objWMIColl
sHexSerial = Lcase(Hex(objItem.SerialNumber))
sPath = LCase(objItem.Path )
If ( sHexSerial = sSerial AND sPath = strDeviceId) Then
bFlag = True
Exit For
End If
Next