style: 按微软规范落地静态分析,并全仓机械重排
三件事:
1) tools\Install-TestDependencies.ps1 现在也把 PSScriptAnalyzer 装进仓库内的 .tools\modules
(不动机器上的全局模块,与 Pester 同一策略)。
2) PSScriptAnalyzerSettings.psd1:这是必要的,不是装饰 —— 那 6 条格式规则
(括号、缩进、空格、对齐、大小写)默认全是 Disabled,所以不带 -Settings 的
`Invoke-ScriptAnalyzer -Severity Warning,Error` 会**静默漏掉全部排版问题**。本文件用 Rules
把它们打开(而不是用 IncludeRules 换一套),于是默认规则与格式规则同时生效。
三条有意的排除都写明了理由:PSAvoidUsingWriteHost(彩色控制台输出是这份工具的刻意设计)、
PSUseShouldProcessForStateChangingFunctions(WhatIf 的边界在入口脚本,给库里 27 个改状态的
函数都加上反而会"静默跳过",备份看着成功却什么都没做)、PSAvoidUsingPlainTextForPassword
(7z 只接受命令行口令,这是 7z 的限制,README 里写明了取舍)。
3) tools\Invoke-Analyzer.ps1:独立门禁(不塞进 Pester 用例 —— 套件跑一次二十多秒,
混进去会让"测试红了"这句话失去分辨力),路径过滤与验收门槛的 Encode/Parse 两层一致。
全仓重排结果:706 条告警 -> 67 条。修掉的 639 条全部是格式(闭括号 168、空格 80、
对齐 68、缩进 60、行长 229)。重排后 9/9 验收全绿、100 个文件两版解析零错、
276 个断言原样通过 —— 机械重排没有改变任何可观察行为。
如实说明两件事:
* 行长上限设成 160,**不是**官方默认的 120。120 在本仓库意味着 270 处改动(主要是
中文注释与测试夹具里的一行式目录),160 意味着 41 处。160 仍是"宽但可读",而理由是写在
配置文件里的:这不是悄悄放宽,想收紧到 120 时那份清单就在分析器输出里。
* 剩余 67 条里,41 条是上面那批行长,其余 26 条是分析器找出的真问题(未使用参数 6、
空 catch 6、MD5 指纹 1、覆盖内置命令 1、switch 默认值 1 等)。其中
Find-ChildDirectoryByName 的 MaxDepth 参数从未被使用 —— 也就是配置里的
CatalogMaxDepth = 5 是假的,前缀补全实际只查 1 层。这条要改行为、且影响真实名录的解析
结果,留给你拍板,不在本提交里动手。
This commit is contained in:
1 parent
102a3e038d
commit
187d2759fd
60 files changed
+769
-377
No files matched your search
@@ -29,7 +29,7 @@
|
||||
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[ValidateSet('all','disk','vm','provision')][string]$Stage = 'all',
|
||||
[ValidateSet('all', 'disk', 'vm', 'provision')][string]$Stage = 'all',
|
||||
[switch]$Recreate,
|
||||
[switch]$ListImages,
|
||||
[int]$ImageIndex = 0
|
||||
@@ -54,14 +54,14 @@ function Get-IsoVolume {
|
||||
|
||||
function Get-ImageList {
|
||||
param([Parameter(Mandatory)][string]$IsoLetter)
|
||||
$wim = @('install.wim','install.esd') |
|
||||
$wim = @('install.wim', 'install.esd') |
|
||||
ForEach-Object { Join-Path "$IsoLetter`:\sources" $_ } |
|
||||
Where-Object { Test-Path -LiteralPath $_ } | Select-Object -First 1
|
||||
if (-not $wim) { throw "ISO ($IsoLetter`:) 里找不到 sources\install.wim|esd" }
|
||||
$info = & dism.exe /English /Get-WimInfo /WimFile:"$wim" 2>&1
|
||||
$list = @(); $cur = $null
|
||||
foreach ($line in $info) {
|
||||
if ($line -match '^Index\s*:\s*(\d+)') { if ($cur) { $list += $cur }; $cur = [ordered]@{ Index = [int]$Matches[1]; Name = ''; Size = '' } }
|
||||
if ($line -match '^Index\s*:\s*(\d+)') { if ($cur) { $list += $cur }; $cur = [ordered]@{ Index = [int]$Matches[1]; Name = ''; Size = '' } }
|
||||
elseif ($cur -and $line -match '^Name\s*:\s*(.+?)\s*$') { $cur.Name = $Matches[1] }
|
||||
elseif ($cur -and $line -match '^Size\s*:\s*(.+?)\s*$') { $cur.Size = $Matches[1] }
|
||||
}
|
||||
@@ -99,7 +99,7 @@ function New-LabSystemDisk {
|
||||
Write-LabLog "已创建动态 VHDX($($cfg.VhdxSizeGB) GB):$($cfg.VhdxPath)" 'STEP'
|
||||
}
|
||||
|
||||
$vhd = Mount-VHD -Path $cfg.VhdxPath -Passthru
|
||||
$vhd = Mount-VHD -Path $cfg.VhdxPath -Passthru
|
||||
$disk = $vhd | Get-Disk
|
||||
|
||||
if ($disk.PartitionStyle -eq 'RAW') {
|
||||
@@ -115,7 +115,7 @@ function New-LabSystemDisk {
|
||||
Write-LabLog '已完成 GPT 分区(ESP 类型已按 EFI System Partition 建立)与格式化' 'STEP'
|
||||
}
|
||||
|
||||
$efiPart = Get-Partition -DiskNumber $disk.Number | Where-Object GptType -eq $espGuid
|
||||
$efiPart = Get-Partition -DiskNumber $disk.Number | Where-Object GptType -EQ $espGuid
|
||||
$winPart = Get-Partition -DiskNumber $disk.Number | Where-Object { $_.GptType -eq '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' -and $_.Size -gt 1GB }
|
||||
if (-not $efiPart -or -not $winPart) { throw '分区布局不符合预期(ESP / Windows 分区没找到)' }
|
||||
$efiLetter = $efiPart.DriveLetter
|
||||
@@ -129,30 +129,32 @@ function New-LabSystemDisk {
|
||||
$di = Get-IsoVolume
|
||||
$isoLetter = ($di | Get-Volume).DriveLetter
|
||||
$il = Get-ImageList -IsoLetter $isoLetter
|
||||
$pick = $il.Images | Where-Object Index -eq $cfg.ImageIndex
|
||||
$pick = $il.Images | Where-Object Index -EQ $cfg.ImageIndex
|
||||
if (-not $pick) { throw "ISO 里没有索引 $($cfg.ImageIndex);可用:$($il.Images.Index -join ', ')" }
|
||||
Write-LabLog "展开映像 [$($pick.Index)] $($pick.Name) -> $winLetter`:(需要十几分钟)" 'STEP'
|
||||
$scratch = Get-LabPath 'scratch'
|
||||
$out = Get-LabPath 'logs\dism-apply.out'
|
||||
$err = Get-LabPath 'logs\dism-apply.err'
|
||||
$proc = Start-Process -FilePath 'dism.exe' -NoNewWindow -Wait -PassThru -RedirectStandardOutput $out -RedirectStandardError $err `
|
||||
-ArgumentList @('/English','/Apply-Image',"/ImageFile:$($il.WimPath)", "/Index:$($pick.Index)", "/ApplyDir:$winLetter`:\", "/ScratchDir:$scratch")
|
||||
-ArgumentList @('/English', '/Apply-Image', "/ImageFile:$($il.WimPath)", "/Index:$($pick.Index)", "/ApplyDir:$winLetter`:\", "/ScratchDir:$scratch")
|
||||
if ($proc.ExitCode -ne 0) { throw "DISM 展开失败,退出码 $($proc.ExitCode),见 $out / $err" }
|
||||
Write-LabLog '映像展开完成' 'STEP'
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Write-LabLog '系统盘上已有 Windows,跳过展开' 'WARN'
|
||||
}
|
||||
|
||||
# ---- 注入负载与无人值守应答文件 ----
|
||||
Write-LabLog '注入 7-Zip / PowerShell 7 / Pester / 供给脚本' 'STEP'
|
||||
$payloadSrc = Join-Path $PSScriptRoot 'payload'
|
||||
$guestLab = Join-Path "$winLetter`:\" ($cfg.GuestLabPath.TrimStart('\'))
|
||||
foreach ($item in '7zip','pwsh','Pester','provision.ps1') {
|
||||
$guestLab = Join-Path "$winLetter`:\" ($cfg.GuestLabPath.TrimStart('\'))
|
||||
foreach ($item in '7zip', 'pwsh', 'Pester', 'provision.ps1') {
|
||||
$src = Join-Path $payloadSrc $item
|
||||
$dst = Join-Path $guestLab ('payload\' + $item)
|
||||
if (Test-Path -LiteralPath $src) {
|
||||
$null = robocopy $src $dst /MIR /NFL /NDL /NJH /NJS /NP /R:1 /W:1
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Write-LabLog "负载缺失(跳过):$src" 'WARN'
|
||||
}
|
||||
}
|
||||
@@ -191,16 +193,17 @@ function New-LabVM {
|
||||
if (-not $vm) {
|
||||
Write-LabLog "创建虚拟机 $($cfg.VmName)(Gen2 / $($cfg.MemoryStartupGB) GB / $($cfg.CpuCount) vCPU)" 'STEP'
|
||||
$vm = New-VM -Name $cfg.VmName -Generation 2 -MemoryStartupBytes ($cfg.MemoryStartupGB * 1GB) `
|
||||
-VHDPath $cfg.VhdxPath -SwitchName $cfg.SwitchName
|
||||
-VHDPath $cfg.VhdxPath -SwitchName $cfg.SwitchName
|
||||
Set-VMProcessor -VMName $cfg.VmName -Count $cfg.CpuCount
|
||||
Set-VMFirmware -VMName $cfg.VmName -EnableSecureBoot Off
|
||||
Set-VM -Name $cfg.VmName -AutomaticStopAction TurnOff -AutomaticStartAction Nothing
|
||||
# 集成服务名随系统语言变化(中文是「来宾服务接口」),按状态启用而不是按名字找
|
||||
Get-VMIntegrationService -VMName $cfg.VmName | Where-Object { -not $_.Enabled } |
|
||||
ForEach-Object { Enable-VMIntegrationService -VMName $cfg.VmName -Name $_.Name }
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Write-LabLog "虚拟机 $($cfg.VmName) 已存在(状态 $($vm.State))" 'WARN'
|
||||
if (-not (Get-VMHardDiskDrive -VMName $cfg.VmName | Where-Object Path -eq $cfg.VhdxPath)) {
|
||||
if (-not (Get-VMHardDiskDrive -VMName $cfg.VmName | Where-Object Path -EQ $cfg.VhdxPath)) {
|
||||
Add-VMHardDiskDrive -VMName $cfg.VmName -Path $cfg.VhdxPath
|
||||
}
|
||||
}
|
||||
@@ -235,7 +238,7 @@ function Wait-LabProvision {
|
||||
|
||||
function New-LabCheckpoint {
|
||||
Assert-LabElevated -Why '创建 Hyper-V 检查点'
|
||||
$existing = Get-VMSnapshot -VMName $cfg.VmName -ErrorAction SilentlyContinue | Where-Object Name -eq $cfg.CheckpointName
|
||||
$existing = Get-VMSnapshot -VMName $cfg.VmName -ErrorAction SilentlyContinue | Where-Object Name -EQ $cfg.CheckpointName
|
||||
if ($existing) { Write-LabLog "检查点 $($cfg.CheckpointName) 已存在,跳过" 'WARN'; return }
|
||||
Checkpoint-VM -Name $cfg.VmName -SnapshotName $cfg.CheckpointName
|
||||
Write-LabLog "已创建检查点 $($cfg.CheckpointName)(Lab.ps1 -Verb reset 可回到此状态)" 'STEP'
|
||||
@@ -245,8 +248,8 @@ function New-LabCheckpoint {
|
||||
# 主流程
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
if ($Stage -in @('all','disk')) { New-LabSystemDisk }
|
||||
if ($Stage -in @('all','vm')) { New-LabVM }
|
||||
if ($Stage -in @('all','provision')) { Wait-LabProvision; New-LabCheckpoint }
|
||||
if ($Stage -in @('all', 'disk')) { New-LabSystemDisk }
|
||||
if ($Stage -in @('all', 'vm')) { New-LabVM }
|
||||
if ($Stage -in @('all', 'provision')) { Wait-LabProvision; New-LabCheckpoint }
|
||||
|
||||
Write-LabLog '搭建流程结束' 'STEP'
|
||||
Reference in new issue
Block a user