Awhile back I was looking to see if anyone had provided a solution for this. I suspected it was possible, but didn't want to have to recreate what someone else would've already solved, if it was available.
I never found anyone else's solution, as provided in a form that was tested and verified to work in Power Automate Desktop. I therefore attempted it, and wanted to provide my result in case anyone else finds it useful.
The trick is to take advantage of Scripting actions. In this case, PowerShell is typically the most directly associated option to a Windows host and access to lower level system options, so I started there. I found some PowerShell scripts via GitHub that offered the ability to prevent sleep, force "away mode" (seemingly some sort of sleep state), and prevent sleep while also preventing the monitor/display from turning off. With that, I was able to create an action that forced the running process to prevent the host it is running on from going to sleep.
The PowerShell script:
# https://gist.github.com/CMCDragonkai/bf8e8b7553c48e4f65124bc6f41769eb
param (
[string]$Executable = $null,
[ValidateSet('Away', 'Display', 'System')]$Option = 'System'
)
$Code=@'
[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
'@
$ste = Add-Type -memberDefinition $Code -name System -namespace Win32 -passThru
# Requests that the other EXECUTION_STATE flags set remain in effect until
# SetThreadExecutionState is called again with the ES_CONTINUOUS flag set and
# one of the other EXECUTION_STATE flags cleared.
$ES_CONTINUOUS = [uint32]"0x80000000"
$ES_AWAYMODE_REQUIRED = [uint32]"0x00000040"
$ES_DISPLAY_REQUIRED = [uint32]"0x00000002"
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001"
Switch ($Option) {
"Away" {$Setting = $ES_AWAYMODE_REQUIRED}
"Display" {$Setting = $ES_DISPLAY_REQUIRED}
"System" {$Setting = $ES_SYSTEM_REQUIRED}
}
$ste::SetThreadExecutionState($ES_CONTINUOUS -bor $Setting)
The PAD action (as text; you can copy and paste the below directly into PAD):
@@copilotGeneratedAction: 'False'
Scripting.RunPowershellScript.RunPowershellScript Script: $'''# https://gist.github.com/CMCDragonkai/bf8e8b7553c48e4f65124bc6f41769eb
param (
[string]$Executable = $null,
[ValidateSet(\'Away\', \'Display\', \'System\')]$Option = \'System\'
)
$Code=@\'
[DllImport(\"kernel32.dll\", CharSet = CharSet.Auto,SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
\'@
$ste = Add-Type -memberDefinition $Code -name System -namespace Win32 -passThru
# Requests that the other EXECUTION_STATE flags set remain in effect until
# SetThreadExecutionState is called again with the ES_CONTINUOUS flag set and
# one of the other EXECUTION_STATE flags cleared.
$ES_CONTINUOUS = [uint32]\"0x80000000\"
$ES_AWAYMODE_REQUIRED = [uint32]\"0x00000040\"
$ES_DISPLAY_REQUIRED = [uint32]\"0x00000002\"
$ES_SYSTEM_REQUIRED = [uint32]\"0x00000001\"
Switch ($Option) {
\"Away\" {$Setting = $ES_AWAYMODE_REQUIRED}
\"Display\" {$Setting = $ES_DISPLAY_REQUIRED}
\"System\" {$Setting = $ES_SYSTEM_REQUIRED}
}
$ste::SetThreadExecutionState($ES_CONTINUOUS -bor $Setting)''' ScriptOutput=> PowershellOutput
This will set the power state of the computer to disallow it from going to sleep while the EXE running the automation is active. Once the EXE is no longer active, the sleep/hibernate prevention will automatically end. You'll most likely want this placed at the beginning of your automation.
(I tried to get PAD to run a disable command using code from the linked resource, and it worked OUTSIDE of PAD, but for some reason would not halt the sleep prevention from inside of PAD. Since it auto-stops when PAD completes its flow, it was not an issue in my case.)
To test, and/or see what state the machine is currently in, and if any machines refuse to go to sleep (and you're convinced it was caused by this [it shouldn't be]), you can put your mind at ease by checking using the following command from an administrative command prompt:
powercfg -requests
Happy automating!

Report
All responses (
Answers (