Script directory for a PowerShell profile. https://blog.lksz.me/keeping-track-of-everything/#-mypsscriptroot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.7 KiB

  1. param([switch]$FetchFromServer,[string]$PlexHost='localhost',[switch]$PassThruOnly)
  2. $local:content = $(try{(Get-Content /run/plex.stream.counter/sessions -Raw -ErrorAction Stop)}catch{})
  3. if( $FetchFromServer ) {
  4. $local:creds = New-Object System.Management.Automation.PsCredential($env:PLEX_USERNAME,$(ConvertTo-SecureString -AsPlainText $env:PLEX_PASSWORD -Force))
  5. $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"
  6. $local:plexHeaders = @{ "X-Plex-Token" = ([xml]$webResp.Content).user.authenticationToken }
  7. $webResp = Invoke-WebRequest -Headers $plexHeaders "http://${PlexHost}:32400"
  8. $local:plexVersion = ([xml]$webResp.Content).MediaContainer.version
  9. Write-Host -ForegroundColor Cyan "Plex Version: $plexVersion"
  10. $webResp = Invoke-WebRequest -Headers $plexHeaders "http://${PlexHost}:32400/status/sessions"
  11. $content = $webResp.Content
  12. }
  13. $local:PlexInfo = $(try{([xml]$content).MediaContainer}catch{})
  14. $local:result = $PlexInfo.ChildNodes | ForEach-Object { [PSCustomObject]@{
  15. Object = $_
  16. Type = $_.type
  17. Library = $_.librarySectionTitle
  18. Title = $(@($_.grandparentTitle,$_.parentIndex,$_.parentTitle,$_.index,$_.title) | Where-Object {$_}) -join ' | '
  19. User = $_.User.title
  20. Offset = [TimeSpan]::FromMilliseconds($_.viewOffset).ToString() -split '\.' | select-object -first 1
  21. Duration = [TimeSpan]::FromMilliseconds($_.duration).ToString() -split '\.' | select-object -first 1
  22. Player = "$($_.Player.Platform)|$($_.Player.State)"
  23. # UpdateTime = [TimeSpan]::FromMilliseconds($_.).ToString()
  24. } }
  25. if( $PassThruOnly ) { return $result }
  26. $result | Format-Table