【探索PowerShell 】【十】循环语句

简介:

PowerShell作为可编程性语言,拥有以下循环语句。

注:本节所要讨论的内容的实质更多的偏向于程序设计方面,所以在此不做过多详细讲解,只针对PowerShell中的应用进行具体讲解。

    • for (初值;表达式;赋值语句) {代码}          用变量值控制执行次数

    • foreach (成员变量 in 数组) {代码}         利用迭代执行代码

    • foreach-object                                       对一组输入的每个对象执行运算

    • while(表达式) {代码}                              表达式为真时循环执行代码

    • do {代码}  while(表达式)                         类似于while,只是先执行代码,再判断表达式真假

    • do {代码}  until(表达式)                            执行代码,直至表达式为假

 

循环语句在PowerShell中的应用

利用foreach查询硬件信息

例一:

 
  1. $DiskDrive=get-wmiobject -class Win32_DiskDrive -namespace root\CIMV2  
  2.  
  3. foreach ($item in $DiskDrive)  
  4. {  
  5. write-host "Description:" $item.Description  
  6. write-host "Device ID:" $item.DeviceID  
  7. write-host "Interface Type:" $item.InterfaceType  
  8. write-host "Media Type:" $item.MediaType  
  9. write-host "Model:" $item.Model  
  10. write-host "Partitions:" $item.Partitions  
  11. write-host "Size:" $item.Size 
  12. write-host "Status:" $item.Status  

运行结果:

 

例二:

 
  1. $Processor=get-wmiobject -class Win32_Processor -namespace root\CIMV2  
  2.  
  3. foreach ($item in $Processor)   
  4. {  
  5. write-host "Caption:" $item.Caption  
  6. write-host "CPU Status:" $item.CpuStatus  
  7. write-host "Current Clock Speed:" $item.CurrentClockSpeed  
  8. write-host "Device ID:" $item.DeviceID  
  9. write-host "L2 Cache Size:" $item.L2CacheSize  
  10. write-host "L2 Cache Speed:" $item.L2CacheSpeed  
  11. write-host "Name:" $item.Name 

运行结果:

 

使用while监视进程状态

 
  1. notepad  
  2. While(get-process -name notepad | select -Property Responding){}  
  3. $time = get-date 
  4. Write-Host "The Notepad failed to respond on:$time" 

在此例下,若进程notepad出现未响应,则会产生屏幕输出。

使用do while表达:

 
  1. notepad
  2. do{} 
  3. While(get-process -name notepad | select -Property Responding)  
  4. $time = get-date 
  5. Write-Host "The Notepad failed to respond on:$time" 

 

利用do until进行交互

 
  1. do  
  2. {    
  3.     "Quit Now? (Y/N)" 
  4.     $input=Read-Host   
  5. }  
  6. until($input -eq "Y"

运行结果:

 

使用foreach-object进行格式化输出

对下列数据进行操作,

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对每个数据成员使用正则表达式,最后格式化输出即可:

 
  1. ${C:\test.txt} | `  
  2. foreach-object{$total=0;$id=1}`  
  3. {  
  4.     [void]($_ -match '^.{3}(?<id>\d+)(?<name>[\p{IsCJKUnifiedIdeographs}]+)(?<salary>[\d.]*)');  
  5.     $ofs = '|';  
  6.     "$($id;$id++;$matches.id;$matches.name;'{0:f2}' -f [float]$matches.salary)";  
  7.     $total += $matches.salary  
  8. }`  
  9. {"`t小计`t`t|$total"

 运行结果:







     本文转自melvillo 51CTO博客,原文链接:http://blog.51cto.com/marui/294113,如需转载请自行联系原作者





相关文章
|
15天前
|
人工智能 机器人 Shell
【shell】shell条件判断、循环语句、基本运算符
【shell】shell条件判断、循环语句、基本运算符
|
4月前
|
Java Shell C#
shell(四)条件判断语句
条件判断语句在编程中使用是很频繁的,首先我们想到的就是if-else语句。但是这里要区分开,条件判断语句是指if括号里边的那个语句,不是指if-else,if-else是流程控制语句。
26 0
|
8月前
|
存储 Java Shell
shell&流程控制语句if
shell 流程控制语句if
|
8月前
|
Shell
Shell if else 条件判断
Shell if else 条件判断
44 0
|
10月前
|
Shell
【Shell编程】Shell中for循环、while循环、until循环语句
【Shell编程】Shell中for循环、while循环、until循环语句
38 0
VBS基础篇 - 循环语句(3) - For...Next
VBS基础篇 - 循环语句(3) - For...Next     指定循环次数,使用计数器重复运行语句,语法结构如下:    1 2 3 4 5 For counter = start To end [Step step]     [statements]     [Exit For]     [statements] Next      主要参数:        counter:用做循环计数器的数值变量。
1210 0
VBS基础篇 - 循环语句(4) - For Each...Next
VBS基础篇 - 循环语句(4) - For Each...Next   For Each...Next 循环与 For...Next 循环类似。For Each...Next 不是将语句运行指定的次数,而是对于数组中的每个元素或对象集合中的每一项重复一组语句。
1044 0