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

PowerShell 管道和过滤:选择、排序与末端格式化:本章独立本地实验

POWERSHELLWindows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验powershell-pipeline-filtering.ps1
powershell-pipeline-filtering.ps115 行
. (Join-Path $PSScriptRoot 'lab.ps1')
$root=New-LearningDirectory
try {
 $data=@([pscustomobject]@{Name='WinRead';DisplayName='Z Read';Status='Running';CPU=2;WorkingSet=3MB},[pscustomobject]@{Name='Other';DisplayName='B Other';Status='Stopped';CPU=0;WorkingSet=1MB},[pscustomobject]@{Name='WinBuild';DisplayName='A Build';Status='Running';CPU=6;WorkingSet=8MB})
 $full=@($data|Where-Object {$_.Name -like 'Win*'}|Sort-Object Name);$short=@($data|Where-Object Name -like 'Win*'|Sort-Object Name)
 Assert-Learning (($full.Name -join ',') -eq ($short.Name -join ',') -and $full.Count -eq 2) 'both where forms'
 $display=@($data|Where-Object Status -eq 'Running'|Sort-Object DisplayName);Assert-Learning ($display[0].Name -eq 'WinBuild') 'sort DisplayName not Name assumption'
 $memory=@($data|Sort-Object WorkingSet -Descending|Select-Object Name,WorkingSet -First 10);Assert-Learning ($memory.Count -eq 3 -and $memory[0].WorkingSet -eq 8MB) 'top10 from three'
 $selected=@($data|Select-Object @{Name='Label';Expression={$_.DisplayName}},Status);$expanded=@($data|Select-Object -ExpandProperty Name)
 Assert-Learning ($selected[0].Label -eq 'Z Read' -and $expanded[0] -is [string]) 'computed and expanded properties'
 $formatted=@($data|Format-Table Name,Status);Assert-Learning ($formatted[0].GetType().FullName -like 'Microsoft.PowerShell.Commands.Internal.Format.*') 'formatting yields control objects'
 $file=Join-Path $root 'data.csv';$data|Select-Object Name,Status|Export-Csv -LiteralPath $file -NoTypeInformation -Encoding utf8;Assert-Learning (@(Import-Csv -LiteralPath $file).Count -eq 3) 'export before formatting'
 Write-LearningReport 'powershell-pipeline-filtering' @{fixture_not_live_service=$true;filtered=$full.Name;display_sort=$display.Name;top_memory=$memory.Name;expand_returns_strings=$true;format_is_not_business_object=$true;csv_rows=3}
}finally{Remove-LearningDirectory $root}
本例需在 Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

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

入口文件:projects/powershell-lab/powershell-pipeline-filtering.ps1

预期结果与验证条件观察提示
  • 两种Where结果一致,计算/展开形状、Name与DisplayName排序、前10上限、Format指令类型及CSV3行。

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

附件与关联工程包下载

下载课程关联工程包:PowerShell

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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