-
-
Save jamesfreeman959/231b068c3d1ed6557675f21c0e346a9c to your computer and use it in GitHub Desktop.
# Useful references: | |
# | |
# https://superuser.com/questions/992511/emulate-a-keyboard-button-via-the-command-line | |
# https://ss64.com/vb/sendkeys.html | |
# https://social.technet.microsoft.com/Forums/windowsserver/en-US/96b339e2-e9da-4802-a66d-be619aeb21ac/execute-function-one-time-in-every-10-mins-in-windows-powershell?forum=winserverpowershell | |
# https://learn-powershell.net/2013/02/08/powershell-and-events-object-events/ | |
# | |
# Future enhancements - use events rather than an infinite loop | |
$wsh = New-Object -ComObject WScript.Shell | |
while (1) { | |
# Send Shift+F15 - this is the least intrusive key combination I can think of and is also used as default by: | |
# http://www.zhornsoftware.co.uk/caffeine/ | |
# Unfortunately the above triggers a malware alert on Sophos so I needed to find a native solution - hence this script... | |
$wsh.SendKeys('+{F15}') | |
Start-Sleep -seconds 59 | |
} |
This works great without admin privilege in work computer on windows 10. Thanks :)
Indeed... and it did work on Windows 11 without admin privilege until about a week ago [when an update of some sort was pushed that changed/added the Constrained Language mode].
((Commenting to bump in order to solicit possible replies))
This works great without admin privilege in work computer on windows 10. Thanks :)
Indeed... and it did work on Windows 11 without admin privilege until about a week ago [when an update of some sort was pushed that changed/added the Constrained Language mode].
((Commenting to bump in order to solicit possible replies))
Try this (it must be in a .bat file):
@if (@a==@a) @end /*
@echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
set starttime=%TIME%
:loop
cls
set /a rng = %RANDOM% * 60 / 32768 + 30
echo Sending [Shift]+[F15]
echo Start: %starttime%
echo Latest: %TIME%
%SendKeys% "+{F15}"
timeout /t %rng% /nobreak
goto loop
*/
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
I realize the original use case was to keep the Windows computer awake AND for another app (lync) to see keyboard activity. However for anyone who just needs the 1st part (to prevent Windows from sleeping and optionally keep the screen on) another solution was posted by @dend on his blog. You can also use the Awake utility from PowerToys which was created by the same author.
My use case: to keep the Windows machine hosting OpenSSH server awake when there is an active ssh connection using a pwsh session (seems like a bug in Windows OpenSSH tbh). I didn't need the screen to stay on and also didn't seem to require using a background thread as Den originally did (maybe something changed in .NET or PowerShell versions since his post, idk). So I just put this in my pwsh $PROFILE:
if (Test-Path env:SSH_CLIENT) { # only do this for a remote ssh user's session, but if that's not your thing remove this conditional
$Signature=@"
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
"@
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001"
$ES_CONTINUOUS = [uint32]"0x80000000"
$stes = Add-Type -MemberDefinition $Signature -Name System -Namespace Win32 -PassThru
$stes::SetThreadExecutionState($ES_CONTINUOUS -bor $ES_SYSTEM_REQUIRED)
Write-Host "Keeping computer awake"
}
If you need the screen to stay on change the script to use the appropriate flags with SetThreadExecutionState.
Thank you @dend
@stringTrimmer thank you for the shout-out! 🥳
I added some extra features and built an EXE using some of your code.
Random keep alive time
Dynamic time to run (application will ask you if you didn't enter it at run time)
Add a Pause at completion (optional)
Included a compiled version
https://github.com/jheinrichs79/Public/blob/main/Powershell/Utilities/Start-KeepAwake/
https://github.com/jheinrichs79/Public/blob/main/Powershell/Utilities/Start-KeepAwake/Start-KeepAwake.ps1
https://github.com/jheinrichs79/Public/blob/main/Powershell/Utilities/Start-KeepAwake/Start-KeepAwake.exe
Is keepawake.ps1 intended to work on Windows Home? I got unauthorized error. This work provisioned laptop really sucks!!
File C:\Users\user1\Documents\MyScripts\keepawake.ps1 cannot be loaded because running scripts is disabled on this
system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess
p.s. apologies probably not in the correct venue. Stumbled on this thread looking for solution. former programmer but 90's-00s
Nevermind! the .exe works just fine. I've recently run into other limitations of Windows Home so I was projecting blame to Home b4 even digging.
Thank you @jheinrichs79
Is keepawake.ps1 intended to work on Windows Home? I got unauthorized error. This work provisioned laptop really sucks!!
File C:\Users\user1\Documents\MyScripts\keepawake.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess
p.s. apologies probably not in the correct venue. Stumbled on this thread looking for solution. former programmer but 90's-00s
@noviceboomer - Any time you run any Powershell script you need to make sure your security policies allow for you to run a powershell script. You most likely have a policy that states not to run a public or non-sign script. Google "Set-ExecutionPolicy" and find the setting that will work for you. Also when you download a script from the internet you need to right click it and say that you trust it as it came from an untrusted network (internet).
This works great without admin privilege in work computer on windows 10. Thanks :)