如何使用Powershell在Windows 10中更改电源计划,并在长脚本后恢复到原始设置



如何制作PowerShell脚本来检查当前电源计划,并将其更改为"高性能";(如果还没有(,然后运行一个长的PowerShell脚本,然后在脚本之后,切换回原始电源计划?

我想出了这样的办法,但感觉-和语句不起作用,或者我做错了什么?

此时此刻我得到了以下信息:

$PowerSettingsorg = Get-WmiObject -Namespace rootcimv2power -Class win32_powerplan
$PowerSettings = Get-WmiObject -Namespace rootcimv2power -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 rootcimv2power -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 rootcimv2power -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.!"
}

在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.!"
}

所以这是我的修复,因为.Activate()方法在新版本的PS:上神秘地消失了

使用激活方法和CIM的旧方法

$p = Get-CimInstance -Name rootcimv2power -Class win32_PowerPlan -Filter "ElementName = 'High Performance'"      
Invoke-CimMethod -InputObject $p -MethodName Activate

使用powercfg设置所选计划的新方法

$p = Get-CimInstance -Name rootcimv2power -Class win32_PowerPlan -Filter "ElementName = 'High Performance'"      
powercfg /setactive ([string]$p.InstanceID).Replace("Microsoft:PowerPlan{","").Replace("}","")

$PowerSettings变量包含所有三个电源计划的设置。您需要测试哪个计划处于活动状态。

$PowerSettings = Get-WmiObject -Namespace rootcimv2power -Class win32_powerplan
($PowerSettings | Where-Object { $_.IsActive }).ElementName

相关内容

  • 没有找到相关文章

最新更新