Skip to content

Instantly share code, notes, and snippets.

@KakersUK
Last active March 20, 2025 09:38
Show Gist options
  • Save KakersUK/d090e1836ffb881d29c9f529b380f795 to your computer and use it in GitHub Desktop.
Save KakersUK/d090e1836ffb881d29c9f529b380f795 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.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$xmlResponseIRM = Invoke-RestMethod "https://${PlexHost}:${PlexPort}/status/sessions?X-Plex-Token=${PlexToken}" -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}')
}
@coreyspeed
Copy link

Script works if you change line 16 and 17 to the below

GET the active play session information from our Plex API.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$xmlResponseIRM = Invoke-RestMethod "https://${PlexHost}:${PlexPort}/status/sessions?X-Plex-Token=${PlexToken}" -Method Get

@KakersUK
Copy link
Author

Thanks! I don't use this method anymore after I migrated to Proxmox.
I've updated the script with your change.

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