Turning off energy-saving mechanisms

Customers frequently ask how to disable the energy-saving technologies for a specific time frame to avoid installation interruptions. In most scenarios, the engineers solved this by setting the energy savings to high performance. Unfortunately, the next time the GPOs are applied, this modification will be reverted. A very handy solution is to use the SetThreadExecutionState function on your own. 

SetThreadExecutionState function 

This function enables an application to inform the system that it is in use, thereby preventing the system from entering sleep mode or turning off the display while the application is running. 
For more information, please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx.

In the following example, you can see how a Windows API is being called directly from PowerShell. Open the informational link for the SetThreadExecutionState function as well to understand how you could achieve this for every other documented Windows API:

#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'

#Configuring the system to ignore any energy saving technologies
$method::SetThreadExecutionState($ES_SYSTEM_REQUIRED -bor $ES_DISPLAY_REQUIRED -bor $ES_CONTINUOUS)

#Executing an installation e.g.
& msiexec.exe /i \ShareImportantInstallation.msi /qb

#Restoring saving mechanisms
$method::SetThreadExecutionState($ES_CONTINUOUS)

First, we import the specific DLL and function. Afterwards, the flags are specified. It makes it easier to use them as a variable in scripts to avoid any errors. You can call the imported function now with $method:: like a static class. After you have set the ThreadExecutionState to prevent sleep mechanisms, an installation can easily be started. Don't forget to revert the configuration after the installation has finished. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.107.241