-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisable_standby.ps1
158 lines (101 loc) · 5.27 KB
/
disable_standby.ps1
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
Function Disable-Standby {
param (
[Parameter(Mandatory=$True)][string]$PathSettings,
[Parameter(Mandatory=$False)][switch]$Reinstate
)
$PathSettings = ($PathSettings + 'PowerPlan_Settings.xml')
if (!$Reinstate) {
# Timings are in seconds except for the Lid
# Lid: DoNothing (0), Sleep (1), Hibernate (2), ShutDown (3)
$Settings = @{
Categories = @{
Hibernate = 0, 'SUB_SLEEP', 'HIBERNATEIDLE';
Harddisk = 0, 'SUB_DISK', 'DISKIDLE';
Lid = 0, 'SUB_BUTTONS', 'LIDACTION';
Sleep = 0, 'SUB_SLEEP', 'STANDBYIDLE';
Display = 2, 'SUB_VIDEO', 'VIDEOIDLE'
}; Plans = @{}
}
foreach ( $Item in $Settings.Categories.GetEnumerator() | Where-Object { $_.Name -ne 'Lid' }) {
$Settings.Categories[$Item.Name][0] *= 60
}
# get active powerplan
$Settings.ActiveGUID = (PowerCFG -GETACTIVESCHEME).split(' ')[3]
# get powerplans from registry
$RegistryHive = [Microsoft.Win32.RegistryHive]::LocalMachine
$RegistryView = [Microsoft.Win32.RegistryView]::Default
$SubKey = 'SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\'
$RegistryPlans = [Microsoft.Win32.RegistryKey]::OpenBaseKey($RegistryHive, $RegistryView).
OpenSubKey($SubKey).
GetSubKeyNames()
foreach ($Plan in $RegistryPlans) {
$TempName = ([Microsoft.Win32.RegistryKey]::OpenBaseKey($RegistryHive, $RegistryView).
OpenSubKey(($SubKey + $Plan + '\')).
GetValue('FriendlyName')).
Split(',')[-1].
Split('(')[0].
Trim()
$Settings.Plans[$TempName] = @{ GUID = $Plan; Time = @{}}
}
# unhide lid settings
PowerCFG -ATTRIBUTES SUB_BUTTONS LIDACTION -ATTRIB_HIDE
if (Test-Path -Path $PathSettings) {
Write-Log -Text 'Existing settings file found!' -Color 'Black'
$Settings = Import-Clixml -Path $PathSettings
# check if settings are temporal value already or else set it
foreach ($Plan in $Settings.Plans.GetEnumerator()) {
foreach ($Property in $Settings.Categories.GetEnumerator()) {
$CheckSetting = PowerCFG -QUERY $Plan.Value.GUID $Property.Value[1] $Property.Value[2]
if (($CheckSetting[-3,-2] | ForEach-Object { [UInt32]($_ -split ': ')[1] }) -notcontains $Settings.Categories[$Property.Name][0]) {
foreach ($Iteration in $True, $False) {
$PowerType = if ($Iteration) { '-SETACVALUEINDEX' } else { '-SETDCVALUEINDEX' }
PowerCFG $PowerType $Plan.Value.GUID $Property.Value[1] $Property.Value[2] $Settings.Categories[$Property.Name][0]
}
}
}
PowerCFG -SETACTIVE $Plan.Value.GUID
}
PowerCFG -SETACTIVE $Settings.ActiveGUID
} else {
# grab existing settings and set temporal value
foreach ($Plan in $Settings.Plans.GetEnumerator()) {
foreach ($Property in $Settings.Categories.GetEnumerator()) {
$CheckSetting = PowerCFG -QUERY $Plan.Value.GUID $Property.Value[1] $Property.Value[2]
$Settings.Plans[$Plan.Name].Time[$Property.Name] = @(
[UInt32]($CheckSetting[-3] -split ': ')[1], [UInt32]($CheckSetting[-2] -split ': ')[1]
)
foreach ($Iteration in $True, $False) {
$PowerType = if ($Iteration) { '-SETACVALUEINDEX' } else { '-SETDCVALUEINDEX' }
PowerCFG $PowerType $Plan.Value.GUID $Property.Value[1] $Property.Value[2] $Settings.Categories[$Property.Name][0]
}
}
PowerCFG -SETACTIVE $Plan.Value.GUID
}
PowerCFG -SETACTIVE $Settings.ActiveGUID
# exporting settings
$Settings | Export-Clixml -Path $PathSettings
}
} else {
$Settings = Import-Clixml -Path $PathSettings
foreach ($Plan in $Settings.Plans.GetEnumerator()) {
foreach ($Property in $Settings.Categories.GetEnumerator()) {
foreach ($Index in 0, 1) {
$PowerType = if ($Index -eq 0) { '-SETACVALUEINDEX' } else { '-SETDCVALUEINDEX' }
PowerCFG $PowerType $Plan.Value.GUID $Property.Value[1] $Property.Value[2] $Settings.Plans[$Plan.Name].Time[$Property.Name][$Index]
}
}
PowerCFG -SETACTIVE $Plan.Value.GUID
}
PowerCFG -SETACTIVE $Settings.ActiveGUID
# delete settings file
if (Test-Path -Path $PathSettings) {
Remove-Item -Path $PathSettings
}
}
}
$PathSettings = 'C:\Folder\'
Disable-Standby -PathSettings $PathSettings
# do something
PAUSE
Disable-Standby -PathSettings $PathSettings -Reinstate