Quick Powershell Script for Plex/Grafana

Here’s a quick example script for pushing data from a web API with powershell, into a InfluxDB Database. Replace “TOKEN” with your Plex token.

while ($true)
{
 
#clean previous connection
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint("http://192.168.99.118:8086/write?db=cacti2")
#you could do something like this or manually clear out as required.
$ServicePoint.ConnectionLimit = 10
#start clean
$ServicePoint.CloseConnectionGroup("")
 
# Server URL of PlexPY API to request from
$request = "http://slimleech.local:32400/status/sessions/?X-Plex-Token=TOKEN"
 
 
#Grab the data and count it
$object = Invoke-WebRequest $request | select content
$result = $object | select-string -allmatches -Pattern 'Player' | Select-Object -ExpandProperty Matches
$final = $result.count
 
#Push the data to influxdb
$uri = 'http://192.168.99.118:8086/write?db=cacti2'
$postParams = "plex_users,host=slimleech value=$final"
Invoke-RestMethod -Uri $uri -Method POST -Body $postParams
 
 
Write-Host "Number of People watching is $final"
 
$ServicePoint.CloseConnectionGroup("")
start-sleep 60
 
}
</code>
Share this content:

Leave a Comment

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