# _common.ps1 — 給 steps/*.ps1 dot-source 用的共用 helper # 每個 step.ps1 開頭會:. "$PSScriptRoot\_common.ps1" $ErrorActionPreference = 'Continue' $ProgressPreference = 'SilentlyContinue' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $global:LogFile = 'C:\Windows\Temp\setup.log' if (-not (Test-Path 'C:\Windows\Temp')) { New-Item -ItemType Directory -Path 'C:\Windows\Temp' -Force | Out-Null } function Log { param([string]$msg) $line = '[{0}] {1}' -f (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'), $msg Add-Content -Path $LogFile -Value $line -Encoding UTF8 Write-Host $line } function Section { param([string]$name, [scriptblock]$block) Log "=== $name START ===" try { & $block Log "=== $name DONE ===" } catch { Log "=== $name ERR: $($_.Exception.Message) ===" } } function Set-RegValue { param([string]$Path, [string]$Name, $Value, [string]$Type = 'DWord') if (-not (Test-Path $Path)) { New-Item -Path $Path -Force | Out-Null } New-ItemProperty -Path $Path -Name $Name -Value $Value -PropertyType $Type -Force | Out-Null } function Wait-Network { param([int]$timeoutSec = 60) $i = 0 $max = [int]($timeoutSec / 2) while ($i -lt $max -and -not (Test-Connection 8.8.8.8 -Count 1 -Quiet -ErrorAction SilentlyContinue)) { Start-Sleep 2 $i++ } Log "Network wait: $($i*2)s" }