探索PowerShell(十) 循環(huán)語(yǔ)句介紹
PowerShell作為可編程性語(yǔ)言,擁有以下循環(huán)語(yǔ)句。
注:本節(jié)所要討論的內(nèi)容的實(shí)質(zhì)更多的偏向于程序設(shè)計(jì)方面,所以在此不做過(guò)多詳細(xì)講解,只針對(duì)PowerShell中的應(yīng)用進(jìn)行具體講解。
• for (初值;表達(dá)式;賦值語(yǔ)句) {代碼} 用變量值控制執(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)語(yǔ)句在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 ISE中代碼轉(zhuǎn)換大小寫的技巧
這篇文章主要介紹了PowerShell ISE中代碼轉(zhuǎn)換大小寫的技巧,本文使用ISE內(nèi)置快捷鍵解決這個(gè)問題,需要的朋友可以參考下2015-03-03Windows Powershell 通過(guò)函數(shù)擴(kuò)展別名
這篇文章主要介紹了Windows Powershell 通過(guò)函數(shù)擴(kuò)展別名,需要的朋友可以參考下2014-09-09PowerShell中使用正則表達(dá)式篩選數(shù)組實(shí)例
這篇文章主要介紹了PowerShell中使用正則表達(dá)式篩選數(shù)組實(shí)例,使用match操作符配合相應(yīng)正則表達(dá)式實(shí)現(xiàn)數(shù)組篩選功能,需要的朋友可以參考下2014-07-07自定義PowerShell控制臺(tái)提示符風(fēng)格的方法
這篇文章主要介紹了自定義PowerShell控制臺(tái)提示符風(fēng)格的方法,需要的朋友可以參考下2014-04-04PowerShell ISE自動(dòng)化簡(jiǎn)單示例
這篇文章主要介紹了PowerShell ISE自動(dòng)化簡(jiǎn)單示例,本文給出了2個(gè)PowerShell ISE自動(dòng)化例子,需要的朋友可以參考下2015-03-03PowerShell實(shí)現(xiàn)在控制臺(tái)中插入綠色的打勾符號(hào)
這篇文章主要介紹了PowerShell實(shí)現(xiàn)在控制臺(tái)中插入綠色的打勾符號(hào),本文重點(diǎn)的過(guò)分如何輸出個(gè)一個(gè)特殊字符以及如何輸出一個(gè)有顏色的字符,需要的朋友可以參考下2015-06-06PowerShell函數(shù)中使用$PSBoundParameters獲取輸入?yún)?shù)列表實(shí)例
這篇文章主要介紹了PowerShell函數(shù)中使用$PSBoundParameters獲取輸入?yún)?shù)列表實(shí)例,需要的朋友可以參考下2014-07-07