如何制作一个 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.!"
}
在 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 上神秘地消失了:
$p = Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan -Filter "ElementName = 'High Performance'"
Invoke-CimMethod -InputObject $p -MethodName Activate
$p = Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan -Filter "ElementName = 'High Performance'"
powercfg /setactive ([string]$p.InstanceID).Replace("Microsoft:PowerPlan\{","").Replace("}","")
您的 $PowerSettings 变量包含所有三个电源计划的设置。您需要测试看看哪个计划处于活动状态。
$PowerSettings = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan
($PowerSettings | Where-Object { $_.IsActive }).ElementName