使用PowerShell更改电源计划

时间:2018-10-18 10:32:18

标签: powershell powershell-v5.0 powershell-ise

我正在尝试创建一个在电源计划之间切换的脚本。这是脚本:

$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 个答案:

答案 0 :(得分:0)

我认为您与此代码有关

$PP1 = Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerPlan -Filter "ElementName ='Balanced'"
$PP2 = Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerPlan -Filter "ElementName = 'Power saver'"
if ($PP1.IsActive -eq "true"){
write-host Balanced
$PP2.Activate()
}
else{
Write-Host Power saver
}

祝你好运

答案 1 :(得分:0)

我发现由于某种原因(我不知道确切的原因),该方法在Windows 10上不起作用,所以我只是使用export class HomePage { isLogin: boolean = false; isSignUp: boolean = false; loginForm: FormGroup; constructor(public navCtrl: NavController, private fb: FormBuilder, private alertCtrl: AlertController) { } ngOnInit() { this.loginForm = this.fb.group({ username: '', password: '' }); } login() { this.isLogin = true; this.isSignUp = false; } async loginSubmit() { if (this.loginForm.value.username == "abc" && this.loginForm.value.password == "abc1") { this.navCtrl.push(LoginSuccessPage, { username: this.loginForm.value.username }); } else { let alert = this.alertCtrl.create({ title: 'Login Failed', buttons: [{ text: 'Ok', handler: () => { this.isLogin = false; alert.dismiss(); this.loginForm.reset(); return false; } }] }); alert.present(); } } } 命令来工作,这是一个有效的脚本。 / p>

powercfg

由于我只有2个电源计划,因此这变得越来越容易,这就是为什么我只有1个变量,并且只能是PowerPlan A或PowerPlan B的原因。

不知道如何在Task Scheduler上进行这项工作,因为如果我运行该任务,它将为我打开一个记事本,尽管我可以通过$PP1 = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan | where {$_.elementname -eq "Balanced"} if ($PP1.IsActive -eq "False") { powercfg /setactive 47f02214-4297-464e-8b31-f95e26d28082 } Else { powercfg /setactive 381b4222-f694-41f0-9685-ff5bb260df2e } 命令使用bat文件来完成这项工作,但只是出于好奇。 有什么办法可以做到这一点?