Skip to content

Instantly share code, notes, and snippets.

@KakersUK
Last active May 3, 2024 05:17
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.
$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}')
}
@KakersUK
Copy link
Author

KakersUK commented Mar 15, 2023

My computer is set to sleep after 20 minutes and the above script is run in Task Scheduler every 10 minutes.

Task Scheduler settings

General
When running the task, use the following user account: (Local user or service account)
Run whether user is logged on or not
Configure for: Windows 10

Triggers
Daily
Repeat task every: 10 minutes

Actions
Action: Start a program
Program/script: "C:\Program Files\PowerShell\7\pwsh.exe"
Add arguments(optional): -File "C:\<Path to File>\plex-anti-sleep.ps1"

@KakersUK
Copy link
Author

KakersUK commented Mar 15, 2023

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

  1. Install Install PowerShell 7.
  2. Download script onto your machine.
  3. Find your X-Plex-Token and update the script.
  4. 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.

@johnterickson
Copy link

THANK YOU!

@DanySupernova
Copy link

Awesome! Thank you very much!

@Ezzzy007
Copy link

Ezzzy007 commented Mar 9, 2024

love the idea of this but cannot get it working, I get the following

Invoke-RestMethod : A parameter cannot be found that matches parameter name 'SkipCertificateCheck'.
At C:\plex-anti-sleep.ps1:17 char:113

  • ... tus/sessions?X-Plex-Token=${PlexToken}" -SkipCertificateCheck -Method ...
  •                                         ~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
    • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

@johnterickson
Copy link

johnterickson commented Mar 9, 2024 via email

@icescoop12
Copy link

icescoop12 commented May 2, 2024

hi the script isn't working can you please rectify
Tried everything allowing executionpolicy in PowerShell both versions to remotesigned and unrestricted
allowing in firewall exceptions ,running pwsh.exe as admin shortening time to when task scheduler runs the script ,script runs fine and completes via windows task scheduler
trying on two separate machines
double checked plex tokens and they are fine too

UPDATE: no does not work on any machine plex servers still sleep when streaming from

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