Windows Vista和Windows Server 2008的電源計劃有大量改進,可以針對使用電池或交流電供電的情況分別設置。如果當前計算機正在使用電池,那麼續航時間是個需要關心的問題。而在某些情況下,計算機的性能纔是最重要的。例如,如果電力會在幾分鐘之後恢復,則不必爲延長電池使用時間而降低性能。在不同的電源計劃下顯示器、磁盤及CPU的電力消耗也不同,在Windows Server 2008系統中可以創建自定義的電源計劃,如圖1所示。
圖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)
-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所示。
圖2 在Windows 2008中列出當前系統電源計劃的詳細方案
圖3 在WindowsXP中獲取到的電源計劃的信息
作者: 付海軍
出處:http://blog.csdn.net/fuhj02
版權:本文版權歸作者和csdn共有
轉載:歡迎轉載,爲了保存作者的創作熱情,請按要求【轉載】,謝謝
要求:未經作者同意,必須保留此段聲明;必須在文章中給出原文連接;否則必究法律責任
個人網站: http://txj.lzuer.com/