zoukankan      html  css  js  c++  java
  • PowerShell2.0之桌面计算机维护(六)修改电源计划

    Windows Vista和Windows Server 2008的电源计划有大量改进,可以针对使用电池或交流电供电的情况分别设置。如果当前计算机正在使用电池,那么续航时间是个需要关心的问题。而在某些情况下,计算机的性能才是最重要的。例如,如果电力会在几分钟之后恢复,则不必为延长电池使用时间而降低性能。在不同的电源计划下显示器、磁盘及CPU的电力消耗也不同,在Windows Server 2008系统中可以创建自定义的电源计划,如图1所示。

    image

    图1 创建自定义的电源计划

    创建名为“SetPowerConfig.ps1”的脚本用于设置电源计划,代码如下:

    param($c, $t, $q, $help)

    function funline ($strIN)

    {

    $num = $strIN.length

    for($i=1 ; $i -le $num ; $i++)

    { $funline += "=" }

    Write-Host -ForegroundColor yellow `n$strIN

    Write-Host -ForegroundColor darkYellow $funline

    }

    function funHelp()

    {

    $helpText=@"

    DESCRIPTION:

    NAME: SetPowerConfig.ps1

    Sets power config on a local machine.

    PARAMETERS:

    -c(hange) <mp,mb,dp,db,sp,sb,hp,hb>

    -q(uery) detailed query of current power plan

    -t(ime out) time out value for change. Required when using -c to change a value

    -help prints help file

    SYNTAX:

    SetPowerConfig.ps1

    Displays error message. Must supply a parameter

    SetPowerConfig.ps1 -c mp -t 10

    Sets time out value of monitor when on power to10 minutes

    SetPowerConfig.ps1 -c mb -t 5

    Sets time out value of monitor when on battery to 5 minutes

    SetPowerConfig.ps1 -c dp -t 15

    Sets time out value of disk when on power to 15 minutes

    SetPowerConfig.ps1 -c db -t 7

    Sets time out value of disk when on battery to 7 minutes

    SetPowerConfig.ps1 -c sp -t 30

    Sets time out value of standby when on power to 30 minutes

    SetPowerConfig.ps1 -c sb -t 10

    Sets time out value of standby when on battery to 10 minutes

    SetPowerConfig.ps1 -c hp -t 45

    Sets time out value of hibernate when on power to 45 minutes

    SetPowerConfig.ps1 -c hb -t 15

    Sets time out value of hibernate when on battery to 15 minutes

    SetPowerConfig.ps1 -q c

    Lists detailed configuration settings of the current power scheme

    SetPowerConfig.ps1 -help ?

    Displays the help topic for the script

    "@

    $helpText

    exit

    }

    if($help){funline("Obtaining help ...") ; funhelp }

    $computer = (New-Object -ComObject WScript.Network).computername

    if($q)

    {

    funline("Power configuration on: $($computer)")

    powercfg -query

    exit

    }

    if($c -and !$t)

    {

    $(Throw 'A value for $t is required.

    Try this: SetPowerConfig.ps1 -help ?')

    }

    switch($c)

    {

    "mp" { powercfg -CHANGE -monitor-timeout-ac $t }

    "mb" { powercfg -CHANGE -monitor-timeout-dc $t }

    "dp" { powercfg -CHANGE -disk-timeout-ac $t}

    "db" { powercfg -CHANGE -disk-timeout-dc $t }

    "sp" { powercfg -CHANGE -standby-timeout-ac $t }

    "sb" { powercfg -CHANGE -standby-timeout-dc $t }

    "hp" { powercfg -CHANGE -hibernate-timeout-ac $t }

    "hb" { powercfg -CHANGE -hibernate-timeout-dc $t }

    DEFAULT {

    "$c is not allowed. Try the following:

    SetPowerConfig.ps1 -help ?"

    }

    }

    该脚本首先使用param语句定义了4个参数,即-c、-t、-q和-help。其中-q指定查询,-help指定输出帮助。-c和-t必须同时使用,因为-t值指定修改电源计划的参数超时值,如果该值为空,脚本执行将出错。该脚本使用WScript.Network对象获得本机的计算机名,因此可以使用New-Object cmdlet配合-comobject参数。通过小括号将这些内容括起作为一个对象,使用ComputerName属性并将结果保存在$computer变量中。

    如果在脚本执行时提供了参数-q,那么会出现$q变量。这样可使用funline函数将计算机名作为标题输出,并使用Powercfg工具提供参数-query,从而得到本机当前的详细电源计划。

    如果提供了参数-c,则必须使用参数-t。这是因为$t变量中包括的是时间信息,需要为-c参数中指定的操作指定有限的超时时间,这样才不会无限期地等待。如果仅有$c变量,则使用throw语句输出红色的错误信息,并停止脚本执行。

    接下来脚本根据-c参数值匹配switch语句中的分支,如果值为mp,则使用包括在$t变量值作为交流电情况下的显示器超时时间的分钟数;如果值为mb,并且计算机在使用电池供电,则会使用$t变量值设置显示器的超时时间;如果值为db,则配置当前电源计划在达到$t变量设定的分钟数后关闭驱动器;如果值为sp,则在使用交流电的情况下当空闲时间超过$t变量设定的分钟数后计算机将会进入待机状态;如果需要计算机休眠,则使用hp,并修改用于交流电下休眠的电源计划值;如果使用hb,则为电池模式下的休眠值。

    此脚本在Windows Server 2008和Windows XP下通过-q参数查询电源方案的结果如图2和图3所示。

    image

    图2 在Windows 2008中列出当前系统电源计划的详细方案

    image

    图3 在WindowsXP中获取到的电源计划的信息

    作者: 付海军
    出处:http://fuhj02.cnblogs.com
    版权:本文版权归作者和博客园共有
    转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
    要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
    个人网站: http://txj.lzuer.com/

  • 相关阅读:
    单层感知机实现或运算
    Ubuntu关于eclipse新插件不显示的解决方案
    台州学院maximum cow训练记录
    利用矩阵快速幂转换的题目
    A Few Laughing Men
    TOJ1196: RSA Signing
    HDU
    2018“百度之星”程序设计大赛
    2018 “百度之星”程序设计大赛
    2018 “百度之星”程序设计大赛
  • 原文地址:https://www.cnblogs.com/fuhj02/p/1933392.html
Copyright © 2011-2022 走看看