param([string]$BaseUrl='http://127.0.0.1:8771')
. (Join-Path $PSScriptRoot 'lab.ps1')
$root=New-LearningDirectory
try {
$inputs=New-Item -Path (Join-Path $root 'inputs') -ItemType Directory
foreach($name in @('b.txt','a.txt')){Set-Content -LiteralPath (Join-Path $inputs.FullName $name) -Value $name}
$files=@(Get-ChildItem -LiteralPath $inputs.FullName -File -Filter '*.txt'|Sort-Object Name);$counter=1;$plan=@(foreach($f in $files){[pscustomobject]@{Old=$f.FullName;New=('log_{0:D3}.txt' -f $counter)};$counter++})
Assert-Learning (($plan.New -join ',') -eq 'log_001.txt,log_002.txt') 'stable rename plan'
foreach($row in $plan){$target=Assert-LearningPath -Path (Join-Path $inputs.FullName $row.New) -Root $root;if(Test-Path -LiteralPath $target){throw 'rename collision before any move'}}
foreach($row in $plan){$safe=Assert-LearningPath -Path $row.Old -Root $root;Rename-Item -LiteralPath $safe -NewName $row.New}
Assert-Learning ((Get-Content -LiteralPath (Join-Path $inputs.FullName 'log_001.txt')).Trim() -eq 'a.txt') 'actual deterministic rename'
$cutoff=[datetime]'2026-11-01T00:00:00Z';$old=Join-Path $root 'old.tmp';$new=Join-Path $root 'new.tmp';Set-Content -LiteralPath $old -Value 'old';Set-Content -LiteralPath $new -Value 'new';(Get-Item -LiteralPath $old).LastWriteTimeUtc=$cutoff.AddDays(-8);(Get-Item -LiteralPath $new).LastWriteTimeUtc=$cutoff
$expired=@(Get-ChildItem -LiteralPath $root -File -Filter '*.tmp'|Where-Object {$_.LastWriteTimeUtc -lt $cutoff.AddDays(-7)})
Assert-Learning ($expired.Count -eq 1) 'only old own file'
foreach($file in $expired){$safe=Assert-LearningPath -Path $file.FullName -Root $root;Remove-Item -LiteralPath $safe -WhatIf 6>$null}
Assert-Learning (Test-Path -LiteralPath $old) 'WhatIf did not delete'
foreach($file in $expired){$safe=Assert-LearningPath -Path $file.FullName -Root $root;Remove-Item -LiteralPath $safe}
Assert-Learning (-not(Test-Path -LiteralPath $old) -and (Test-Path -LiteralPath $new)) 'real owned expiry deletion'
$log=Join-Path $root 'requests.log';@('#Fields: date uri status bytes','2026-11-01 /ok500 200 500','2026-11-01 /error 500 20','2026-11-01 /missing 404 5')|Set-Content -LiteralPath $log
$errors=@(Get-Content -LiteralPath $log|Where-Object {$_ -notlike '#*'}|ForEach-Object {$v=$_ -split '\s+';[pscustomobject]@{Date=$v[0];Uri=$v[1];Status=[int]$v[2];Bytes=[int]$v[3]}}|Where-Object Status -eq 500)
Assert-Learning ($errors.Count -eq 1 -and $errors[0].Uri -eq '/error') 'status field not substring500'
$bytes=[Text.Encoding]::UTF8.GetBytes('mbsky.com');$hashFile=Join-Path $root 'hash.txt';[IO.File]::WriteAllBytes($hashFile,$bytes)
$sha=(Get-FileHash -LiteralPath $hashFile -Algorithm SHA256).Hash;$md5=[Security.Cryptography.MD5]::Create();try{$legacy=[Convert]::ToHexString($md5.ComputeHash($bytes))}finally{$md5.Dispose()}
Assert-Learning ($sha.Length -eq 64 -and $legacy -eq (Get-FileHash -LiteralPath $hashFile -Algorithm MD5).Hash) 'actual hash legacy and modern'
Assert-Learning (([uri]$BaseUrl).Host -eq '127.0.0.1') 'local API only'
$api=Invoke-RestMethod -Uri ($BaseUrl+'/learning') -TimeoutSec 5;Assert-Learning ($api.lessons -eq 3) 'real API response'
$httpError=$false;try{Invoke-RestMethod -Uri ($BaseUrl+'/missing') -TimeoutSec 5}catch{$httpError=$true};Assert-Learning $httpError 'HTTP404 caught'
$badJson=$false;try{$bad=Invoke-WebRequest -Uri ($BaseUrl+'/bad-json') -TimeoutSec 5;$bad.Content|ConvertFrom-Json}catch{$badJson=$true};Assert-Learning $badJson 'invalid JSON caught'
Write-LearningReport 'powershell-practice' @{rename_targets=$plan.New;expired_owned_files=$expired.Count;whatif_preserved=$true;actual_expired_removed=$true;status500_rows=$errors.Count;sha256=$sha;legacy_md5=$legacy;local_api_site=$api.site;http404_caught=$httpError;bad_json_caught=$badJson;remote_sql_office_and_scheduled_tasks_executed=$false}
}finally{Remove-LearningDirectory $root}
本例需在 Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验
入口文件:projects/powershell-lab/powershell-practice.ps1
预期结果与验证条件观察提示
- 有序log_001/log_002重命名、WhatIf保留文件后真实仅删过期自有文件。
- 日志仅status500计1,不误计URL/字节500;SHA256/旧MD5、HTTP/404/坏JSON核对;远程SQL/Excel不执行。
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联16章) · 含当前章节或实例 · mbskycom202611a525.zip