使用脚本配置windows电源选项

我们在日常运维工作中,有的时候需要批量设置生产电脑的电源设置,比如设置五分钟锁屏,永不睡眠等,常规设置起来比较麻烦,我们可以使用脚本一键设置

1.首先我们要配置一个电源配置文件

设置好我们要达到的效果,我这里设置永不睡眠

2.我们使用powerlist导出电源方案

首先在cmd中输入命令powercfg -list,找到我们要导出的电源方案,并复制对应的GUID

3. 输入命令powercfg -export 盘符:备份电源文件名.pow 电源方案名称,比如我们要备份电源正在使用的电源方案,可以输入命令powercfg -export d:powercfg.pow abe48c28-5947-4562-a8e3-08a578e7ef2a,就是备份正在使用的电源方案,把其备份到d:\power.pow文件中。

这样我们就在 D盘中看到导出的方案了

4.我们写一个powershell脚本自动导入配置文件并激活

#提取administrator权限
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
#导入电源计划并激活计划
$dir = Split-Path -Parent $MyInvocation.MyCommand.Definition
#cd $dir
powercfg /import $dir\power.pow
$UPPlan = gwmi -NS root\cimv2\power -Class win32_PowerPlan -Filter "ElementName ='leaf-shuidi'"

if ($UPPlan) {

    $regex = [regex]"{(.*?)}$"

    $UPPlanGuid = $regex.Match($UPPlan.InstanceID.Tostring()).Groups[1].value

#Add-Content -Path C:\log.log -Value $UPPlanGuid
powercfg -setactive $UPPlanGuid
#弹窗提示修改成功
function Read-MessageBoxDialog

{

$PopUpWin = new-object -comobject wscript.shell

$PopUpWin.popup("不息屏设置成功")

}

Read-MessageBoxDialog
}

这样,我们只需要把写好的ps1文件和pow文件放到一起,运行ps1脚本就可以倒入并激活了

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值