我正在尝试创建一个在电源计划之间切换的脚本 . 这是脚本:
$PP1 = Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerPlan |
where {$_.elementname -eq "Balanced"}
$PP2 = Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerPlan |
where {$_.elementname -eq "My Custom Plan 1"}
if ($PP1) {
$PP2.Activate()
}
if ($PP2) {
$PP1.Activate()
}
这是我得到的错误:
Exception calling "Activate" : "This method is not implemented in any class "
At line:6 char:1
+ $PP2.Activate()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WMIMethodException
Exception calling "Activate" : "This method is not implemented in any class "
At line:10 char:1
+ $PP1.Activate()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WMIMethodException
知道如何解决这个问题吗?
下面是执行 $PP1
后得到的输出 . 输出与 $PP2
完全相同:
__GENUS : 2
__CLASS : Win32_PowerPlan
__SUPERCLASS : CIM_SettingData
__DYNASTY : CIM_ManagedElement
__RELPATH : Win32_PowerPlan.InstanceID="Microsoft:PowerPlan\\{381b4222-f694-41f0-9685-ff5bb260df2e}"
__PROPERTY_COUNT : 5
__DERIVATION : {CIM_SettingData, CIM_ManagedElement}
__SERVER : PCLP2
__NAMESPACE : root\cimv2\power
__PATH : \\PCLP2\root\cimv2\power:Win32_PowerPlan.InstanceID="Microsoft:PowerPlan\\{381b4222-f694-41f0-9685-ff5bb260df2e
}"
Caption :
Description : Automatically balances performance with energy consumption on capable hardware.
ElementName : Balanced
InstanceID : Microsoft:PowerPlan\{381b4222-f694-41f0-9685-ff5bb260df2e}
IsActive : True
PSComputerName : PCLP2
我的目标是将脚本添加到任务计划程序,每次锁定计算机时,电源状态都会相应切换 .
2 回答
I think you have something to do with this code
good luck
我发现这个方法没有确切知道原因,所以我只是使用
powercfg
命令来处理这个,这是一个工作脚本 .由于我只有2个电源计划,因此这更简单,更简单,这就是为什么我有1个变量,它只能是PowerPlan A或PowerPlan B.
不确定如何使这个工作在任务计划程序,因为它为我打开一个记事本,如果我运行任务,虽然我可以使用
powercfg
命令使用bat文件,但只是好奇 . 任何方式使这项工作?