. (Join-Path $PSScriptRoot 'lab.ps1')
$waited=Start-LearningChild -DelayMs 2000
$stopped=$null
try {
Assert-Learning ($waited.Id -ne $PID) 'owned wait child'
Wait-Process -InputObject $waited -Timeout 10;$waited.Refresh();Assert-Learning $waited.HasExited 'Wait-Process actual natural exit'
$stopped=Start-LearningChild -DelayMs 15000;Stop-LearningChild $stopped;$stopped.Refresh();Assert-Learning $stopped.HasExited 'Stop-Process own child only'
$serviceCommands=@('Get-Service','Start-Service','Stop-Service','Restart-Service','Set-Service') | ForEach-Object {Get-Command $_}
Assert-Learning (@($serviceCommands).Count -eq 5) 'five Windows service command metadata'
$sample=@(Get-Service -ErrorAction SilentlyContinue | Select-Object -First 1)
Assert-Learning ($sample.Count -eq 1) 'actual read-only service sample'
$status=[string]$sample[0].Status;$sample[0].Refresh()
Write-LearningReport 'cmdlet-process-and-service-management' @{natural_child_exit=$waited.HasExited;stopped_owned_child=$stopped.HasExited;service_command_count=5;actual_service_sample_status=$status;service_sample_name_not_exported=$true;service_mutations_executed=$false;cpu_means_cumulative_seconds=$true}
}finally{Stop-LearningChild $waited;$waited.Dispose();if($null -ne $stopped){Stop-LearningChild $stopped;$stopped.Dispose()}}
本例需在 Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验
入口文件:projects/powershell-lab/cmdlet-process-and-service-management.ps1
预期结果与验证条件观察提示
- 一个自建进程实际等待结束,另一个实际停止;真实服务只读样本,不改系统服务。
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联16章) · 含当前章节或实例 · mbskycom202611a525.zip