# Function to add the DB Free Space to the data object.
# Parameters:
# dataObject - The data object that is being modified.
# PureArray instance - The database instance. This is used to get the performance data.
# criteria - Criteria to use for retrieving the performance data.
# startTime - The starting time of the performance data to collect.
# endTime - The ending time of the performance data to collect.
function SetAndReturnPerfCounterLastValue($dataObject, $instance, $criteria, $counterName, $startTime, $endTime)
{
$performanceDataCollection = $instance.GetMonitoringPerformanceData($criteria)
Log $ScriptName $GLOBAL:VERBOSE_LEVEL $performanceDataCollection
$valueReader = $performanceDataCollection.GetValueReader($startTime, $endTime)
$counter = 0
$sum = 0
while ($valueReader.Read()) {
$counter += 1
$sum += $valueReader.GetMonitoringPerformanceDataValue().SampleValue
}
if ($counter -eq 0) {
Log $ScriptName $GLOBAL:WARNING_LEVEL "No perf data for $counterName"
return $null
}
$perfValue = $sum / $counter
$perfValue = [Math]::Round($perfValue * 0.001, 4)
Log $ScriptName $GLOBAL:VERBOSE_LEVEL "Value for counter $counterName is $perfValue"
$dataObject[$counterName] = [string]($perfValue) + ' K/s'
return $perfValue
}
# The perf APIs require a start and end time
# In this case, we specify current time as end time and 1 hour ago as start time.
$endTime = [DateTime]::UtcNow
$timeDiff = [TimeSpan]::FromHours(1)
$startTime = $endTime - $timeDiff
foreach ($pureArray in $pureArrays)
{
# Create a data object for the each database specifying the database object and the properties to include.
$dataObject = $ScriptContext.CreateFromObject($pureArray, "Id=Id,DisplayName=DisplayName", "ArrayName=ArrayName")
# Call function to add the performance data to the data object.
$inputPerfCriteria = New-Object Microsoft.EnterpriseManagement.Monitoring.MonitoringPerformanceDataCriteria("CounterName='Reads per Second'")
$inputPerfValue = SetAndReturnPerfCounterLastValue $dataObject $pureArray $inputPerfCriteria 'Reads per Second' $startTime $endTime
$outputPerfCriteria = New-Object Microsoft.EnterpriseManagement.Monitoring.MonitoringPerformanceDataCriteria("CounterName='Writes per Second'")
$outputPerfValue = SetAndReturnPerfCounterLastValue $dataObject $pureArray $outputPerfCriteria 'Writes per Second' $startTime $endTime