-
-
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 :)
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
Hello there; I am not sure if it was the most recent Windows 11 update that was forced, but I have been exposed to this [new to me] "Constrained Language Mode" within PS. Here is the Keepalive script I normally run:
The above script (and any script that tries to leverage the "New-Object -ComObject WScript.Shell" command (or series of commands)) is met with the following error dialog (I will redact certain elements with an asterisk (*).
--- Begin Error Output ---
New-Object : Cannot create type. Only core types are supported in this language mode.
At C:\Users*REDACTED*\OneDrive - PATHREDACTED\Documents\PowerShell\Alive.ps1:7 char:11
You cannot call a method on a null-valued expression.
At C:\Users*REDACTED*\OneDrive - PATHREDACTED\Documents\PowerShell\Alive.ps1:15 char:3
You cannot call a method on a null-valued expression.
At C:\Users*REDACTED*\OneDrive - PATHREDACTED\Documents\PowerShell\Alive.ps1:19 char:3
--- End Error Output ---
I can see that there's the whole permission denied bit, but that's interesting [at least to me] because previous permissions issues were cleared up by running the following:
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
So, that Execution Policy is still in effect (verified by running Get-ExecutionPolicy; still set to Unrestricted). Which brings me to the root of my question; was this Constrained Language Mode automatically pushed by Microsoft for Windows 11, i.e., is the Language Mode being unconstrained a vulnerability [that has now been modified as to not be exploited]?
Background -- this keep alive PS Script was useful for kiosks in customer lobbies or in some cases where a presentation deck is run in a cyclical manner all day (in a common area or in an auditorium). It is also useful for operational type situations, such as air operations, where a particular status board needs to remain up and visible without a screen saver turning on or a display going to sleep.
I'd appreciate any guidance or even workarounds on how to make keep alive type PS scripts function in this new [to me] Constrained Language paradigm.
Thank you in advance,
The Beer Genius