Disable Sleep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# ---------------------------------------------------
# Script: C:\Users\stefstr\Documents\GitHub\PowerShell\Examples\SuspendPowerPlan.ps1
# Version: 0.1
# Author: Stefan Stranger
# Date: 07/05/2014 15:01:57
# Description: Helper Function to Suspend Power Plan when running PowerShell scripts
# Comments:
# Disclamer: This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
# ---------------------------------------------------
<#
.Synopsis
Function to suspend your current Power Plan settings when running a PowerShell script.
.DESCRIPTION
Function to suspend your current Power Plan settings when running a PowerShell script.
Scenario: When downloading files using Robocopy from PowerShell you don't want your
laptop to go into sleep mode.
.EXAMPLE
Suspend-PowerPlan -script c:\scripts\mylongrunningscript.ps1
Run mylongrunningscript with sleep idle timeout prevented
.EXAMPLE
Suspend-Powerplan -script c:\scripts\mylongrunningscript.ps1 -option Display -Verbose
Run mylongrunningscript with Display idle timeout prevented and verbose messages
.LINK
http://www.microsofttranslator.com/bv.aspx?from=ru&to=en&a=http%3A%2F%2Fsocial.technet.microsoft.com%2FForums%2Fen-US%2F1f4754cb-37bf-4e1d-a59f-ec0f1aaf9d1c%2Fsetthreadexecutionstate-powershell%3FThread%3A1f4754cb-37bf-4e1d-a59f-ec0f1aaf9d1c%3DMicrosoft.Forums.Data.Models.Discussion%26ThreadViewModel%3A1f4754cb-37bf-4e1d-a59f-ec0f1aaf9d1c%3DMicrosoft.Forums.CachedViewModels.ThreadPageViewModel%26forum%3Dscrlangru
#>
function Suspend-Powerplan
{
[CmdletBinding()]
[Alias()]
[OutputType([int])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
Position=0)]
$script,
[ValidateSet("Away","Display","System")]
[string]$option
)
$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
$ES_CONTINUOUS = [uint32]"0x80000000" #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_AWAYMODE_REQUIRED = [uint32]"0x00000040" #Requests Away Mode to be enabled.
$ES_DISPLAY_REQUIRED = [uint32]"0x00000002" #Requests display availability (display idle timeout is prevented).
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001" #Requests system availability (sleep idle timeout is prevented).
Switch ($option)
{
"Away" {$setting = $ES_AWAYMODE_REQUIRED}
"Display" {$setting = $ES_DISPLAY_REQUIRED}
"System" {$setting = $ES_SYSTEM_REQUIRED}
Default {$setting = $ES_SYSTEM_REQUIRED}
}
Write-Host "Power Plan suspended with option: $option"
$ste::SetThreadExecutionState($ES_CONTINUOUS -bor $setting)
#do something
Write-Verbose "Executing $script"
&$script
Write-Verbose "Power Plan suspension ended"
$ste::SetThreadExecutionState($ES_CONTINUOUS)
}
Function Set-WindowSize {
Param([int]$x=$host.ui.rawui.windowsize.width,
[int]$y=$host.ui.rawui.windowsize.heigth)
$size=New-Object System.Management.Automation.Host.Size($x,$y)
$host.ui.rawui.WindowSize=$size
}
# __main__
Set-WindowSize 100 5
if ($MyInvocation.InvocationName -ne '.') {
Suspend-Powerplan {Read-Host 'press enter to terminate'} -option "Display"
}
Dieser Eintrag ist vom Autor unter
CC BY 4.0
lizensiert.