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

PowerShell 基本语法:类型、运算、函数与错误流:本章独立本地实验

POWERSHELLWindows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验powershell-basic-syntax.ps1
powershell-basic-syntax.ps121 行
. (Join-Path $PSScriptRoot 'lab.ps1')
# Line comment; this must stay outside strings.
<# Block comment remains parser input, not execution. #>
$label='sky';$number=7;$decimal=3.14;$flag=$true;$array=@(1,2,3);$map=@{Name='mbsky';Count=3}
$types=@($label.GetType().Name,$number.GetType().Name,$decimal.GetType().Name,$flag.GetType().Name,$array.GetType().Name,$map.GetType().Name)
Assert-Learning (($types -join ',') -eq 'String,Int32,Double,Boolean,Object[],Hashtable') 'types'
$math=@((7+2),(7-2),(7*2),(7/2),(7%2));Assert-Learning (($math -join ',') -eq '9,5,14,3.5,1') 'arithmetic'
$compare=@((5 -eq 5),(5 -ne 4),(3 -lt 4),(4 -le 4),(5 -gt 4),(5 -ge 5));Assert-Learning (@($compare | Where-Object {$_ -ne $true}).Count -eq 0) 'comparisons'
Assert-Learning (($true -and -not $false) -and ($false -or $true)) 'logic'
Assert-Learning (('abc' -like 'a*') -and ('abc' -match '^a') -and ('abc' -replace 'a','s') -eq 'sbc') 'patterns'
Assert-Learning ((2 -in $array) -and ($array -contains 2) -and ('A' -eq 'a') -and -not('A' -ceq 'a')) 'membership and case'
$branches=@(50,70,95 | ForEach-Object {if($_ -ge 90){'high'}elseif($_ -ge 60){'pass'}else{'retry'}})
Assert-Learning (($branches -join ',') -eq 'retry,pass,high') 'all condition branches'
$forValues=@(for($i=1;$i -le 5;$i++){$i});$foreachValues=@(foreach($item in $array){$item*2});$whileValues=@();$counter=0;while($counter -lt 3){$whileValues+=,$counter;$counter++}
Assert-Learning (($forValues -join ',') -eq '1,2,3,4,5' -and ($foreachValues -join ',') -eq '2,4,6' -and ($whileValues -join ',') -eq '0,1,2') 'loops'
function Get-LearningOutput {param([int]$Value) 'earlier';return $Value*$Value}
$functionValues=@(Get-LearningOutput 5);Assert-Learning ($functionValues.Count -eq 2 -and $functionValues[1] -eq 25) 'return does not retract earlier output'
$root=New-LearningDirectory
try {$caught=$false;try {Get-Item -LiteralPath (Join-Path $root 'absent') -ErrorAction Stop}catch{$caught=$true};Assert-Learning $caught 'terminating missing item caught';Write-LearningReport 'powershell-basic-syntax' @{types=$types;arithmetic=$math;branches=$branches;for=$forValues;foreach=$foreachValues;while=$whileValues;function_outputs=$functionValues;missing_item_caught=$caught;single_quote='$label';double_quote="$label"}}
finally{Remove-LearningDirectory $root}
本例需在 Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

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

入口文件:projects/powershell-lab/powershell-basic-syntax.ps1

预期结果与验证条件观察提示
  • 6类类型、算术9/5/14/3.5/1、全部比较/逻辑/模式/包含及三级条件实际核对。
  • 三种循环次数、函数两个成功输出及缺文件终止错误捕获。

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

附件与关联工程包下载

下载课程关联工程包:PowerShell

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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