-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodexPowerMode.ps1
More file actions
293 lines (242 loc) · 10.3 KB
/
Copy pathCodexPowerMode.ps1
File metadata and controls
293 lines (242 loc) · 10.3 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
param(
[ValidateSet("on", "off", "status")]
[string]$Mode = "status"
)
$ErrorActionPreference = "Stop"
function Test-IsAdmin {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal]::new($identity)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Ensure-Admin {
if (Test-IsAdmin) {
return
}
$args = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Mode $Mode"
Write-Host "Requesting administrator permission..."
Start-Process -FilePath "powershell.exe" -ArgumentList $args -Verb RunAs -Wait -WindowStyle Normal
exit
}
function Set-PowerValue {
param(
[string]$Subgroup,
[string]$Setting,
[int]$Ac,
[int]$Dc
)
& powercfg /setacvalueindex SCHEME_CURRENT $Subgroup $Setting $Ac | Out-Null
& powercfg /setdcvalueindex SCHEME_CURRENT $Subgroup $Setting $Dc | Out-Null
}
function Set-Dword {
param(
[string]$Path,
[string]$Name,
[int]$Value
)
if (-not (Test-Path $Path)) {
New-Item -Path $Path -Force | Out-Null
}
New-ItemProperty -Path $Path -Name $Name -PropertyType DWord -Value $Value -Force | Out-Null
}
function Set-StringValue {
param(
[string]$Path,
[string]$Name,
[string]$Value
)
if (-not (Test-Path $Path)) {
New-Item -Path $Path -Force | Out-Null
}
New-ItemProperty -Path $Path -Name $Name -PropertyType String -Value $Value -Force | Out-Null
}
function Remove-ValueIfPresent {
param(
[string]$Path,
[string]$Name
)
Remove-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue
}
function Set-ServiceMode {
param(
[string]$Name,
[ValidateSet("Disabled", "Automatic")]
[string]$StartupType,
[bool]$ShouldRun
)
$service = Get-Service -Name $Name -ErrorAction SilentlyContinue
if (-not $service) {
return
}
if ($ShouldRun) {
Set-Service -Name $Name -StartupType $StartupType
Start-Service -Name $Name -ErrorAction SilentlyContinue
} else {
Stop-Service -Name $Name -Force -ErrorAction SilentlyContinue
Set-Service -Name $Name -StartupType $StartupType
}
}
$SUB_SLEEP = "238c9fa8-0aad-41ed-83f4-97be242c8f20"
$STANDBYIDLE = "29f6c1db-86da-48c5-9fdb-f2b67b1f44da"
$HYBRIDSLEEP = "94ac6d29-73ce-41a6-809f-6363ba21b47e"
$HIBERNATEIDLE = "9d7815a6-7ee4-497e-8888-515a05f02364"
$RTCWAKE = "bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d"
$UNATTENDSLEEP = "7bc4a2f9-d8fc-4469-b07b-33eb785aaca0"
$REMOTEFILESLEEP = "d4c1d4c8-d5cc-43d3-b83e-fc51215cb04d"
$SUB_VIDEO = "7516b95f-f776-4464-8c53-06167f40cc99"
$VIDEOIDLE = "3c0bc021-c8a8-4e07-a973-6b14cbcb2b7e"
$VIDEONORMALLEVEL = "aded5e82-b909-4619-9949-f5d71dac0bcb"
$SUB_DISK = "0012ee47-9041-4b5d-9b77-535fba8b1442"
$DISKIDLE = "6738e2c4-e8a5-4a42-b16a-e040e769756e"
$SUB_BUTTONS = "4f971e89-eebd-4455-a8de-9e59040e7347"
$LIDACTION = "5ca83367-6e45-459f-a27b-476b1d01c936"
$PBUTTONACTION = "7648efa3-dd9c-4e3e-b566-50f929386280"
$SBUTTONACTION = "96996bc0-ad50-47ec-923b-6f41874dd9eb"
$SUB_NONE = "fea3413e-7e05-4911-9a71-700331f1c294"
$CONSOLELOCK = "0e796bdb-100d-47d6-a2d5-f7d2daa51f51"
$DEVICEIDLE = "4faab71a-92e5-4726-b531-224559672d19"
$CONNECTIVITYINSTANDBY = "f15576e8-98b7-4186-b944-eafa664402d9"
$SUB_PCIEXPRESS = "501a4d13-42af-4429-9fd1-a8218c268e20"
$ASPM = "ee12f906-d277-404b-b6da-e5fa1a576df5"
$SUB_USB = "2a737441-1930-4402-8d77-b2bebba308a3"
$USBSELECTIVE = "48e6b7a6-50f5-4782-a5d4-53bb8f07e226"
$SUB_WIFI = "19cbb8fa-5279-450e-9fac-8a3d5fedd0c1"
$WIFI_POWER_SAVE = "12bbebe6-58d6-4636-95bb-3217ef867c1a"
$SUB_PRESENCE = "8619b916-e004-4dd8-9b66-dae86f806698"
$HUPRVIDEOIDLE = "0a7d6ab6-ac83-4ad1-8282-eca5b58308f3"
$USERPRESENCEPREDICTION = "82011705-fb95-4d46-8d35-4042b1d20def"
$HUPRVIDEODIMAWAY = "a79c8e0e-f271-482d-8f8a-5db9a18312de"
$HUPRVIDEODIM = "cf8c6097-12b8-4279-bbdd-44601ee5209d"
$HUPRVIDEOIDLEINATTENTIVE = "ee16691e-6ab3-4619-bb48-1c77c9357e5a"
$DesktopKey = "HKCU:\Control Panel\Desktop"
$DynamicLockKey = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"
$SystemPolicyKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
$WindowsUpdateAuKey = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
$LenovoPresenceServices = @(
"SmartSense",
"Lenovo Smart Communication Intelligent Sensing Service"
)
function Enable-CodexMode {
Ensure-Admin
# No sleep, hibernate, display-off, lid sleep, or device idle on AC or battery.
Set-PowerValue $SUB_SLEEP $STANDBYIDLE 0 0
Set-PowerValue $SUB_SLEEP $HIBERNATEIDLE 0 0
Set-PowerValue $SUB_SLEEP $HYBRIDSLEEP 0 0
Set-PowerValue $SUB_SLEEP $UNATTENDSLEEP 0 0
Set-PowerValue $SUB_SLEEP $REMOTEFILESLEEP 0 0
Set-PowerValue $SUB_SLEEP $RTCWAKE 0 0
Set-PowerValue $SUB_VIDEO $VIDEOIDLE 0 0
Set-PowerValue $SUB_VIDEO $VIDEONORMALLEVEL 15 15
Set-PowerValue $SUB_DISK $DISKIDLE 0 0
Set-PowerValue $SUB_BUTTONS $LIDACTION 0 0
Set-PowerValue $SUB_BUTTONS $PBUTTONACTION 4 4
Set-PowerValue $SUB_BUTTONS $SBUTTONACTION 0 0
Set-PowerValue $SUB_NONE $CONSOLELOCK 0 0
Set-PowerValue $SUB_NONE $DEVICEIDLE 0 0
Set-PowerValue $SUB_NONE $CONNECTIVITYINSTANDBY 1 1
Set-PowerValue $SUB_PCIEXPRESS $ASPM 0 0
Set-PowerValue $SUB_USB $USBSELECTIVE 0 0
Set-PowerValue $SUB_WIFI $WIFI_POWER_SAVE 0 0
Set-PowerValue $SUB_PRESENCE $HUPRVIDEOIDLE 0 0
Set-PowerValue $SUB_PRESENCE $USERPRESENCEPREDICTION 0 0
Set-PowerValue $SUB_PRESENCE $HUPRVIDEODIMAWAY 0 0
Set-PowerValue $SUB_PRESENCE $HUPRVIDEODIM 0 0
Set-PowerValue $SUB_PRESENCE $HUPRVIDEOIDLEINATTENTIVE 0 0
Set-Dword $DynamicLockKey "EnableGoodbye" 0
Set-StringValue $DesktopKey "ScreenSaveActive" "0"
Set-StringValue $DesktopKey "ScreenSaverIsSecure" "0"
Set-StringValue $DesktopKey "ScreenSaveTimeOut" "0"
Set-Dword $SystemPolicyKey "InactivityTimeoutSecs" 0
Set-Dword $WindowsUpdateAuKey "NoAutoRebootWithLoggedOnUsers" 1
foreach ($serviceName in $LenovoPresenceServices) {
Set-ServiceMode -Name $serviceName -StartupType Disabled -ShouldRun $false
}
Stop-Process -Name "UserSSCtrl" -Force -ErrorAction SilentlyContinue
& powercfg /requestsoverride PROCESS UserSSCtrl.exe DISPLAY | Out-Null
& powercfg /setactive SCHEME_CURRENT | Out-Null
Set-Dword $DynamicLockKey "EnableGoodbye" 0
Set-StringValue $DesktopKey "ScreenSaveActive" "0"
Set-StringValue $DesktopKey "ScreenSaverIsSecure" "0"
Set-StringValue $DesktopKey "ScreenSaveTimeOut" "0"
Set-Dword $SystemPolicyKey "InactivityTimeoutSecs" 0
Set-Dword $WindowsUpdateAuKey "NoAutoRebootWithLoggedOnUsers" 1
Write-Host "Codex overnight mode is ON."
Write-Host "Warning: battery sleep is also disabled in this mode."
}
function Disable-CodexMode {
Ensure-Admin
# Restore the normal-ish values observed before the overnight tuning.
Set-PowerValue $SUB_SLEEP $STANDBYIDLE 3600 1800
Set-PowerValue $SUB_SLEEP $HIBERNATEIDLE 10800 3600
Set-PowerValue $SUB_SLEEP $HYBRIDSLEEP 1 1
Set-PowerValue $SUB_SLEEP $UNATTENDSLEEP 120 120
Set-PowerValue $SUB_SLEEP $REMOTEFILESLEEP 1 1
Set-PowerValue $SUB_SLEEP $RTCWAKE 0 0
Set-PowerValue $SUB_VIDEO $VIDEOIDLE 900 1800
Set-PowerValue $SUB_VIDEO $VIDEONORMALLEVEL 50 50
Set-PowerValue $SUB_DISK $DISKIDLE 1200 600
Set-PowerValue $SUB_BUTTONS $LIDACTION 1 1
Set-PowerValue $SUB_BUTTONS $PBUTTONACTION 1 1
Set-PowerValue $SUB_BUTTONS $SBUTTONACTION 1 1
Set-PowerValue $SUB_NONE $CONSOLELOCK 1 1
Set-PowerValue $SUB_NONE $DEVICEIDLE 0 1
Set-PowerValue $SUB_NONE $CONNECTIVITYINSTANDBY 1 2
Set-PowerValue $SUB_PCIEXPRESS $ASPM 2 2
Set-PowerValue $SUB_USB $USBSELECTIVE 1 1
Set-PowerValue $SUB_WIFI $WIFI_POWER_SAVE 0 2
Set-PowerValue $SUB_PRESENCE $HUPRVIDEOIDLE 30 30
Set-PowerValue $SUB_PRESENCE $USERPRESENCEPREDICTION 0 1
Set-PowerValue $SUB_PRESENCE $HUPRVIDEODIMAWAY 0 0
Set-PowerValue $SUB_PRESENCE $HUPRVIDEODIM 5 5
Set-PowerValue $SUB_PRESENCE $HUPRVIDEOIDLEINATTENTIVE 0 0
Set-Dword $DynamicLockKey "EnableGoodbye" 1
Set-StringValue $DesktopKey "ScreenSaveActive" "1"
Remove-ValueIfPresent $DesktopKey "ScreenSaverIsSecure"
Remove-ValueIfPresent $DesktopKey "ScreenSaveTimeOut"
Remove-ValueIfPresent $SystemPolicyKey "InactivityTimeoutSecs"
Remove-ValueIfPresent $WindowsUpdateAuKey "NoAutoRebootWithLoggedOnUsers"
foreach ($serviceName in $LenovoPresenceServices) {
Set-ServiceMode -Name $serviceName -StartupType Automatic -ShouldRun $true
}
& powercfg /requestsoverride PROCESS UserSSCtrl.exe | Out-Null
& powercfg /setactive SCHEME_CURRENT | Out-Null
Set-Dword $DynamicLockKey "EnableGoodbye" 1
Set-StringValue $DesktopKey "ScreenSaveActive" "1"
Remove-ValueIfPresent $DesktopKey "ScreenSaverIsSecure"
Remove-ValueIfPresent $DesktopKey "ScreenSaveTimeOut"
Remove-ValueIfPresent $SystemPolicyKey "InactivityTimeoutSecs"
Remove-ValueIfPresent $WindowsUpdateAuKey "NoAutoRebootWithLoggedOnUsers"
Write-Host "Normal mode is restored."
Write-Host "Hibernate is restored: AC after 3 hours, battery after 1 hour."
Write-Host "Note: normal mode does not close third-party apps; check power requests if sleep is still blocked."
}
function Show-Status {
Write-Host "Administrator: $(Test-IsAdmin)"
Write-Host ""
Write-Host "Power requests:"
& powercfg /requests
Write-Host ""
Write-Host "Request overrides:"
& powercfg /requestsoverride
Write-Host ""
Write-Host "Lenovo presence services:"
Get-CimInstance Win32_Service |
Where-Object { $_.Name -in $LenovoPresenceServices } |
Select-Object Name, State, StartMode |
Format-Table -AutoSize
Write-Host ""
Write-Host "Lock-related registry values:"
Get-ItemProperty $DynamicLockKey -ErrorAction SilentlyContinue |
Select-Object EnableGoodbye |
Format-List
Get-ItemProperty $DesktopKey -ErrorAction SilentlyContinue |
Select-Object ScreenSaveActive, ScreenSaverIsSecure, ScreenSaveTimeOut |
Format-List
Get-ItemProperty $SystemPolicyKey -ErrorAction SilentlyContinue |
Select-Object InactivityTimeoutSecs |
Format-List
}
switch ($Mode) {
"on" { Enable-CodexMode }
"off" { Disable-CodexMode }
"status" { Show-Status }
}