我是脚本方面的新手。我正在运行上面的脚本。我认为我必须采取并更改这行代码:
{param ($Plan = $(throw ‘Set-PowerPlan Ultimate Performance’ ))}
但我不确定,因为当我在 PS 中运行脚本时出现以下错误:
At C:\temp\Set-PowerPlan.ps1:35 char:67
+ ... RegEx = “(?<planguid>[A-Fa-f0-9]{8}-(?:[A-Fa-f0-9]{4}-){3}[A-Fa-f0- ...
+ ~
Array index expression is missing or not valid.
At C:\temp\Set-PowerPlan.ps1:41 char:36
+ $result = powercfg -s $matches[“PlanGUIDâ€] 2>&1
+ ~
Array index expression is missing or not valid.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : MissingArrayIndexExpression
我唯一更改的代码行就是上面的那行。
是的,“终极性能”电源方案已安装。
我还需要做其他事情吗?
我正在尝试将电源选项更改为“终极性能”,此选项已通过上一个命令安装。我正在运行的脚本:
param ($Plan = $(throw ‘Set-PowerPlan Ultimate Performance’ ))
Set-StrictMode -Version Latest
# Get the list of plans on the current machine.
$planList = powercfg.exe -l
# The regular expression to pull out the GUID for the specified plan.
$planRegEx = “(?<PlanGUID>[A-Fa-f0-9]{8}-(?:[A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12})” + (“(?:s+({0}))” -f $Plan)
# If the plan appears in the list…
if ( ($planList | Out-String) -match $planRegEx )
{
# Pull out the matching GUID and capture both stdout and stderr.
$result = powercfg -s $matches[“PlanGUID”] 2>&1
# If there were any problems, show the error.
if ( $LASTEXITCODE -ne 0)
{
$result
}
}
else
{
Write-Error (“The requested power scheme ‘{0}’ does not exist on this machine” -f $Plan)
}
答案1
我已经弄清楚了如何通过名称而不是 GUID 来更改 Power Schema。以下代码将把 Power Schema 更改为“Ultimate Performance”,但可以用来将其更改为任何常用名称
#Change to Ultimate Performance Power Schema
Get-CimInstance -N root\cimv2\power -Class win32_PowerPlan | select ElementName,
IsActive | ft -a
$p = gwmi -NS root\cimv2\power -Class win32_PowerPlan -Filter "ElementName ='Ultimate
Performance'"
$p.Activate()
Get-CimInstance -N root\cimv2\power -Class win32_PowerPlan | select ElementName,
IsActive | ft -a
pause
答案2
来自未来的更新,旨在帮助未来的探险者远离贬值,找到更快的替代方案。
$p = Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan -Filter “ElementName = ‘* Performance'”
(*(您的选择):高/终极等)
Invoke-CimMethod -InputObject $p -MethodName Activate
笔记值得一提的是,新的 CIM 接口速度惊人!而使用获取 WmiObject花了将近10秒钟才返回,调用 CimMethod电话立即回拨。