Skip to content

Instantly share code, notes, and snippets.

@StancuFlorin
Forked from KakersUK/plex-anti-sleep.ps1
Created November 23, 2023 18:46
Show Gist options
  • Save StancuFlorin/3ef95d6043d3e9437aca4bb24090970d to your computer and use it in GitHub Desktop.
Save StancuFlorin/3ef95d6043d3e9437aca4bb24090970d to your computer and use it in GitHub Desktop.
PowerShell 7 script to stop the PC from going to sleep if there are any active streams
# Plex on Windows Anti-Sleep
#
# References
# Gist: https://gist.github.com/KakersUK/d090e1836ffb881d29c9f529b380f795
# Install PowerShell 7: https://learn.microsoft.com/en-gb/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3#winget
# X-Plex-Token: https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
# Host variables
$PlexHost = '127.0.0.1'
$PlexPort = 32400
$PlexToken = ''
# Create Windows shell object.
$WshShell = New-Object -ComObject WScript.Shell
# GET the active play session information from our Plex API.
$xmlResponseIRM = Invoke-RestMethod "https://${PlexHost}:${PlexPort}/status/sessions?X-Plex-Token=${PlexToken}" -SkipCertificateCheck -Method Get
# If the sessions are greater than 0, Plex is streaming. Send a Shift + F15 key combo to keep the PC awake.
If([int]$xmlResponseIRM.MediaContainer.size -gt 0){
$WshShell.SendKeys('+{F15}')
}
@StancuFlorin
Copy link
Author

Setup Instructions:
Links can be found in the script's references.

Install Install PowerShell 7.
Download script onto your machine.
Find your X-Plex-Token and update the script.
Setup task in Windows Task Scheduler.
Plex settings:
"Support Away Mode when preventing system sleep" Not too sure on this setting, but I've turned this off to ensure the machine goes to sleep when not being used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment