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

PowerShell 对象命令行:属性、计算列和结构化导出:本章独立本地实验

POWERSHELLWindows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验powershell-object-oriented-command-line.ps1
powershell-object-oriented-command-line.ps119 行
. (Join-Path $PSScriptRoot 'lab.ps1')
$root=New-LearningDirectory
try {
 $data=@([pscustomobject]@{Name='WinLearn';Status='Running';CPU=12;WorkingSet=8MB},[pscustomobject]@{Name='Read';Status='Stopped';CPU=2;WorkingSet=2MB},[pscustomobject]@{Name='WinBuild';Status='Running';CPU=30;WorkingSet=16MB})
 $running=@($data | Where-Object {$_.Status -eq 'Running'})
 $memory=@($data | Select-Object Name,@{Name='MemoryMiB';Expression={$_.WorkingSet/1MB}})
 $top=@($data | Sort-Object CPU -Descending | Select-Object -First 5 Name,CPU)
 $groups=@($data | Group-Object Status | Sort-Object Name)
 $measure=$data | Measure-Object CPU -Sum -Average
 Assert-Learning ($running.Count -eq 2 -and $memory[2].MemoryMiB -eq 16) 'filter and computed column'
 Assert-Learning ($top[0].Name -eq 'WinBuild' -and $top.Count -eq 3) 'sort top bounded by inputs'
 Assert-Learning ($groups[0].Count -eq 2 -and $measure.Sum -eq 44) 'group and measure'
 $file=Join-Path $root 'report.csv';$data | Select-Object Name,Status | Export-Csv -LiteralPath $file -NoTypeInformation -Encoding utf8
 $back=@(Import-Csv -LiteralPath $file);Assert-Learning ($back.Count -eq 3 -and $back[0].Status -eq 'Running') 'CSV roundtrip'
 $json=$data | ConvertTo-Json -Depth 4;$backJson=@($json | ConvertFrom-Json);Assert-Learning ($backJson[2].CPU -eq 30) 'JSON roundtrip'
 $members=@(Get-Process -Id $PID | Get-Member);Assert-Learning (@($members | Where-Object Name -eq 'Id').Count -eq 1) 'real process member'
 Write-LearningReport 'powershell-object-oriented-command-line' @{fixture_is_not_live_services=$true;running_count=$running.Count;memory_mib=$memory.MemoryMiB;top_names=$top.Name;group_counts=$groups.Count;cpu_sum=$measure.Sum;csv_rows=$back.Count;json_numeric_cpu=$backJson[2].CPU;real_process_id_member=$true}
} finally {Remove-LearningDirectory $root}
本例需在 Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

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

入口文件:projects/powershell-lab/powershell-object-oriented-command-line.ps1

预期结果与验证条件观察提示
  • 教学集合Running2项、内存16MiB、CPU总44秒;分组、CSV3行和JSON数字往返。

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

附件与关联工程包下载

下载课程关联工程包:PowerShell

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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