蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

PowerShell 控制结构:全部循环、switch与终止错误:本章独立本地实验

POWERSHELLWindows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验powershell-control-structures.ps1
powershell-control-structures.ps123 行
. (Join-Path $PSScriptRoot 'lab.ps1')
$branches=@(3GB,7GB,12GB|ForEach-Object {if($_ -lt 5GB){'low'}elseif($_ -lt 10GB){'middle'}else{'enough'}})
Assert-Learning (($branches -join ',') -eq 'low,middle,enough') 'all disk branches'
$switchValues=@(switch -Wildcard (@('a.txt','a.jpg','a.bin')) {'*.txt'{'text'} '*.jpg'{'image'} default{'other'}})
Assert-Learning (($switchValues -join ',') -eq 'text,image,other') 'wildcard switch branches'
$multi=@(switch ('abc') {'abc'{'exact'} {$_ -like 'a*'}{'pattern'}});Assert-Learning ($multi.Count -eq 2) 'switch can match multiple clauses'
$for=@(for($i=1;$i -le 5;$i++){$i});$foreach=@(foreach($s in @('apple','banana','cherry')){$s.Length});$pipeline=@('apple','banana','cherry'|ForEach-Object {$_.Length})
$while=@();$n=0;while($n -lt 3){$while+=,$n;$n++};$zero=0;while($false){$zero++};$once=0;do{$once++}while($false)
$dw=@();$n=0;do{$dw+=,$n;$n++}while($n -lt 3);$du=@();$n=0;do{$du+=,$n;$n++}until($n -ge 3)
Assert-Learning (($for -join ',') -eq '1,2,3,4,5' -and ($foreach -join ',') -eq '5,6,6' -and ($pipeline -join ',') -eq '5,6,6') 'for foreach pipeline'
Assert-Learning (($while -join ',') -eq '0,1,2' -and ($dw -join ',') -eq '0,1,2' -and ($du -join ',') -eq '0,1,2' -and $zero -eq 0 -and $once -eq 1) 'all loop conditions'
$skip=@(foreach($i in 1..6){if($i -eq 2){continue};if($i -eq 5){break};$i});Assert-Learning (($skip -join ',') -eq '1,3,4') 'break and continue'
$root=New-LearningDirectory
try {
 $divisionCaught=$false;$finallyRan=$false;try{$null=10/0}catch{$divisionCaught=$true}finally{$finallyRan=$true}
 Assert-Learning ($divisionCaught -and $finallyRan) 'division catch finally'
 $nonTerminatedCatch=$false;$continued=$false;try{Get-Item -LiteralPath (Join-Path $root 'absent') -ErrorAction SilentlyContinue;$continued=$true}catch{$nonTerminatedCatch=$true}
 Assert-Learning ($continued -and -not $nonTerminatedCatch) 'nonterminating error continues'
 $terminatingType='';try{Get-Item -LiteralPath (Join-Path $root 'absent') -ErrorAction Stop}catch [Management.Automation.ItemNotFoundException]{$terminatingType=$_.Exception.GetType().FullName}
 Assert-Learning ($terminatingType -eq 'System.Management.Automation.ItemNotFoundException') 'actual typed catch'
 Write-LearningReport 'powershell-control-structures' @{if_branches=$branches;switch_branches=$switchValues;switch_multi_count=$multi.Count;for=$for;foreach_lengths=$foreach;pipeline_lengths=$pipeline;while=$while;do_while=$dw;do_until=$du;while_false_times=$zero;do_false_times=$once;break_continue=$skip;division_caught=$divisionCaught;finally_ran=$finallyRan;nonterminating_continued=$continued;typed_error=$terminatingType;seven_is_odd=(7%2 -ne 0)}
}finally{Remove-LearningDirectory $root}
本例需在 Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验

入口文件:projects/powershell-lab/powershell-control-structures.ps1

预期结果与验证条件观察提示
  • 所有条件、switch两种目标、六种循环、零次/至少一次、break/continue分别实测。
  • 除零、非终止继续、ItemNotFound类型catch及finally实际核对。

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:PowerShell

课程关联工程包(登记关联16章) · 含当前章节或实例 · mbskycom202611a525.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习