while (<condition>) { <action block> }
PS C:\> while (Get-Process notepad -ErrorAction SilentlyContinue){ >> Write-Host "Waiting for notepad to exit" >> sleep 1 >> } >> Waiting for notepad to exit Waiting for notepad to exit Waiting for notepad to exit
PS C:\> $i=5 PS C:\> do{ >> Write-Host 'incermenting $i' >> $i++ >> }while($i -le 1) >> incermenting $i
PS C:\> $i=5 PS C:\> do{ >> Write-Host 'incermenting $i' >> $i++ >> }while($i -le 1) >> incermenting $i PS C:\> $i=0; PS C:\> do{ >> Write-Host "`$i=$i" >> $i++ >> }until ($i -ge 3) >> $i=0 $i=1 $i=2
PS C:\> $i=5 PS C:\> do{ >> Write-Host 'incermenting $i' >> $i++ >> }while($i -le 1) >> incermenting $i PS C:\> $i=0; PS C:\> do{ >> Write-Host "`$i=$i" >> $i++ >> }while (-not($i -ge 3)) >> $i=0 $i=1 $i=2
for(<initializer>;<exit condition>;<step action>) { <action> }
PS C:\> for($i=0;$i -lt 3;$i++){ >> Write-Host "`$i=$i" >> } >> $i=0 $i=1 $i=2
PS C:\> $i=0; PS C:\> for(;$i -lt 3;){ >> Write-Host "`$i=$i" >> $i++ >> } >> $i=0 $i=1 $i=2
PS C:\>$items=2,3,4 PS C:\>for($i=0;$i –lt $items.Length;$i++){ >>$item=$items[$i] >>Write-Host $item >>} >> 2 3 4
foreach($item in $collection) { <action> }
PS C:\>$items=2,3,4 PS C:\>foreach ($item in $items){ >> Write-Host $item >>} >> 2 3 4
PS C:\>foreach ($file in dir){ >>if($file.Name –like "*.log"){ >>Write-Host $fiile.Name >>break >> } >>} >> test.log
PS C:\>foreach ($item in 1..5){ >> if ($item %2 –eq 0){ >> continue >> } >>Write-Host $item >>} 1 3 5
PS C:\>foreach ($outerItem in 1..3){ >> Write-Host "OuterItem:$outerItem" >> foreach($innerItem in 1..3){ >> Write-Host "InnerItem:$innerItem" >> break; >> } >>Write-Host $item >>} >> OuterItem:1 InnerItem:1 OuterItem:2 InnerItem:1 OuterItem:3 InnerItem:1
PS C:\>:OuterLoop foreach ($outerItem in 1..3){ >> Write-Host "OuterItem:$outerItem" >> foreach($innerItem in 1..3){ >> Write-Host "InnerItem:$innerItem" >> break OuterLoop; >> } >>Write-Host $item >>} >> OuterItem:1 InnerItem:1
PS C:\>:OuterLoop foreach ($outerItem in 1..3){ >> Write-Host "OuterItem:$outerItem" >> foreach($innerItem in 1..3){ >> Write-Host "InnerItem:$innerItem" >> break OuterLoop; >> } >>Write-Host $item >>} >> OuterItem:1 InnerItem:1 OuterItem:2 InnerItem:1 OuterItem:3 InnerItem:1
出处:http://fuhj02.blog.51cto.com
版权:本文版权归作者和51cto共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
个人网站: http://txj.shell.tor.hu/