我们在日常运维工作中,有的时候需要批量设置生产电脑的电源设置,比如设置五分钟锁屏,永不睡眠等,常规设置起来比较麻烦,我们可以使用脚本一键设置
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脚本就可以倒入并激活了