探索PowerShell(十) 循環(huán)語句介紹
PowerShell作為可編程性語言,擁有以下循環(huán)語句。
注:本節(jié)所要討論的內(nèi)容的實(shí)質(zhì)更多的偏向于程序設(shè)計(jì)方面,所以在此不做過多詳細(xì)講解,只針對(duì)PowerShell中的應(yīng)用進(jìn)行具體講解。
• for (初值;表達(dá)式;賦值語句) {代碼} 用變量值控制執(zhí)行次數(shù)
• foreach (成員變量 in 數(shù)組) {代碼} 利用迭代執(zhí)行代碼
• foreach-object 對(duì)一組輸入的每個(gè)對(duì)象執(zhí)行運(yùn)算
• while(表達(dá)式) {代碼} 表達(dá)式為真時(shí)循環(huán)執(zhí)行代碼
• do {代碼} while(表達(dá)式) 類似于while,只是先執(zhí)行代碼,再判斷表達(dá)式真假
• do {代碼} until(表達(dá)式) 執(zhí)行代碼,直至表達(dá)式為假
循環(huán)語句在PowerShell中的應(yīng)用
利用foreach查詢硬件信息
例一:
$DiskDrive=get-wmiobject -class Win32_DiskDrive -namespace root\CIMV2
foreach ($item in $DiskDrive)
{
write-host "Description:" $item.Description
write-host "Device ID:" $item.DeviceID
write-host "Interface Type:" $item.InterfaceType
write-host "Media Type:" $item.MediaType
write-host "Model:" $item.Model
write-host "Partitions:" $item.Partitions
write-host "Size:" $item.Size
write-host "Status:" $item.Status
}
例二:
$Processor=get-wmiobject -class Win32_Processor -namespace root\CIMV2
foreach ($item in $Processor)
{
write-host "Caption:" $item.Caption
write-host "CPU Status:" $item.CpuStatus
write-host "Current Clock Speed:" $item.CurrentClockSpeed
write-host "Device ID:" $item.DeviceID
write-host "L2 Cache Size:" $item.L2CacheSize
write-host "L2 Cache Speed:" $item.L2CacheSpeed
write-host "Name:" $item.Name
}
運(yùn)行結(jié)果:

使用while監(jiān)視進(jìn)程狀態(tài)
notepad
While(get-process -name notepad | select -Property Responding){}
$time = get-date
Write-Host "The Notepad failed to respond on:$time"
在此例下,若進(jìn)程notepad出現(xiàn)未響應(yīng),則會(huì)產(chǎn)生屏幕輸出。
使用do while表達(dá):
notepad
do{}
While(get-process -name notepad | select -Property Responding)
$time = get-date
Write-Host "The Notepad failed to respond on:$time"
利用do until進(jìn)行交互
do
{
"Quit Now? (Y/N)"
$input=Read-Host
}
until($input -eq "Y")
運(yùn)行結(jié)果:

使用foreach-object進(jìn)行格式化輸出
對(duì)下列數(shù)據(jù)進(jìn)行操作,
D00454798106276487326471李德建829.51
H00374967692981018226574劉錫明891.31
R00759861215965098103878趙子龍898.21
J00741245626115645970139楊高遠(yuǎn)-13.21
K00142545764587219409172周明647.41
P00103851828756182786938張龍-27.51
使之輸出為以下所示格式:
1|454798106276487326471|李德建|829.51
3|374967692981018226574|劉錫明|891.31
4|759861215965098103878|趙子龍|898.21
5|741245626115645970139|楊高遠(yuǎn)|0.00
6|142545764587219409172|周明|647.41
7|103851828756182786938|張龍|0.00
小計(jì) |3979.09
使用foreach-object對(duì)每個(gè)數(shù)據(jù)成員使用正則表達(dá)式,最后格式化輸出即可:
${C:\test.txt} | `
foreach-object{$total=0;$id=1}`
{
[void]($_ -match '^.{3}(?<id>\d+)(?<name>[\p{IsCJKUnifiedIdeographs}]+)(?<salary>[\d.]*)');
$ofs = '|';
"$($id;$id++;$matches.id;$matches.name;'{0:f2}' -f [float]$matches.salary)";
$total += $matches.salary
}`
{"`t小計(jì)`t`t|$total"}
運(yùn)行結(jié)果:

歡迎提出意見建議!
相關(guān)文章
Powershell小技巧之非相同域或信任域也能遠(yuǎn)程
這篇文章主要介紹了使用Powershell在非相同域或信任域也能遠(yuǎn)程的方法以及如何設(shè)置powershell遠(yuǎn)程處理的方法,需要的朋友可以參考下2014-10-10
PowerShell中簡(jiǎn)單的自定義函數(shù)和調(diào)用函數(shù)例子
這篇文章主要介紹了PowerShell中簡(jiǎn)單的自定義函數(shù)和調(diào)用函數(shù)例子,非常簡(jiǎn)單的一個(gè)小例子,需要的朋友可以參考下2014-08-08
PowerShell正則表達(dá)式(Regex)從右往左進(jìn)行匹配方法代碼實(shí)例
這篇文章主要介紹了PowerShell正則表達(dá)式(Regex)從右往左進(jìn)行匹配方法代碼實(shí)例,最重要的就是一個(gè)RightToLeft參數(shù)的運(yùn)用,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-05-05
Powershell獲取系統(tǒng)中所有可停止的服務(wù)
這篇文章主要介紹了Powershell獲取系統(tǒng)中所有可停止的服務(wù),本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03
Powershell小技巧之使用Copy-Item添加程序到開機(jī)啟動(dòng)
本文主要記錄了本人安裝office2013后遇到的一個(gè)小需求,然后用powershell實(shí)現(xiàn)了這個(gè)功能,特此記錄下,并附上copy-item的使用方法2014-09-09
Powershell改變腳本執(zhí)行優(yōu)先權(quán)的代碼分享
這篇文章主要介紹了Powershell改變腳本執(zhí)行優(yōu)先權(quán)的代碼分享,本文通過控制進(jìn)程的方式調(diào)整程序的執(zhí)行順序,需要的朋友可以參考下2014-11-11
PowerShell函數(shù)指定返回值類型實(shí)例
這篇文章主要介紹了PowerShell函數(shù)指定返回值類型,即定義PowerShell函數(shù)的返回值類型,需要的朋友可以參考下2014-07-07
在cmd中直接運(yùn)行PowerShell腳本文件的方法
這篇文章主要介紹了在cmd中直接運(yùn)行PowerShell腳本文件的方法,本文給出了兩個(gè)小技巧實(shí)現(xiàn)在cmd中直接運(yùn)行PowerShell腳本,需要的朋友可以參考下2014-12-12

