Get ADFS token requests remotely using PowerShell

Today I want to share simple script for getting ADFS token requests remotely. It might be useful if you want to ensure that load is equally processed in your farm.

ADFS token requests

Script is based on Get-Counter command where we have to specify ADFS tokens counter "\AD FS\token requests/sec". It monitors the number of token requests sent to the federation server including SSOAuth token requests per second.

More info about this command can be found on Microsoft docs page:

#Locally
Get-Counter "\AD FS\token requests/sec"

#Remotely
Invoke-Command ADFS01 -Scriptblock{ Get-Counter "\AD FS\token requests/sec" }

#Remotely shorten version
icm -cn ADFS01 { Get-Counter "\AD FS\token requests/sec" }
Get-Counter
Get-Counter

For each server script will create temporary object and display it in console. Additionally it will add object to $Report array. At the end you can export it to CSV file.

Token Requests
Token Requests

Final script:


#Server list
$Servers = Get-Content -Path "D:\scripts\ADFS_Servers.txt"

$Report = @()
 
#Looping servers
Foreach($Server in $Servers)
{ 
    $Server = $Server.Trim()
    $a = $null
    $CValue = $null

    $temp = '' | Select ServerName, TimeStamp, Tokens
    $temp.ServerName = $Server

    Try
    {
	    $a = icm -ErrorAction Stop -cn $server { Get-Counter "\AD FS\token requests/sec" }
    }
    Catch
    {
        $temp.TimeStamp = " - "
        $temp.Tokens = " - "
        
        Write-Warning $_.Exception#.Message
    }
    If($a)
    {
        foreach ($req in $a) 
        {
		    $timest = $req.timestamp
            $CValue = (($req.readings).split("{:}")[1].trim())
            $CValue = [math]::Round($CValue,3)
        }

        $temp.TimeStamp = $timest
        $temp.Tokens = $CValue
    }

    $temp
    $report += $temp
}
$report | Export-Csv -Path "d:\scripts\Tokens.csv" -NoTypeInformation -Force

We can use code lines from one of the previous articles about creating first chart report and prepare nice looking report for ADFS tokens:


#Building chart 
[void][Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void][Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms.DataVisualization')
  
$Chart = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Size = '800,750'
 
$ChartArea = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisX.Title = 'Token requests'
$ChartArea.AxisY.Title = '\AD FS\token requests/sec'
$ChartArea.AxisX.Interval = '1'
$ChartArea.AxisX.LabelStyle.Enabled = $true
$ChartArea.AxisX.LabelStyle.Angle = 90
$Chart.ChartAreas.Add($ChartArea)
$End = $Chart.Series.Add('Token')
 
#Define chart type - I use "StackedBar"
$Chart.Series['Token'].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::StackedBar

#Server list
$Servers = Get-Content -Path "D:\scripts\servers.txt"
 
#Looping servers
Foreach($Server in $Servers)
{ 
    $Server = $Server.Trim()
    $a = $null
    $CValue = $null

    Write-Host "Processing $Server - " -ForegroundColor Green -NoNewLine
    Try
    {
	    $a = icm -ErrorAction Stop -cn $server { Get-Counter "\AD FS\token requests/sec" }
    }
    Catch
    {
        Write-Host "Token requests/s: 0"
        Write-Warning $_.Exception.Message
        $Value = $Chart.Series['Token'].Points.AddXY("$Server","0")
    }
    If($a)
    {
        foreach ($req in $a) 
        {
		    $timest = $req.timestamp
            $CValue = (($req.readings).split("{:}")[1].trim())
            $CValue = [math]::Round($CValue,3)
		    #$CValue = [math]::Round(($req.readings).split("{:}")[1].trim())
            
            Write-host "Token requests/s: $CValue"
        }
        #Adding token value to chart
        $Value = $Chart.Series['Token'].Points.AddXY("$Server","$CValue")
    }
}
 
$Title = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($Title)
$Chart.Titles[0].Text = 'IAM CSS Team'

[string]$FileName = "Chart_" + (Get-Date -format "dd-MMM-yyyy_HHmm") + ".png" 
 
#Saving PNG file on desktop
$Chart.SaveImage("D:\scripts\$FileName", "PNG")

Write-Host "`nResults saved: " -NoNewline
"D:\scripts\$FileName" 

pause

Console:

Token Requests console
Token Requests console

Chart:

Token Requests chart
Token Requests chart

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.