. (Join-Path $PSScriptRoot 'lab.ps1')
function Convert-LearningValue {param([Parameter(ValueFromPipeline=$true)][string]$Value) process {$Value.ToUpperInvariant()}}
function Get-LearningName {param([Parameter(ValueFromPipelineByPropertyName=$true)][string]$Name) process {$Name}}
$byValue=@('read','build'|Convert-LearningValue);$byProperty=@([pscustomobject]@{Name='mbsky'}|Get-LearningName)
Assert-Learning (($byValue -join ',') -eq 'READ,BUILD' -and $byProperty[0] -eq 'mbsky') 'two actual binding styles'
$tokens=$null;$parseErrors=$null;$null=[Management.Automation.Language.Parser]::ParseInput('"notepad" | Get-Process -Name',[ref]$tokens,[ref]$parseErrors)
# Missing argument can be a runtime binding error rather than a parser error. Inspect metadata, do not execute the destructive pipeline.
$command=Get-Command Get-Process;$nameParameter=$command.Parameters['Name'];Assert-Learning ($nameParameter.ParameterType -eq [string[]]) 'Name needs value'
$root=New-LearningDirectory
try {$folder=New-Item -Path (Join-Path $root 'notes') -ItemType Directory;$file=New-Item -Path (Join-Path $folder.FullName 'hello.txt') -ItemType File;Set-Content -LiteralPath $file.FullName -Value '蓝色天空网';$content=Get-Content -LiteralPath $file.FullName;Assert-Learning ($content -eq '蓝色天空网') 'file cmdlets';Write-LearningReport 'powershell-cmdlet' @{by_value=$byValue;by_property=$byProperty;name_requires_string_array=$true;bad_source_fragment_parse_errors=$parseErrors.Count;bad_fragment_not_executed=$true;file_read=$content;help_update_or_system_change_executed=$false}}
finally {Remove-LearningDirectory $root}
本例需在 Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验
入口文件:projects/powershell-lab/powershell-cmdlet.ps1
预期结果与验证条件观察提示
- ByValue得READ/BUILD,ByPropertyName得mbsky;真实Name参数需要String[];自建文件可读。
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联16章) · 含当前章节或实例 · mbskycom202611a525.zip