Cluster Disk Monitoring Script Data Source

Microsoft.Windows.Server.ClusterDisksMonitoring.DS.Moduletype.NanoServer (DataSourceModuleType)

This module collects performace counters for cluster disks on Nano Server Cluster.

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.Performance.Data

Member Modules:

ID Module Type TypeId RunAs 
Scheduler DataSource System.Scheduler Default
DS ProbeAction Microsoft.Windows.PowerShellPropertyBagProbe Default
InstanceFilter ConditionDetection System.ExpressionFilter Default
PerfMapper ConditionDetection System.Performance.DataGenericMapper Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IntervalSecondsint$Config/IntervalSeconds$Interval Seconds
LogSuccessEventbool$Config/LogSuccessEvent$Log Successful Event
TimeoutSecondsint$Config/TimeoutSeconds$Timeout Seconds

Source Code:

<DataSourceModuleType ID="Microsoft.Windows.Server.ClusterDisksMonitoring.DS.Moduletype.NanoServer" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IntervalSeconds" type="xsd:integer" minOccurs="1"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SyncTime" type="xsd:string" minOccurs="1"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="LogSuccessEvent" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ScriptGroupId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TargetComputer" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ClusterName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ClusterResourceName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CounterName" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="LogSuccessEvent" Selector="$Config/LogSuccessEvent$" ParameterType="bool"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="Scheduler" TypeID="System!System.Scheduler">
<Scheduler>
<SimpleReccuringSchedule>
<Interval>$Config/IntervalSeconds$</Interval>
<SyncTime>$Config/SyncTime$</SyncTime>
</SimpleReccuringSchedule>
<ExcludeDates/>
</Scheduler>
</DataSource>
<ProbeAction ID="DS" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagProbe">
<ScriptName>Microsoft.Windows.Server.MonitorClusterDisks.ps1</ScriptName>
<ScriptBody><Script>

param ($mblnLogSuccessEvent, $mstrIdentifier, $strTargetComputer, $strClusterName)

$SCRIPT_VERSION = "1.08"
$SCRIPT_NAME = "Microsoft.Windows.Server.MonitorClusterDisks.ps1"
$LOG_SUCCESS_EVENT_PARAMETER_NAME = "LogSuccessEvent"
$MP_ELEMENT_PARAMETER_NAME = "MP Element"
$MANAGED_ENTITY_ID_PARAMETER_NAME = "Managed Entity"
$TARGET_COMPUTER_PARAMETER_NAME = "Target Computer"
$CLUSTER_NAME_PARAMETER_NAME = "Cluster Name"

#WMI constants
$WMI_MSCLUSTER_NAMESPACE = "root\MSCluster"
$WMI_CIMV2_NAMESPACE = "root\cimv2"
$WMI_MSCLUSTER_CLUSTER_RESOURCE = "MSCluster_Resource"
$WMI_CLUSTER_RESOURCE_TO_DISK_ASSOCIATOR_CLASS = "MSCluster_ResourceToDisk"
$WMI_CLUSTER_DISKPARTITION_TO_DISK_ASSOCIATOR_CLASS = "MSCluster_DiskToDiskPartition"

$WMI_MSCLUSTER_CLUSTER_CLASS = "MSCluster_Cluster"
$WMI_NAME_PROPERTY_NAME = "Name"
$WMI_PATH_PROPERTY_NAME = "Path"
$WMI_FILESYSTEM_PROPERTY_NAME = "FileSystem"
$WMI_TOTALSIZE_PROPERTY_NAME = "TotalSize"
$WMI_FREESPACE_PROPERTY_NAME = "FreeSpace"
$WMI_VOLUMELABEL_PROPERTY_NAME = "VolumeLabel"

#State property bag constants
$STATE_DATA_TYPE = 3
$STATE_PROPERTY_NAME = "State"
$STATE_NTFS_NAME = "NTFSIsDirty"
$STATE_GOOD = "GOOD"
$STATE_BAD = "BAD"

#Performance property bag constants
$INSTANCE_NAME = "ClusterDiskName"
$PERFORMANCE_DATA_TYPE = 2
$PERFORMANCE_OBJECT_NAME = "PerfObject"
$PERFORMANCE_COUNTER_NAME = "PerfCounter"
$PERFORMANCE_INSTANCE_NAME = "PerfInstance"
$PERFORMANCE_VALUE_NAME = "PerfValue"
$PERF_OBJECT_CSV = "Cluster Disk"
$PERF_COUNTER_TOTAL_SIZE_MB = "Total size / MB"
$PERF_COUNTER_FREE_SPACE_MB = "Free space / MB"
$PERF_COUNTER_FREE_SPACE_PERCENT = "Free space / Percent"

#Event constants
$EVENT_TYPE_SUCCESS = 0
$EVENT_TYPE_ERROR = 1
$EVENT_TYPE_WARNING = 2
$EVENT_TYPE_INFORMATION = 4

$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

#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 disk monitoring data.
# PARAMETERS: void
#*********************************************************************************************
Function Main()
{
$ErrorActionPreference = 'SilentlyContinue'
$error.Clear()

# Targeted at Microsoft.Windows.Cluster.VirtualServer (based on Microsoft.Windows.Server.Computer)
$objMomScriptAPI = New-Object -ComObject Mom.ScriptAPI -ErrorAction SilentlyContinue

if ($null -eq $objMomScriptAPI -or $error.Count -ne 0)
{
Throw-ScriptError -Message "Cannot Initialize MOM Script API"

}

$useCIM = CheckCmdLets
#Monitor instances
$list = MonitorClusterDisks -useCIM $useCIM

if($list.Count -ne $null -and $list.Count -ne 0)
{
Set-PerfDataFromList -list $list -objMomScriptAPI $objMomScriptAPI
$mblnLogSuccessEvent = Convert-ToBoolean -sBool $mblnLogSuccessEvent
if ($mblnLogSuccessEvent -eq $true)
{
$Message = "-- Cluster Disks Monitoring Script -- Script executed successfully."
LogScriptEvent -LogType 0 -EventId $EVENT_ID_SUCCESS -EventLevel $EVENT_TYPE_INFORMATION -objMomScriptAPI $objMomScriptAPI -Message $Message

}

}
else
{

$Message = "-- Cluster Disks Monitoring Script -- An error occurred while running the script."
LogScriptEvent -LogType 0 -EventId $EVENT_ID_SCRIPTERROR -EventLevel $EVENT_TYPE_WARNING -objMomScriptAPI $objMomScriptAPI -Message $Message
Create-EmptyData -objMomScriptAPI $objMomScriptAPI

}

}

Function Create-EmptyData ($objMomScriptAPI)
{
if ($null -eq $objMomScriptAPI)
{
return

}

$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
#Create an Empty typed property bag of the performance data type
$objTypedPropertyBag = $objMomScriptAPI.CreateTypedPropertyBag($PERFORMANCE_DATA_TYPE)

$objTypedPropertyBag.AddValue($PERFORMANCE_OBJECT_NAME, "EMPTY")
$objTypedPropertyBag.AddValue($PERFORMANCE_COUNTER_NAME, "EMPTY")
$objTypedPropertyBag.AddValue($PERFORMANCE_INSTANCE_NAME, "EMPTY")
$objTypedPropertyBag.AddValue($PERFORMANCE_VALUE_NAME, "EMPTY")

#Add the property bag to the script#s XML output
$objTypedPropertyBag

$error.Clear()
}

Function Set-PerfDataFromList($list, $objMomScriptAPI)
{
if ($null -eq $objMomScriptAPI)
{
return
}

$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
foreach($data in $list)
{
#Add the values for the perf counter#s object name, counter name, instance name and the value)
if($data.$PERFORMANCE_OBJECT_NAME -eq $null)
{
continue
}

$objTypedPropertyBag = $objMomScriptAPI.CreateTypedPropertyBag($PERFORMANCE_DATA_TYPE)

$objTypedPropertyBag.AddValue($PERFORMANCE_OBJECT_NAME, $data.$PERFORMANCE_OBJECT_NAME)
$objTypedPropertyBag.AddValue($PERFORMANCE_COUNTER_NAME, $data.$PERFORMANCE_COUNTER_NAME)
$objTypedPropertyBag.AddValue($PERFORMANCE_INSTANCE_NAME, $data.$PERFORMANCE_INSTANCE_NAME)
if(($data.$PERFORMANCE_VALUE_NAME -eq $null) -or ($data.$PERFORMANCE_VALUE_NAME -eq "")){
$objTypedPropertyBag.AddValue($PERFORMANCE_VALUE_NAME, 0)
}else{
$objTypedPropertyBag.AddValue($PERFORMANCE_VALUE_NAME, $data.$PERFORMANCE_VALUE_NAME)
}
#Necessary information for the property bag data source filter condition
#to enable Agent cook down
$objTypedPropertyBag.AddValue($INSTANCE_NAME, $data.$INSTANCE_NAME)
$objTypedPropertyBag

}

}

Function Get-WmiQueryFilter
{
$strOSVersion = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").CurrentVersion

#Adding OS Validation to use propert WMI query to get correct instances
#ResourceClass and IsClusterSharedVolume properties are not available in Windows 2008 OS, only 2008 R2 and beyond.

$strWMIQuery = [string]::Empty
if ($strOSVersion -ne "6.0")
{
$strWMIQuery = " ResourceClass = 1 and IsClusterSharedVolume = FALSE"
}

return $strWMIQuery
}

Function Get-ClusterDiskPerfDataWmi($objClusterResources,$list,$useCim)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()

if ($null -eq $list)
{
$error.Clear()
return $null
}

foreach ($objClusterResource in $objClusterResources)
{
$objClusterDisks = WMIGetAssociatorInstanceNoAbort -sAssociatorName $WMI_CLUSTER_RESOURCE_TO_DISK_ASSOCIATOR_CLASS -sWmiAssociatorName "MSCluster_Disk" -oWmiInstance $objClusterResource -useCim $useCim

foreach ($objClusterDisk in $objClusterDisks)
{
#Get the associated disk partition
$objClusterDiskPartitions = WMIGetAssociatorInstanceNoAbort -sAssociatorName $WMI_CLUSTER_DISKPARTITION_TO_DISK_ASSOCIATOR_CLASS -sWmiAssociatorName "MSCluster_DiskPartition" -oWmiInstance $objClusterDisk -useCim $useCim
foreach ($objClusterDiskPartition in $objClusterDiskPartitions)
{
CreateMonitoringData -objClusterDisk $objClusterDisk -objClusterDiskPartition $objClusterDiskPartition -objClusterResource $objClusterResource -list $list

}
}
}

$error.Clear()

}

#****************************************************************************************************************
# FUNCTION: MonitorClusterDisks
# DESCRIPTION: Creates monitoring data for all instances of the class #Microsoft.Windows.Server.ClusterDisksMonitoring.ClusterDisk# using WMI.
# PARAMETERS: IN String strTargetComputer: principal name of the targeted #Microsoft.Windows.Cluster.VirtualServer# instance.
# IN String strClusterName: the cluster containing the cluster shared volume
# RETURNS: ArrayList of Performance Data
#****************************************************************************************************************
Function MonitorClusterDisks ($useCIM)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
$list = New-Object System.Collections.ArrayList($null)
if ($error.Count -ne 0)
{
$error.Clear()
return $null
}

$Filter = Get-WmiQueryFilter
$objClusterResources = WMIGetInstanceNoAbort -sNamespace $WMI_MSCLUSTER_NAMESPACE -sClassName $WMI_MSCLUSTER_CLUSTER_RESOURCE -sFilter $Filter -useCim $useCim
if ($error.Count -ne 0)
{
$error.Clear()
return $null
}

Get-ClusterDiskPerfDataWmi -objClusterResources $objClusterResources -list $list -useCim $useCIM

$error.Clear()
return $list
}

#****************************************************************************************************************
# FUNCTION: CreateMonitoringData
# DESCRIPTION: Create monitoring data for a cluster shared volume.
#
# PARAMETERS: IN Object objClusterResource: the cluster disks as SWbemObject of type MSCluster_Disk.
# 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.
# RETURNS: Array List of Performance data
#****************************************************************************************************************
Function CreateMonitoringData($objClusterDisk, $objClusterDiskPartition, $objClusterResource,$list)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
if ($null -eq $list)
{
$error.Clear()
return
}

#Create performance data
$strPartitionName = $objClusterResource.$WMI_NAME_PROPERTY_NAME + "_" + $objClusterDiskPartition.$WMI_PATH_PROPERTY_NAME

#Total Size /MB
[void]$list.Add((CreatePerformanceTypedPropertyBag $PERF_OBJECT_CSV $PERF_COUNTER_TOTAL_SIZE_MB $strPartitionName `
$objClusterDiskPartition.$WMI_TOTALSIZE_PROPERTY_NAME $strPartitionName))

#Free Space /MB
[void]$list.Add((CreatePerformanceTypedPropertyBag $PERF_OBJECT_CSV $PERF_COUNTER_FREE_SPACE_MB $strPartitionName `
$objClusterDiskPartition.$WMI_FREESPACE_PROPERTY_NAME $strPartitionName))

#Free Space /%
[void]$list.Add((CreatePerformanceTypedPropertyBag $PERF_OBJECT_CSV $PERF_COUNTER_FREE_SPACE_PERCENT $strPartitionName `
([math]::Round(($objClusterDiskPartition.$WMI_FREESPACE_PROPERTY_NAME / $objClusterDiskPartition.$WMI_TOTALSIZE_PROPERTY_NAME), 2) * 100) $strPartitionName))

$error.Clear()
}

#***************************************************************************************
# 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)
{

return @{ $PERFORMANCE_OBJECT_NAME = $strObjectName;
$PERFORMANCE_COUNTER_NAME = $strCounterName;
$PERFORMANCE_INSTANCE_NAME = $strInstanceName;
$PERFORMANCE_VALUE_NAME = $varValue;
$INSTANCE_NAME = $strCSVInstanceName}
}


Function Get-ScriptName()
{
return "-- Cluster Disks Monitoring Script -- `n`nScript name: " + (Split-Path $MyInvocation.ScriptName -Leaf) + "`nVersion: " + $SCRIPT_VERSION + "`n"
}

################################################################
##Common Functions
################################################################
$CIM_ERROR_INVALIDCLASS = 2147749902
$CIM_ERROR_INVALIDNAMESPACE = 2147749904
$WMI_ERROR_INVALIDCLASS = -2147217392
$WMI_ERROR_INVALIDNAMESPACE = -2147217394
$WMI_CLUSTER_RESOURCE_TO_DISKPARTITION_CLASS = "MSCluster_ResourceToDiskPartition"
$WMI_MSCLUSTER_RESOURCE_CLASS = "MSCluster_Resource"
$WMI_CLUSTER_SHARED_VOLUME_TO_PARTITION_ASSOCIATOR_CLASS = "MSCluster_ClusterSharedVolumeToPartition"
$WMI_CLUSTER_SHARED_VOLUME_TO_RESOURCE_ASSOCIATOR_CLASS = "MSCluster_ClusterSharedVolumeToResource"
$WMI_ASSOCIATOR_DELIMITER = "="
$WMI_ASSOCIATOR_TRIMSYMBOL = """"
$WMI_ESCAPE_QUOTESYMBOL = "\"""
$WMI_SINGLE_QUOTESYMBOL = "'"
$WMI_ESCAPE_SINGLE_QUOTESYMBOL = "\'"
$WMI_ESCAPE_BACKSLASH_SYMBOL = "\\"

Function Check-WmiExceptions($Exception,[bool]$UseCim)
{
$result = $false

if ($true -eq $UseCim)
{
$result = Check-CimException -Exception $Exception
}
else
{
$result = Check-WmiException -Exception $Exception
}

return $result
}

Function Check-WmiException ($Exception)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()

$bresult = $false

if ($null -eq $Exception)
{
return $bresult
}

$type = $Exception.GetType()

#Invalid ClassName or NameSpace
if([System.Management.ManagementException] -eq $type)
{
$ErrorCode = $Exception.ErrorCode.value__
if ($WMI_ERROR_INVALIDCLASS -eq $ErrorCode -or $WMI_ERROR_INVALIDNAMESPACE -eq $ErrorCode)
{
$bresult = $true
}
}

return $bresult

}

Function Check-CimException ($Exception)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()

$bresult = $false

if ($null -eq $Exception)
{
return $bresult
}

$type = $Exception.GetType()
$error.Clear()
$WmiExcept = [Microsoft.Management.Infrastructure.CimException] -eq $type
if ($error.Count -ne 0)
{
$error.Clear()
return $bresult
}
#Invalid ClassName or NameSpace
if($WmiExcept)
{
$ErrorCode = $Exception.ErrorData.Error_code
if ($CIM_ERROR_INVALIDCLASS -eq $ErrorCode -or $CIM_ERROR_INVALIDNAMESPACE -eq $ErrorCode)
{
$bresult = $true
}
}

return $bresult

}

Function Convert-ToBoolean([string] $sBool)
{
[bool] $result = $false
[bool] $iresult = $false

if ($false -eq [string]::IsNullOrEmpty($sBool) )
{
$result = $sBool.Equals("true",[System.StringComparison]::InvariantCultureIgnoreCase)
$iresult = $sBool.Equals("1",[System.StringComparison]::InvariantCultureIgnoreCase)
$result = $result -or $iresult
}

return $result
}


Function CheckCimModule()
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()

$retval = $false
$cim = Get-Module -Name CimCmdlets

########Check for powershell 1.0
if ($error.Count -ne 0)
{
$type = $error[0].Exception.GetType()
if ([System.Management.Automation.CommandNotFoundException] -eq $type)
{
return $retval
}

$error.Clear()
}

if ($null -eq $cim)
{
Import-Module CimCmdLets
if ($error.Count -eq 0)
{
$retval = $true
}

$error.Clear()
}
else
{
$retval = $true
}

return $retval
}

function CheckCmdLets()
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()

$retval = $false
$objSWbemObjectSet =$null

if (CheckCimModule)
{
Load-CimModules
try
{
$cimSessionOption = New-CimSessionOption -Protocol DCOM
$cimsession = New-CimSession -SessionOption $cimSessionOption
$objSWbemObjectSet = Get-CimInstance -CimSession $cimsession -Class Win32_OperatingSystem -ErrorAction Stop
}
catch
{
$objSWbemObjectSet = Get-WMIObject -Class Win32_OperatingSystem -ErrorAction Stop
}
Finally
{
Get-CimSession | Remove-CimSession
$cimsession =$null
$cimSessionOption = $null
}

if ($error.Count -eq 0)
{
$retval = $true
}
}

$error.Clear()
return $retval;
}

Function Get-ClusterNameFromRegistry
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
$ClusterPath = "HKLM:\Cluster"
$key = Get-ItemProperty -Path $ClusterPath -Name "ClusterName"
$result = [string]::Empty

if ($key -ne $null)
{
$result = $key.ClusterName
}

$error.Clear()
return $result

}

Function Get-ClusterName ($useCim)
{
$ClusterName = Get-ClusterNameFromRegistry

If (-Not [string]::IsNullOrEmpty($ClusterName) )
{
return $ClusterName
}

$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
$WMI_MSCLUSTER_NAMESPACE_Local = "root\MSCluster"

if($useCIM)
{

Load-CimModules
try
{
$cimSessionOption = New-CimSessionOption -Protocol DCOM
$cimsession = New-CimSession -SessionOption $cimSessionOption
$Clusters = Get-CimInstance -CimSession $cimsession -ClassName MSCluster_Cluster -NameSpace $WMI_MSCLUSTER_NAMESPACE_Local
}
catch
{
$Clusters = Get-WMIObject -ClassName MSCluster_Cluster -NameSpace $WMI_MSCLUSTER_NAMESPACE_Local
}
Finally
{
Get-CimSession | Remove-CimSession
$cimsession =$null
$cimSessionOption = $null
}

if ($error.Count -ne 0)
{
return $ClusterName
}

foreach ($Cluster in $Clusters)
{
$ClusterName = $Cluster.Name
break
}
}else
{
$Clusters = Get-WmiObject -Class MSCluster_Cluster -NameSpace $WMI_MSCLUSTER_NAMESPACE_Local
if ($error.Count -ne 0)
{
return $ClusterName
}

foreach ($Cluster in $Clusters)
{
$ClusterName = $Cluster.Name
break
}
}

$error.Clear()

return $ClusterName

}


Function Get-ComputerName([string]$strTargetComputer)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()
$strTargetComputerShortName = $strTargetComputer

if([string]::IsNullOrEmpty($strTargetComputerShortName))
{
return [string]::Empty
}

$index = $strTargetComputer.IndexOf(".")

if ( $index -gt 0)
{
$strTargetComputerShortName = $strTargetComputer.Substring(0, $index)
}

return $strTargetComputerShortName
}

Function LogScriptEvent($LogType,$EventId,$EventLevel,$Message,$objMomScriptAPI)
{
$ErrorActionPreference = 'SilentlyContinue'
$error.Clear()

$LogScriptName = Get-ScriptName
if ( 0 -eq $LogType)
{
if ($null -eq $objMomScriptAPI)
{
return
}

$objMomScriptAPI.LogScriptEvent($LogScriptName,$EventId,$EventLevel,$Message)
}
}

Function WMIGetAssociatorInstanceNoAbort
{
param (
[string]$sAssociatorName,
[string]$sWmiAssociatorName,
$oWmiInstance,
[bool] $useCim
)

$Error.Clear()
if ($true -eq $useCim)
{
Load-CimModules
try
{
$cimSessionOption = New-CimSessionOption -Protocol DCOM
$cimsession = New-CimSession -SessionOption $cimSessionOption
$oInstance = Get-CimAssociatedInstance -CimSession $cimsession -InputObject $oWmiInstance -Association $sAssociatorName -ErrorAction SilentlyContinue
}
catch
{
if($false -eq [string]::IsNullOrEmpty($oWmiInstance))
{
$ErrorActionPreference = 'SilentlyContinue'
$oInstance = $oWmiInstance.GetRelated($sWmiAssociatorName)
}
else
{
$oInstance = $null
}
}
Finally
{
Get-CimSession | Remove-CimSession
$cimsession =$null
$cimSessionOption = $null
}
}
else
{

if($false -eq [string]::IsNullOrEmpty($oWmiInstance))
{
$ErrorActionPreference = 'SilentlyContinue'
$oInstance = $oWmiInstance.GetRelated($sWmiAssociatorName)
}
else
{
$oInstance = $null
}
}

return $oInstance
}

Function WMIGetInstanceNoAbort
{
param (
[string]$sNamespace,
[string]$sClassName,
[string]$sFilter,
[bool] $useCim
)

if ($true -eq $useCim)
{
if([string]::IsNullOrEmpty($sFilter))
{
Load-CimModules
try
{
$cimSessionOption = New-CimSessionOption -Protocol DCOM
$cimsession = New-CimSession -SessionOption $cimSessionOption
$oInstance = Get-CimInstance -CimSession $cimsession -Namespace $sNamespace -ClassName $sClassName -ErrorAction SilentlyContinue
}
catch
{
$oInstance = Get-WMIObject -Namespace $sNamespace -ClassName $sClassName -ErrorAction SilentlyContinue
}
Finally
{
Get-CimSession | Remove-CimSession
$cimsession =$null
$cimSessionOption = $null
}
}
else
{
Load-CimModules
try
{
$cimSessionOption = New-CimSessionOption -Protocol DCOM
$cimsession = New-CimSession -SessionOption $cimSessionOption
$oInstance = Get-CimInstance -CimSession $cimsession -Namespace $sNamespace -ClassName $sClassName -Filter $sFilter -ErrorAction SilentlyContinue
}
catch
{
$oInstance = Get-WMIObject -Namespace $sNamespace -ClassName $sClassName -Filter $sFilter -ErrorAction SilentlyContinue
}
Finally
{
Get-CimSession | Remove-CimSession
$cimsession =$null
$cimSessionOption = $null
}
}
}
else
{
if([string]::IsNullOrEmpty($sFilter))
{

$oInstance = Get-WmiObject -Class $sClassName -Namespace $sNamespace -ErrorAction SilentlyContinue
}
else
{
$oInstance = Get-WmiObject -Class $sClassName -Namespace $sNamespace -Filter $sFilter -ErrorAction SilentlyContinue
}
}

return $oInstance
}

Function Throw-ScriptError([string]$Message)
{
throw $Message
exit
}


Function Get-CSVResource([string]$VolumeId,[hashtable]$VolumeToResource)
{
if ($null -eq $VolumeToResource -or [string]::IsNullOrEmpty($VolumeId))
{
return $null
}

$CsvResource = $VolumeToResource[$VolumeId]

return $CsvResource
}

Function Get-CSVPartition([string]$VolumeId,[hashtable]$VolumeToPartition)
{
if ($null -eq $VolumeToPartition -or [string]::IsNullOrEmpty($VolumeId))
{
return $null
}

$CsvPartition = $VolumeToPartition[$VolumeId]

return $CsvPartition
}

Function Get-ResourceNameToVolumeTable([bool]$useCim,[hashtable]$ResourceToVolume)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()

if ($null -eq $ResourceToVolume)
{
return $false
}

$ResourceToVolumes = WMIGetInstanceNoAbort -sNamespace $WMI_MSCLUSTER_NAMESPACE -sClassName $WMI_CLUSTER_RESOURCE_TO_DISKPARTITION_CLASS -sFilter $null -useCim $useCim

if ($error.Count -ne 0)
{
return $false
}

foreach($Res in $ResourceToVolumes)
{
if($true -eq $useCim)
{
$ResourceName = $Res.GroupComponent.Name
$VolumeId = $Res.PartComponent.Path
}
else
{
$ResourceName = Get-WmiAssociatorId -ObjectId $Res.GroupComponent
$VolumeId = Get-WmiAssociatorId -ObjectId $Res.PartComponent
}

if ($false -eq [string]::IsNullOrEmpty($ResourceName))
{
$Volumes = $ResourceToVolume[$ResourceName]
if($null -eq $Volumes)
{
$Volumes = @()
}

$Volumes += $VolumeId
$ResourceToVolume[$ResourceName] = $Volumes
}
}

return $true

}

Function Get-VolumeToResourceTable([bool]$useCim,[hashtable]$VolumeToResource)
{
if ($null -eq $VolumeToResource)
{
return $false
}

$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()

$Filter = "ResourceClass = 1 and IsClusterSharedVolume = TRUE"
$ResourceNameToVolume = @{}

$Result = Get-ResourceNameToVolumeTable -useCim $useCim -ResourceToVolume $ResourceNameToVolume

if ($false -eq $Result)
{
return $false
}

$Filter = "ResourceClass = 1 and IsClusterSharedVolume = TRUE"
$Resources = WMIGetInstanceNoAbort -sNamespace $WMI_MSCLUSTER_NAMESPACE -sClassName $WMI_MSCLUSTER_RESOURCE_CLASS -sFilter $Filter -useCim $useCim

if ($error.Count -ne 0)
{
return $false
}

foreach($Resource in $Resources)
{
$ResName = $Resource.Name
if([string]::IsNullOrEmpty($ResName) )
{
continue
}

$Volumes = $ResourceNameToVolume[$ResName]
foreach($Volumeid in $Volumes)
{
$VolumeToResource[$VolumeId] = $Resource
}


}

return $true
}

Function Get-VolumeToPartitionTable([bool]$useCim,[hashtable]$VolumeToPartition)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()

if ($null -eq $VolumeToPartition)
{
return $false
}


$Partitions = WMIGetInstanceNoAbort -sNamespace $WMI_MSCLUSTER_NAMESPACE -sClassName "MSCluster_DiskPartition" -sFilter $null -useCim $useCim

if ($error.Count -ne 0)
{
return $false
}

foreach ($Partition in $Partitions)
{
$VolumeToPartition[$Partition.Path] = $Partition
}


return $true

}

Function Get-ResourceNameToResourceTable([bool]$useCim,[hashtable]$NameToResource,[string]$Filter)
{
$ErrorActionPreference = 'SilentlyContinue' # Scoped only to function
$error.Clear()

if ($null -eq $NameToResource)
{
return $false
}


$Resources = WMIGetInstanceNoAbort -sNamespace $WMI_MSCLUSTER_NAMESPACE -sClassName "MSCluster_Resource" -sFilter $Filter -useCim $useCim

if ($error.Count -ne 0)
{
return $false
}

foreach ($Resource in $Resources)
{
$NameToResource[$Resource.Name] = $Resource
}


return $true

}

Function Get-VolumeTables([bool]$useCim,[hashtable]$VolumeToResource,[hashtable]$VolumeToPartition,$objClusterSharedVolumes)
{
$Result = Get-VolumeToResourceTable -useCim $useCim -VolumeToResource $VolumeToResource
if ($false -eq $Result)
{
$Result = Get-CSVolIdToResource -useCim $useCim -VolumeToResource $VolumeToResource -objClusterSharedVolumes $objClusterSharedVolumes
if ($false -eq $Result)
{
return $Result
}

}

$Result = Get-VolumeToPartitionTable -useCim $useCim -VolumeToPartition $VolumeToPartition

return $Result

}

Function Get-CSVolIdToResource([bool]$useCim,[hashtable]$VolumeToResource,$objClusterSharedVolumes)
{
if ( $null -eq $VolumeToResource)
{
return $false
}

$CSV = @{}
$NameToResource = @{}
$Filter = " ResourceClass = 1 and IsClusterSharedVolume = TRUE"

$Result = Get-ResourceNameToResourceTable -useCim $useCim -NameToResource $NameToResource -Filter $Filter
if ($false -eq $Result)
{
return $Result
}

$Volumes = WMIGetInstanceNoAbort -sNamespace $WMI_MSCLUSTER_NAMESPACE -sClassName $WMI_CLUSTER_SHARED_VOLUME_TO_RESOURCE_ASSOCIATOR_CLASS -sFilter $null -useCim $useCim
if ($error.Count -ne 0)
{
return $false
}

foreach($objCSV in $objClusterSharedVolumes)
{
if ([string]::IsNullOrEmpty($objCSV.Name))
{
continue
}

$CSV[$objCSV.Name] = $objCSV.VolumeName
}

foreach($Volume in $Volumes)
{
if($true -eq $useCim)
{
$CsvName = $Volume.GroupComponent.Name
$ResName = $Volume.PartComponent.Name
}
else
{
$CsvName = Get-WmiAssociatorId -ObjectId $Volume.GroupComponent
$ResName = Get-WmiAssociatorId -ObjectId $Volume.PartComponent
}

if ([string]::IsNullOrEmpty($CsvName) -or [string]::IsNullOrEmpty($ResName))
{
continue
}

$VolId = $CSV[$CsvName]
$Resource = $NameToResource[$ResName]
$VolumeToResource[$VolId] = $Resource
}

return $true
}

Function Get-WmiAssociatorId([string]$ObjectId)
{
if([string]::IsNullOrEmpty($ObjectId))
{
return [string]::Empty
}

$Index = $ObjectId.IndexOf($WMI_ASSOCIATOR_DELIMITER)

if ($Index -lt 3 -or $Index -eq ($ObjectId.Length - $WMI_ASSOCIATOR_DELIMITER.Length ))
{
return [string]::Empty
}

$Id = $ObjectId.Substring($Index + $WMI_ASSOCIATOR_DELIMITER.Length).Trim($WMI_ASSOCIATOR_TRIMSYMBOL)
$Id = Convert-WMIStringToString -WmiString $Id

return $Id
}

Function Convert-WMIStringToString([string]$WmiString)
{
$rString = ( ($WmiString -replace("\\\\","\")) -replace("\\'",$WMI_SINGLE_QUOTESYMBOL) ) -replace ("\\""",$WMI_ASSOCIATOR_TRIMSYMBO)
return $rString
}

Function Convert-StringToWmiString([string]$sString)
{
$rString = ( ($sString -replace($WMI_ESCAPE_BACKSLASH_SYMBOL,$WMI_ESCAPE_BACKSLASH_SYMBOL)) -replace($WMI_SINGLE_QUOTESYMBOL,$WMI_ESCAPE_SINGLE_QUOTESYMBOL) ) -replace ($WMI_ASSOCIATOR_TRIMSYMBOL,$WMI_ESCAPE_QUOTESYMBOL)
return $rString
}

Function Load-CimModules
{
$error.Clear()

$CimModule = Get-Module CimCmdlets

if ($null -eq $CimModule)
{
Import-Module CimCmdlets
$error.Clear()
}
}

Main


</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>mblnLogSuccessEvent</Name>
<Value>$Config/LogSuccessEvent$</Value>
</Parameter>
<Parameter>
<Name>mstrIdentifier</Name>
<Value>$Config/ScriptGroupId$</Value>
</Parameter>
<Parameter>
<Name>strTargetComputer</Name>
<Value>$Config/TargetComputer$</Value>
</Parameter>
<Parameter>
<Name>strClusterName</Name>
<Value>$Config/ClusterName$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</ProbeAction>
<ConditionDetection ID="PerfMapper" TypeID="SystemPerf!System.Performance.DataGenericMapper">
<ObjectName>$Data/Property[@Name='PerfObject']$</ObjectName>
<CounterName>$Data/Property[@Name='PerfCounter']$</CounterName>
<InstanceName>$Data/Property[@Name='PerfInstance']$</InstanceName>
<Value>$Data/Property[@Name='PerfValue']$</Value>
</ConditionDetection>
<ConditionDetection ID="InstanceFilter" TypeID="System!System.ExpressionFilter">
<Expression>
<And>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="String">Property[@Name='PerfInstance']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">$Config/ClusterResourceName$</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="String">Property[@Name='PerfCounter']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">$Config/CounterName$</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</And>
</Expression>
</ConditionDetection>
</MemberModules>
<Composition>
<Node ID="PerfMapper">
<Node ID="InstanceFilter">
<Node ID="DS">
<Node ID="Scheduler"/>
</Node>
</Node>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>SystemPerf!System.Performance.Data</OutputType>
</DataSourceModuleType>