PowerShell作為可編程性語言,擁有以下循環(huán)語句。 注:本節(jié)所要討論的內(nèi)容的實質(zhì)更多的偏向于程序設(shè)計方面,所以在此不做過多詳細講解,只針對PowerShell中的應(yīng)用進行具體講解。 • for (初值;表達式;賦值語句) {代碼} 用變量值控制執(zhí)行次數(shù) • foreach (成員變量 in 數(shù)組) {代碼} 利用迭代執(zhí)行代碼 • foreach-object 對一組輸入的每個對象執(zhí)行運算 • while(表達式) {代碼} 表達式為真時循環(huán)執(zhí)行代碼 • do {代碼} while(表達式) 類似于while,只是先執(zhí)行代碼,再判斷表達式真假 • do {代碼} until(表達式) 執(zhí)行代碼,直至表達式為假 循環(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 } 運行結(jié)果: ![]() 使用while監(jiān)視進程狀態(tài) notepad While(get-process -name notepad | select -Property Responding){} $time = get-date Write-Host "The Notepad failed to respond on:$time" 在此例下,若進程notepad出現(xiàn)未響應(yīng),則會產(chǎn)生屏幕輸出。 使用do while表達: notepad do{} While(get-process -name notepad | select -Property Responding) $time = get-date Write-Host "The Notepad failed to respond on:$time" 利用do until進行交互 do { "Quit Now? (Y/N)" $input=Read-Host } until($input -eq "Y") 運行結(jié)果: ![]() 使用foreach-object進行格式化輸出 對下列數(shù)據(jù)進行操作, D00454798106276487326471李德建829.51 Q00136284503715856294375張春生712.65 H00374967692981018226574劉錫明891.31 R00759861215965098103878趙子龍898.21 J00741245626115645970139楊高遠-13.21 K00142545764587219409172周明647.41 P00103851828756182786938張龍-27.51 使之輸出為以下所示格式: 1|454798106276487326471|李德建|829.51 2|136284503715856294375|張春生|712.65 3|374967692981018226574|劉錫明|891.31 4|759861215965098103878|趙子龍|898.21 5|741245626115645970139|楊高遠|0.00 6|142545764587219409172|周明|647.41 7|103851828756182786938|張龍|0.00 小計 |3979.09 使用foreach-object對每個數(shù)據(jù)成員使用正則表達式,最后格式化輸出即可: ${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小計`t`t|$total"} 運行結(jié)果: ![]() 歡迎提出意見建議! |
免責聲明:本站部分文章和圖片均來自用戶投稿和網(wǎng)絡(luò)收集,旨在傳播知識,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請勿用于商業(yè)用途,如果損害了您的權(quán)利,請聯(lián)系我們及時修正或刪除。謝謝!
始終以前瞻性的眼光聚焦站長、創(chuàng)業(yè)、互聯(lián)網(wǎng)等領(lǐng)域,為您提供最新最全的互聯(lián)網(wǎng)資訊,幫助站長轉(zhuǎn)型升級,為互聯(lián)網(wǎng)創(chuàng)業(yè)者提供更加優(yōu)質(zhì)的創(chuàng)業(yè)信息和品牌營銷服務(wù),與站長一起進步!讓互聯(lián)網(wǎng)創(chuàng)業(yè)者不再孤獨!
掃一掃,關(guān)注站長網(wǎng)微信