2020-11-04 15:58:04 +00:00
|
|
|
param([switch]$FetchFromServer,[string]$PlexHost='localhost',[switch]$PassThruOnly)
|
2020-11-01 06:15:34 +00:00
|
|
|
|
2020-11-04 15:58:04 +00:00
|
|
|
$local:content = $(try{(Get-Content /run/plex.stream.counter/sessions -Raw -ErrorAction Stop)}catch{})
|
|
|
|
|
|
|
|
if( $FetchFromServer ) {
|
|
|
|
$local:creds = New-Object System.Management.Automation.PsCredential($env:PLEX_USERNAME,$(ConvertTo-SecureString -AsPlainText $env:PLEX_PASSWORD -Force))
|
|
|
|
$local:webResp = Invoke-WebRequest -Headers @{ "Content-Length" = "0"; "X-Plex-Client-Identifier" = "my-app" } -Credential $creds -Method Post -Uri "https://my.plexapp.com/users/sign_in.xml"
|
|
|
|
$local:plexHeaders = @{ "X-Plex-Token" = ([xml]$webResp.Content).user.authenticationToken }
|
|
|
|
$webResp = Invoke-WebRequest -Headers $plexHeaders "http://${PlexHost}:32400"
|
|
|
|
$local:plexVersion = ([xml]$webResp.Content).MediaContainer.version
|
|
|
|
Write-Host -ForegroundColor Cyan "Plex Version: $plexVersion"
|
|
|
|
$webResp = Invoke-WebRequest -Headers $plexHeaders "http://${PlexHost}:32400/status/sessions"
|
|
|
|
$content = $webResp.Content
|
|
|
|
}
|
|
|
|
|
|
|
|
$local:PlexInfo = $(try{([xml]$content).MediaContainer}catch{})
|
|
|
|
|
|
|
|
$local:result = $PlexInfo.ChildNodes | ForEach-Object { [PSCustomObject]@{
|
2020-11-01 06:15:34 +00:00
|
|
|
Object = $_
|
|
|
|
Type = $_.type
|
|
|
|
Library = $_.librarySectionTitle
|
|
|
|
Title = $(@($_.grandparentTitle,$_.parentIndex,$_.parentTitle,$_.index,$_.title) | Where-Object {$_}) -join ' | '
|
|
|
|
User = $_.User.title
|
|
|
|
Offset = [TimeSpan]::FromMilliseconds($_.viewOffset).ToString() -split '\.' | select-object -first 1
|
|
|
|
Duration = [TimeSpan]::FromMilliseconds($_.duration).ToString() -split '\.' | select-object -first 1
|
2020-11-04 15:58:04 +00:00
|
|
|
Player = "$($_.Player.Platform)|$($_.Player.State)"
|
2020-11-01 06:15:34 +00:00
|
|
|
# UpdateTime = [TimeSpan]::FromMilliseconds($_.).ToString()
|
|
|
|
} }
|
2020-11-04 15:58:04 +00:00
|
|
|
|
|
|
|
if( $PassThruOnly ) { return $result }
|
|
|
|
$result | Format-Table
|