PoSH Grid Dashboard for RDSH [#opsmgr]

TLDR

Customer wanted to see all RDS Host servers in a view with their current total session count.

Decided to use a powershell grid dashboard, and share the script.

Here’s the gist of it: SCOM_RDSH_TotalSession_PoSHWidget.ps1

How To

Keeping it fairly short this time.

Pre-requisites are:

  • System Center 2012 R2 with UR2 or later
  • Microsoft RDS Management Pack
  • Microsoft Windows Core OS Management Pack

Create a dashboard

  1. Rightclick and create a new view somewhere, make it a Dashboard View
  2. Enter Name and Description
  3. Select Grid type, and layout

Add a widget

  1. Click cog on grid view
  2. Select Powershell Grid Widget
  3. Set Name and Description
  4. Paste your script into the script box
  5. Save

The Script

# Based on https://gallery.technet.microsoft.com/Retrieve-Performance-Data-c4595e2a#content
function Add-Criteria()
{
    param($criteria,$clause)
    if ($criteria -ne $null)
        {$criteria += " AND $clause"}
    else
        {$criteria = $clause}
    return($criteria)
}

$rdsClass = Get-SCOMClass -Name "Microsoft.Windows.Server.2012.R2.RemoteDesktopServicesRole.Service.RDSessionHost"
$rdsHosts = Get-SCOMMonitoringObject -Class $rdsClass | Sort-Object -Property '[Microsoft.Windows.Computer].PrincipalName'
$rdsTotalSessionRule = Get-SCOMRule -Name "Microsoft.Windows.Server.2012.R2.RemoteDesktopServicesRole.Service.RDSessionHost.PerformanceCollection.TotalSessions"
$objectName = "Terminal Services"
$counterName = "Total Sessions"
$InstanceName = ""


$i=0
foreach ($rdsHost in $rdsHosts) {
    # Build performance criteria
    if ($rdsClass -ne $null) { $criteria = Add-Criteria -criteria $criteria -clause "MonitoringClassId = '{$($rdsClass.Id)}'" }
    if ($rdsHost -ne $null) { $criteria = Add-Criteria -criteria $criteria -clause "MonitoringObjectFullName = '$($rdsHost.FullName)'" }
    if ($objectName -ne $null) { $criteria = Add-Criteria -criteria $criteria -clause "ObjectName = '$objectName'" }
    if ($counterName -ne $null) { $criteria = Add-Criteria -criteria $criteria -clause "CounterName = '$counterName'" }

    #$criteria
    # Get Performance reader object.
    $perfReader = (Get-SCOMManagementGroup).GetMonitoringPerformanceDataReader($criteria)
    while ($perfReader.Read()) {
        $perfData = $perfReader.GetMonitoringPerformanceData()
        $perfValueReader = $perfData.GetValueReader(((Get-Date).AddMinutes(-180)),(Get-Date))

        $perfValues = @()
        while ($perfValueReader.Read()) {
            $perfValues += $perfValueReader.GetMonitoringPerformanceDataValue()
        }
        $perfValue = $perfValues | Sort-Object -Property TimeSampled | select -Last 1

        $dataObject = $ScriptContext.CreateInstance("xsd://stegenfeldt/nullschema")
        $dataObject = $ScriptContext.CreateFromObject($rdsHost, "Id=Id,Health=HealthState,Host=Path", $null)
        $dataObject["Time Sampled"] = $perfValue.TimeSampled
        $dataObject["Total Sessions"] = $perfValue.SampleValue
        $ScriptContext.ReturnCollection.Add($dataObject)
    }
    $criteria = $null
    ++$i
}
Share