Task: Set-PowerPlan (official)
What:
- Add Power Button Action
- Add Sleep Button Action
Why: End users bump the power/sleep buttons, causing machine to go offline/unreachable in Immybot. Want Immybot to have ability to manage this setting similar to other power settings.
CC:
- @DimitriRodis (original author?)
- @DarrenDK : Any other user should @ mention for “PR”? Is there better way to subnit this?
Diff: PowerOptionsModule-Old.ps1<->PowerOptionsModule-New.ps1
--- "1/.\\PowerOptionsModule-Old.ps1"
+++ "2/.\\PowerOptionsModule-New.ps1"
@@ -440,6 +440,38 @@ function Set-PowerSchemeSettings {
SubGUID = "4f971e89-eebd-4455-a8de-9e59040e7347"
SettingGUID = "5ca83367-6e45-459f-a27b-476b1d01c936"
}
+ PowerButtonActionAC = @{
+ Description = "Power Button action on AC"
+ RegistrySubPath = "\4f971e89-eebd-4455-a8de-9e59040e7347\7648efa3-dd9c-4e3e-b566-50f929386280"
+ SwitchCommand = "/setacvalueindex"
+ NeedsSchemeGUID = $true
+ SubGUID = "4f971e89-eebd-4455-a8de-9e59040e7347"
+ SettingGUID = "7648efa3-dd9c-4e3e-b566-50f929386280"
+ }
+ PowerButtonActionDC = @{
+ Description = "Power Button action on DC"
+ RegistrySubPath = "\4f971e89-eebd-4455-a8de-9e59040e7347\7648efa3-dd9c-4e3e-b566-50f929386280"
+ SwitchCommand = "/setdcvalueindex"
+ NeedsSchemeGUID = $true
+ SubGUID = "4f971e89-eebd-4455-a8de-9e59040e7347"
+ SettingGUID = "7648efa3-dd9c-4e3e-b566-50f929386280"
+ }
+ SleepButtonActionAC = @{
+ Description = "Sleep Button action on AC"
+ RegistrySubPath = "\4f971e89-eebd-4455-a8de-9e59040e7347\96996bc0-ad50-47ec-923b-6f41874dd9eb"
+ SwitchCommand = "/setacvalueindex"
+ NeedsSchemeGUID = $true
+ SubGUID = "4f971e89-eebd-4455-a8de-9e59040e7347"
+ SettingGUID = "96996bc0-ad50-47ec-923b-6f41874dd9eb"
+ }
+ SleepButtonActionDC = @{
+ Description = "Sleep Button action on DC"
+ RegistrySubPath = "\4f971e89-eebd-4455-a8de-9e59040e7347\96996bc0-ad50-47ec-923b-6f41874dd9eb"
+ SwitchCommand = "/setdcvalueindex"
+ NeedsSchemeGUID = $true
+ SubGUID = "4f971e89-eebd-4455-a8de-9e59040e7347"
+ SettingGUID = "96996bc0-ad50-47ec-923b-6f41874dd9eb"
+ }
USBSelectiveSuspendAC = @{
Description = "USB Selective Suspend on AC"
RegistrySubPath = "\2a737441-1930-4402-8d77-b2bebba308a3\48e6b7a6-50f5-4782-a5d4-53bb8f07e226"
@@ -527,7 +559,7 @@ function Set-PowerSchemeSettings {
# Special case handling for the following settings
switch -Regex ($SettingName) {
- "LidCloseActionAC|LidCloseActionDC" {
+ "LidCloseActionAC|LidCloseActionDC|PowerButtonActionAC|PowerButtonActionDC|SleepButtonActionAC|SleepButtonActionDC" {
$Index = switch ($Value) {
'Do Nothing' { 0 }
'Sleep' { 1 }
Diff: Set-PowerPlan (Official)-Old.ps1<->Set-PowerPlan (Official)-New.ps1
--- "1/.\\Set-PowerPlan (Official)-Old.ps1"
+++ "2/.\\Set-PowerPlan (Official)-New.ps1"
@@ -126,8 +126,11 @@ function Set-PowerScheme {
}
}
-Function Get-LidCloseIndexFromValue
+Function Get-ActionIndexFromValue
{
+ #Lid close Ref: https://learn.microsoft.com/en-us/windows-hardware/customize/power-settings/power-button-and-lid-settings-lid-switch-close-action
+ #Power Button Ref: https://learn.microsoft.com/en-us/windows-hardware/customize/power-settings/power-button-and-lid-settings-power-button-action
+ #Sleep Button Ref: https://learn.microsoft.com/en-us/windows-hardware/customize/power-settings/power-button-and-lid-settings-sleep-button-action
param([string]$Value)
switch($Value)
{
@@ -205,6 +208,10 @@ function Set-PowerSchemeSettings {
[string]$HibernateTimeoutDC,
[string]$LidCloseActionAC,
[string]$LidCloseActionDC,
+ [string]$PowerButtonActionAC,
+ [string]$PowerButtonActionDC,
+ [string]$SleepButtonActionAC,
+ [string]$SleepButtonActionDC,
$USBSelectiveSuspendAC,
$USBSelectiveSuspendDC,
$PasswordProtectWakeupAC,
@@ -294,7 +301,7 @@ function Set-PowerSchemeSettings {
}
}
if ($LidCloseActionAC) {
- $Index = Get-LidCloseIndexFromValue $LidCloseActionAC
+ $Index = Get-ActionIndexFromValue $LidCloseActionAC
$Description = "Lid close action on AC....."
$RegArray += @{
Description = $Description
@@ -305,7 +312,7 @@ function Set-PowerSchemeSettings {
}
}
if ($LidCloseActionDC) {
- $Index = Get-LidCloseIndexFromValue $LidCloseActionDC
+ $Index = Get-ActionIndexFromValue $LidCloseActionDC
$Description = "Lid close action on DC....."
$RegArray += @{
Description = $Description
@@ -316,6 +323,52 @@ function Set-PowerSchemeSettings {
}
}
+ if ($PowerButtonActionAC) {
+ $Index = Get-ActionIndexFromValue $PowerButtonActionAC
+ $Description = "Power Button action on AC....."
+ $RegArray += @{
+ Description = $Description
+ Switches = "/setacvalueindex $($Scheme.GUID) 4f971e89-eebd-4455-a8de-9e59040e7347 7648efa3-dd9c-4e3e-b566-50f929386280 $Index"
+ Path = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\4f971e89-eebd-4455-a8de-9e59040e7347\7648efa3-dd9c-4e3e-b566-50f929386280"
+ Value = $Index
+ PowerIndex = "ACSettingIndex"
+ }
+ }
+ if ($PowerButtonActionDC) {
+ $Index = Get-ActionIndexFromValue $PowerButtonActionDC
+ $Description = "Power Button action on DC....."
+ $RegArray += @{
+ Description = $Description
+ Switches = "/setdcvalueindex $($Scheme.GUID) 4f971e89-eebd-4455-a8de-9e59040e7347 7648efa3-dd9c-4e3e-b566-50f929386280 $Index"
+ Path = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\4f971e89-eebd-4455-a8de-9e59040e7347\7648efa3-dd9c-4e3e-b566-50f929386280"
+ Value = $Index
+ PowerIndex = "DCSettingIndex"
+ }
+ }
+
+ if ($SleepButtonActionAC) {
+ $Index = Get-ActionIndexFromValue $SleepButtonActionAC
+ $Description = "Sleep Button action on AC....."
+ $RegArray += @{
+ Description = $Description
+ Switches = "/setacvalueindex $($Scheme.GUID) 4f971e89-eebd-4455-a8de-9e59040e7347 96996bc0-ad50-47ec-923b-6f41874dd9eb $Index"
+ Path = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\4f971e89-eebd-4455-a8de-9e59040e7347\96996bc0-ad50-47ec-923b-6f41874dd9eb"
+ Value = $Index
+ PowerIndex = "ACSettingIndex"
+ }
+ }
+ if ($SleepButtonActionDC) {
+ $Index = Get-ActionIndexFromValue $SleepButtonActionDC
+ $Description = "Sleep Button action on DC....."
+ $RegArray += @{
+ Description = $Description
+ Switches = "/setdcvalueindex $($Scheme.GUID) 4f971e89-eebd-4455-a8de-9e59040e7347 96996bc0-ad50-47ec-923b-6f41874dd9eb $Index"
+ Path = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\4f971e89-eebd-4455-a8de-9e59040e7347\96996bc0-ad50-47ec-923b-6f41874dd9eb"
+ Value = $Index
+ PowerIndex = "DCSettingIndex"
+ }
+ }
+
if ($null -ne $USBSelectiveSuspendAC) {
$Index = [int][bool]$USBSelectiveSuspendAC
$Description = "USB Selective Suspend on AC....."
@@ -381,7 +434,7 @@ function Set-PowerSchemeSettings {
$RegValue = Get-ItemProperty $Setting.Path -Name $Setting.PowerIndex -ErrorAction SilentlyContinue | %{$_."$($Setting.PowerIndex)"}
Write-Host "RegValue: $RegValue"
# if(!(!([string]::IsNullOrEmpty($USBSelectiveSuspendDC) -or ![string]::IsNullOrEmpty($LidCloseActionAC))))
- if($Setting.Description -notmatch "PasswordProtectWakeup|Lid close action|USB Selective Suspend")
+ if($Setting.Description -notmatch "PasswordProtectWakeup|Lid close action|Power Button action|Sleep Button action|USB Selective Suspend")
{
$RegValue /= 60
}
@@ -431,6 +484,10 @@ $TestResult = (Set-PowerSchemeSettings -Validate -HibernateTimeoutAC $HibernateT
$TestResult = (Set-PowerSchemeSettings -Validate -HibernateTimeoutDC $HibernateTimeoutDC) -and $TestResult
$TestResult = (Set-PowerSchemeSettings -Validate -LidCloseActionAC $LidCloseActionAC) -and $TestResult
$TestResult = (Set-PowerSchemeSettings -Validate -LidCloseActionDC $LidCloseActionDC) -and $TestResult
+$TestResult = (Set-PowerSchemeSettings -Validate -PowerButtonActionAC $PowerButtonActionAC) -and $TestResult
+$TestResult = (Set-PowerSchemeSettings -Validate -PowerButtonActionDC $PowerButtonActionDC) -and $TestResult
+$TestResult = (Set-PowerSchemeSettings -Validate -SleepButtonActionAC $SleepButtonActionAC) -and $TestResult
+$TestResult = (Set-PowerSchemeSettings -Validate -SleepButtonActionDC $SleepButtonActionDC) -and $TestResult
$TestResult = (Set-PowerSchemeSettings -Validate -USBSelectiveSuspendAC $USBSelectiveSuspendAC) -and $TestResult
$TestResult = (Set-PowerSchemeSettings -Validate -USBSelectiveSuspendDC $USBSelectiveSuspendDC) -and $TestResult
$TestResult = (Set-PowerSchemeSettings -Validate -PasswordProtectWakeupAC $PasswordProtectWakeupAC) -and $TestResult
@@ -483,6 +540,10 @@ $TestResult = (Set-PowerSchemeSettings -Validate -PasswordProtectWakeupDC $Passw
Set-PowerSchemeSettings -Config -HibernateTimeoutDC $HibernateTimeoutDC
Set-PowerSchemeSettings -Config -LidCloseActionAC $LidCloseActionAC
Set-PowerSchemeSettings -Config -LidCloseActionDC $LidCloseActionDC
+ Set-PowerSchemeSettings -Config -PowerButtonActionAC $PowerButtonActionAC
+ Set-PowerSchemeSettings -Config -PowerButtonActionDC $PowerButtonActionDC
+ Set-PowerSchemeSettings -Config -SleepButtonActionAC $SleepButtonActionAC
+ Set-PowerSchemeSettings -Config -SleepButtonActionDC $SleepButtonActionDC
Set-PowerSchemeSettings -Config -USBSelectiveSuspendAC $USBSelectiveSuspendAC
Set-PowerSchemeSettings -Config -USBSelectiveSuspendDC $USBSelectiveSuspendDC
Set-PowerSchemeSettings -Config -PasswordProtectWakeupAC $PasswordProtectWakeupAC
Updated files in next post(s) [due to character limit of posts and can’t attach .ps1
or .txt
]