代码改变世界

PowerShell2.0之桌面计算机维护(五)管理桌面电源设置

2011-01-11 01:35  @天行健中国元素  阅读(2199)  评论(2编辑  收藏  举报

便携式计算机的供电是个很重要的问题,因其直接关系到便携式设备的可移动性。Windows有多个组件与电源管理的配置策略相关,在Windows Server 2008中的电源策略设置界面如图1所示。

image

图1 Windows Server 2008中的电源策略设置界面

创建名为“ReportPowerConfig.ps1”的脚本,根据用户提供的如下参数提供相应的电源配置信息。

  1. a:当前主机中活动的电源设置。
  2. l:当前主机中的所有电源配置。
  3. q:当前主机中的所有可用休眠状态。
  4. w:当前主机中的上次唤醒事件。
  5. d:当前主机中的所有的设备。
  6. dv:当前主机中的所有设备的详细信息。
  7. dwa:当前主机中已配置且可唤醒当前主机的设备。
  8. dwp:当前主机中所有配置为可从睡眠中唤醒计算机的设备。

该脚本的代码如下:

param($a="a", $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: ReportPowerConfig.ps1

Prints power config on a local machine.

PARAMETERS:

-a(ction) action to perform <a(ctive scheme), l(ist),

q(uery), d(evice), dv(evice verbose),

dwa(evice wake armed), dwp(evice wake programable)>

-help prints help file

SYNTAX:

ReportPowerConfig.ps1

Lists power configuration on local computer

ReportPowerConfig.ps1 -a a

Lists active power configuration on local computer

ReportPowerConfig.ps1 -a l

Lists all power configuration on local computer

ReportPowerConfig.ps1 -a q

Lists all available sleep states on local computer

ReportPowerConfig.ps1 -a w

Lists last wake event on local computer

ReportPowerConfig.ps1 -a d

Lists all devices on local computer

ReportPowerConfig.ps1 -a dv

Lists all devices on local computer - verbose

ReportPowerConfig.ps1 -a dwa

Lists devices configured to wake the local computer

ReportPowerConfig.ps1 -a dwp

Lists devices that are user confiurable to wake the

computer from sleep on local computer

ReportPowerConfig.ps1 -help ?

Displays the help topic for the script

"@

$helpText

exit

}

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

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

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

switch($a)

{

"a" { POWERCFG -GETACTIVESCHEME ; "`r"}

"l" { powercfg -LIST }

"q" { powercfg -AVAILABLESLEEPSTATES }

"w" { powercfg -lastwake }

"d" { powercfg -devicequery all_devices }

"dv" { powercfg -devicequery all_devices_verbose }

"dwa" { powercfg -devicequery wake_armed }

"dwp" { powercfg -devicequery wake_programmable }

}

在脚本中首先使用param语句定义了两个命令行参数-a和-help,-a指定脚本执行的操作,默认值为a;-help指定显示帮助信息,包括描述、参数及语法范例。要强调的是这个脚本无法在远程执行。

获取计算机名称时可以使用WScript.Network COM对象,并使用New-Object cmdlet创建该对象,然后提供-comobject参数。选择ComputerName属性,计算机名自动保存在$computer变量中。该脚本中的大部分的逻辑控制在switch语句中完成,这些语句通过判断命令行中的$a变量值选择相应的分支。如果值为a,则使用Powercfg工具获取当前电源计划。执行结果如图2和图3所示,可以看到当前计算机可以由网卡远程唤醒。需要强调的是由于Windows XP和Windows Server 2008的powercfg工具的工作环境不同,所以将-a参数的a或q选项传递给脚本使将会抛出“参数无效,键入"/?"得到帮助”的提示信息。

image

图2 列出当前系统中可用的电源计划

image

图3 获取可从睡眠中唤醒计算机的设备

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