I'm trying to write a powershell script to run a few tasks in order to test the batteries on a wide range of tablet PCs.
A big part of this is controlling the display. I don't seem to have too much trouble turning it off, but when I come to turn it back on again, it only seems to work on some machines.
I have found this very helpful code:
#Load the Windows API from a specified dll - here Kernel32.dll
$function=@'
[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
'@
$method = Add-Type -MemberDefinition $function -name System -namespace Win32 -passThru
#Specify the flags to use them later
$ES_CONTINUOUS = [uint32]'0x80000000'
$ES_AWAYMODE_REQUIRED = [uint32]'0x00000040'
$ES_DISPLAY_REQUIRED = [uint32]'0x00000002'
$ES_SYSTEM_REQUIRED = [uint32]'0x00000001'
$method::SetThreadExecutionState($ES_DISPLAY_REQUIRED)
This will work fine on about 60% of the machines I try, but not the rest...
Is there something I'm missing?
Thanks in advance
User contributions licensed under CC BY-SA 3.0