如何使用 Powershell 更改 Windows 10 中的电源计划并在长脚本后恢复为原始设置?
Posted
技术标签:
【中文标题】如何使用 Powershell 更改 Windows 10 中的电源计划并在长脚本后恢复为原始设置?【英文标题】:How to change Power Plan in Windows 10 with Powershell and revert after long script to original settings? 【发布时间】:2020-04-08 16:27:17 【问题描述】:如何制作一个 PowerShell 脚本来检查当前的电源计划,将其更改为“高性能”(如果还没有的话),然后运行一个长的 PowerShell 脚本,然后在脚本之后,切换回原来的电源计划?
我想出了类似的方法,但感觉 -and 语句不起作用,还是我做错了什么?
此时我得到以下信息:
$PowerSettingsorg = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan
$PowerSettings = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan
If ($PowerSettings.IsActive -eq $True -And $PowerSettings.ElementName -eq 'Hoge prestaties')
write-host "++ Power Plan Settings are correct.!"
Else
$hpPlan = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan | Where-Object $_.ElementName -eq 'Hoge prestaties'
$hpPlan.Activate()
write-host "++ Power plan Settings are changed to High Performance.!"
<Long Script here>
If ($PowerSettingsorg.IsActive -eq 'true' -and $PowerSettingsorg.ElementName -eq 'Gebalanceerd')
$orgPlan = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan | Where-Object $_.ElementName -eq 'Gebalanceerd'
$orgPlan.Activate()
write-host "++ Power plan Settings have been reverted to Belanced.!"
Else
write-host "++ No Power Plan settings have been reverted.!"
【问题讨论】:
尝试将 AND 比较器括起来:($PowerSettings.IsActive -And ($PowerSettings.ElementName -eq 'Hoge prestaties') 它一直说它是 $True,而我的 Powerplan 处于平衡状态:如果 ($PowerSettings.IsActive -And ($PowerSettings.ElementName -eq 'Hoge prestaties')) 我希望这是错误的:// 对我来说很好。您确定 ElementName 使用的是区域语言吗?如果输入 $PowerSettings.ElementName 会得到什么? - Gebalanceerd - Hoge prestaties - Energiebesparing 对我来说似乎是正确的? 【参考方案1】:在 Windows 10 中,COM 对象上的 Activate()
方法不再起作用,并显示错误消息 Exception calling "Activate" : "This method is not implemented in any class "
。
要操作电源方案设置,您必须使用PowerCfg.exe
:
# fill a hashtable with power scheme guids and alias names:
# Name Value
# ----- -----
# 381b4222-f694-41f0-9685-ff5bb260df2e SCHEME_BALANCED # --> Balanced
# 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c SCHEME_MIN # --> High performance
# a1841308-3541-4fab-bc81-f71556f20b4a SCHEME_MAX # --> Power saver
$powerConstants = @
PowerCfg.exe -ALIASES | Where-Object $_ -match 'SCHEME_' | ForEach-Object
$guid,$alias = ($_ -split '\s+', 2).Trim()
$powerConstants[$guid] = $alias
# get a list of power schemes
$powerSchemes = PowerCfg.exe -LIST | Where-Object $_ -match '^Power Scheme' | ForEach-Object
$guid = $_ -replace '.*GUID:\s*([-a-f0-9]+).*', '$1'
[PsCustomObject]@
Name = $_.Trim("* ") -replace '.*\(([^)]+)\)$', '$1' # LOCALIZED !
Alias = $powerConstants[$guid]
Guid = $guid
IsActive = $_ -match '\*$'
# set a variable for the desired power scheme (in this case High performance)
$desiredScheme = $powerSchemes | Where-Object $_.Alias -eq 'SCHEME_MIN'
# get the currently active scheme
$PowerSettingsorg = $powerSchemes | Where-Object $_.IsActive
if ($PowerSettingsorg.Alias -eq $desiredScheme.Alias)
# or by guid: if ($PowerSettingsorg.Guid -eq $desiredScheme.Guid)
# or localized: if ($PowerSettingsorg.Name -eq $desiredScheme.Name)
# or: if ($desiredScheme.IsActive)
Write-Host "++ Power Plan Settings are correct.! ($($PowerSettingsorg.Name))"
else
# set powersettings to High Performance
Powercfg.exe -SETACTIVE $desiredScheme.Alias # you can also set this using the $desiredScheme.Guid
# test if the setting has changed
$currentPowerGuid = (Powercfg.exe -GETACTIVESCHEME) -replace '.*GUID:\s*([-a-f0-9]+).*', '$1'
if ($currentPowerGuid -eq $desiredScheme.Guid)
Write-Host "++ Power plan Settings have changed to $($desiredScheme.Name).!"
else
# exit the script here???
Throw "++ Power plan Settings did not change to $($desiredScheme.Name).!"
### <Long Script here> ###
# get the current power scheme and test if it needs to be reverted
if ($currentPowerGuid -ne $PowerSettingsorg.Guid)
# set powersettings to the original scheme
Powercfg.exe -SETACTIVE $PowerSettingsorg.Alias # you can also set this using the $PowerSettingsorg.Guid
# test if the setting has changed
$currentPowerGuid = (Powercfg.exe -GETACTIVESCHEME) -replace '.*GUID:\s*([-a-f0-9]+).*', '$1'
if ($currentPowerGuid -eq $PowerSettingsorg.Guid)
Write-Host "++ Power plan Settings have been reverted to $($PowerSettingsorg.Name).!"
else
# exit the script here???
Throw "++ Power plan Settings did not revert to $($PowerSettingsorg.Name).!"
else
Write-Host "++ Power Plan settings did not need to be reverted.!"
【讨论】:
感谢朋友的评论。激活工作得很好:) 用上面的回复解决了。非常感谢! @DennisActivate()
在我的机器上不起作用(Windows 10 Pro 1909 build 18363.720)并给了我"This method is not implemented in any class "
错误。【参考方案2】:
所以这是我的解决方法,因为 .Activate()
方法在新版本的 PS 上神秘地消失了:
使用激活方法和 CIM 的旧方法
$p = Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan -Filter "ElementName = 'High Performance'"
Invoke-CimMethod -InputObject $p -MethodName Activate
使用 powercfg 现在设置所选计划的新方法
$p = Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan -Filter "ElementName = 'High Performance'"
powercfg /setactive ([string]$p.InstanceID).Replace("Microsoft:PowerPlan\","").Replace("","")
【讨论】:
【参考方案3】:您的 $PowerSettings 变量包含所有三个电源计划的设置。您需要测试以查看哪个计划处于活动状态。
$PowerSettings = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan
($PowerSettings | Where-Object $_.IsActive ).ElementName
【讨论】:
我在搜索对象时所做的事情:IsActive $true。 ($PowerSettings.IsActive -eq $true -And ($PowerSettings.ElementName -eq 'Hoge prestaties')) 然而,虽然平衡的 powerscheme 处于活动状态,但它给我的结果是真实的。从什么时候开始高性能与主动方案中的平衡相同? 你是对的!我编辑了支票,现在一切正常!以上是关于如何使用 Powershell 更改 Windows 10 中的电源计划并在长脚本后恢复为原始设置?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 powershell 从 Win32_DCOMApplicationSetting 类更改 AuthenticationLevel 属性?