I have a PowerShell script that I call from Telegraf to get who is logged into a server and only display the server, user name, connection state, & logon time. I thought I had the output formatted correctly (metric_name,tag=string,tag=string field=value,field=value), but that doesn’t appear to be true.
This is the PowerShell script I have:
$Servers = @(
"SV-DSSAAZ-DC02"
"SV-MSA-APP01"
"SV-ID10T-2"
)
#Initialize $Sessions which will contain all sessions
[System.Collections.ArrayList]$Sessions = New-Object System.Collections.ArrayList($null)
#Go through each server
Foreach ($Server in $Servers) {
#Get the current sessions on $Server and also format the output
$DirtyOuput = (quser /server:$Server) -replace '\s{2,}', ',' | ConvertFrom-Csv
#Go through each session in $DirtyOuput
Foreach ($session in $DirtyOuput) {
#Initialize a temporary hash where we will store the data
$tmpHash = @{}
#Check if SESSIONNAME isn't like "console" and isn't like "rdp-tcp*"
If (($session.sessionname -notlike "console") -AND ($session.sessionname -notlike "rdp-tcp*")) {
#If the script is in here, the values are shifted and we need to match them correctly
$tmpHash = @{
Username = $session.USERNAME
SessionName = "" #Session name is empty in this case
ID = $session.SESSIONNAME
State = $session.ID
IdleTime = $session.STATE
LogonTime = $session."IDLE TIME"
ServerName = $Server
}
}Else {
#If the script is in here, it means that the values are correct
$tmpHash = @{
Username = $session.USERNAME
SessionName = $session.SESSIONNAME
ID = $session.ID
State = $session.STATE
IdleTime = $session."IDLE TIME"
LogonTime = $session."LOGON TIME"
ServerName = $Server
}
}
#Add the hash to $Sessions
$Sessions.Add((New-Object PSObject -Property $tmpHash)) | Out-Null
}
}
#Display the sessions, sort by name, and just show Username, State, Logon time, and Server
$sessions | Sort Username |
foreach{
"RD_Sessions,host=$($_.Servername),rd_user=user user=$($_.Username),STATE=$($_.State),LOGON_TIME=$($_.LogonTime)"
}
This is the output displayed in PowerShell if I run the query manually.
PS C:> C:\Users\a-kfreeman\Desktop\RD_Sessions.ps1
RD_Sessions,host=SV-MSA-APP01,rd_user=user user=a-bsanchez,STATE=Disc,LOGON_TIME=10/20/2021 1:22 PM
RD_Sessions,host=SV-DSSAAZ-DC02,rd_user=user user=a-kfreeman,STATE=Active,LOGON_TIME=10/26/2021 10:08 AM
RD_Sessions,host=SV-MSA-APP01,rd_user=user user=a-kfreeman,STATE=Active,LOGON_TIME=10/25/2021 2:21 PM
RD_Sessions,host=SV-ID10T-2,rd_user=user user=a-kfreeman,STATE=Active,LOGON_TIME=11/2/2021 12:09 PM
RD_Sessions,host=SV-ID10T-2,rd_user=user user=kfreeman,STATE=Active,LOGON_TIME=11/2/2021 11:39 AM
This is the error I get in the Telegraf logs.
2021-11-03T17:45:00Z E! [inputs.exec] Error in plugin: metric parse error: expected field at 1:51: “RD_Sessions,host=SV-DSSAAZ-DC02,rd_user=user user=a-kfreeman,STATE=Active,LOGON_TIME=10/26/2021 10:08 AM”
I have been banging my head attempting to get this query working and fixing the output. If anyone has another PowerShell script to pull this data I would gladly switch.
I am running Telegraf 1.20.2 and InfluxDB 2.0.8
Thank You for any help.