$EVENT_ID_SUCCESS = 1000 #Use IDs in the range 1 - 1000
$EVENT_ID_SCRIPTERROR = 999 #Then you can use eventcreate.exe to test the MP
$WIN_SRV_2012_OSVer = "6.2"
#******************************************************************************
# FUNCTION: CheckByOSCurrentVersion
# DESCRIPTION: Returns True if the Registry Key for CurrentVersion
# is equal the target OS Versions Number.
# RETURNS: Boolean: True, if build is greater or equal than the given number
#******************************************************************************
function CheckByOSCurrentVersion()
{
$strCurrentOSVer = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
$strCurrentOSVer = $strCurrentOSVer.CurrentVersion
$CheckByOSCurrentVersion = $false
if($strCurrentOSVer -ge $WIN_SRV_2012_OSVer){
$CheckByOSCurrentVersion = $true
}else{
$CheckByOSCurrentVersion = $false
}
return $CheckByOSCurrentVersion;
}
$useCIM = CheckByOSCurrentVersion
#Scripting.FileSystemObject constants
$FOR_READING = 1 #Open a file for reading only. You can#t write to this file.
$FOR_WRITING = 2 #Open a file for writing.
$FOR_APPENDING = 8 #Open a file and write to the end of the file.
#Global vars
#$mblnLogSuccessEvent #As String(ToBoolean) (passed in as parameter of script)
#$mstrIdentifier #As String (passed in as parameter of script)
#*********************************************************************************************
# PROCEDURE: Main
# DESCRIPTION: Reads the script parameters and creates the cluster shared volume discovery data.
# PARAMETERS: void
#*********************************************************************************************
Function Main()
{
$objMomScriptAPI = New-Object -comObject MOM.ScriptAPI
#Monitor instances
#Connect to WMI NS \\.\root\MSCluster
$error.Clear()
# !!! Refactoring comment:
# Original VBScript only tries to connect to the namespace. Piping to get only the first one saves time.
if($useCIM){
$oWMI = Get-CimClass -Namespace $WMI_MSCLUSTER_NAMESPACE -ErrorAction SilentlyContinue | select -First 1
}else{
$oWMI = Get-WMIObject -Namespace $WMI_MSCLUSTER_NAMESPACE -Class $WMI_MSCLUSTER_CLUSTER_SHARED_VOLUME -ErrorAction SilentlyContinue | select -First 1
}
if ($error.Count -eq 0)
{
if($useCIM){
$objClusterSharedVolumes = Get-CimInstance -Namespace $WMI_MSCLUSTER_NAMESPACE -Query ("select * from " + $WMI_MSCLUSTER_CLUSTER_SHARED_VOLUME)
}else{
$objClusterSharedVolumes = Get-WMIObject -Namespace $WMI_MSCLUSTER_NAMESPACE -Query ("select * from " + $WMI_MSCLUSTER_CLUSTER_SHARED_VOLUME)
}
#Loop through all returned cluster shared volumes
foreach ($objClusterSharedVolume in $objClusterSharedVolumes)
{
#Get the associated disk partition
if($useCIM){
$objClusterDiskPartitions = (Get-CimAssociatedInstance -InputObject $objClusterSharedVolume -Association $WMI_CLUSTER_SHARED_VOLUME_TO_PARTITION_ASSOCIATOR_CLASS)
}else{
#****************************************************************************************************************
# FUNCTION: CreateMonitoringData
# DESCRIPTION: Create monitoring data for a cluster shared volume.
#
# PARAMETERS: IN Object objClusterSharedVolume: the cluster shared volume as SWbemObject of type MSCluster_ClusterSharedVolume.
# IN Object objClusterDiskPartition: the associated disk partition as SWbemObject of type MSCluster_DiskPartition.
# IN Object objClusterResource: the associated cluster resource as SWbemObject of type MSCluster_Resource.
# IN String strTargetComputer: the principal name of the targeted cluster virtual server hosting the cluster shared volume,
# i.e. the cluster name and not the current owner!
# IN String strClusterName: the cluster containing the cluster shared volume
# RETURNS: Boolean: True if successful
#****************************************************************************************************************
Function CreateMonitoringData($objClusterSharedVolume, $objClusterDiskPartition, $objClusterResource,
$strTargetComputer, $strClusterName, $objMomScriptAPI) #As Boolean
{
#Create performance data
$list = New-Object System.Collections.ArrayList($null)
#CSV State
$list.Add( (CreatePerformanceTypedPropertyBag $PERF_OBJECT_CSV $PERF_COUNTER_STATE_PROPERTY_NAME $objClusterResource.$WMI_NAME_PROPERTY_NAME `
$objClusterSharedVolume.$WMI_FAULTSTATE_PROPERTY_NAME $objClusterResource.$WMI_NAME_PROPERTY_NAME $objMomScriptAPI))
#***************************************************************************************
# PROCEDURE: CreatePerformanceTypedPropertyBag
# DESCRIPTION: Generates a performance typed property bag and adds it to the script#s XML output.
# PARAMETERS: IN String strObjectName: name of the performance object
# IN String strCounterName: name of the performance counter
# IN String strInstanceName: name of the performance counter in instance
# IN Variant varValue: value of the performance counter
# IN String strCSVInstanceName: Friendly volume name of the cluster shared volume
# IN Object objMomScriptAPI: initialised MOM.ScriptAPI object
#***************************************************************************************
Function CreatePerformanceTypedPropertyBag($strObjectName, $strCounterName, $strInstanceName,
$varValue, $strCSVInstanceName, $objMomScriptAPI)
{
return @{ $PERFORMANCE_OBJECT_NAME = $strObjectName;
$PERFORMANCE_COUNTER_NAME = $strCounterName;
$PERFORMANCE_INSTANCE_NAME = $strInstanceName;
$PERFORMANCE_VALUE_NAME = $varValue;
$INSTANCE_NAME = $strCSVInstanceName}
}