-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSIX.AutoFixLoop.ps1
More file actions
360 lines (294 loc) · 14.1 KB
/
Copy pathMSIX.AutoFixLoop.ps1
File metadata and controls
360 lines (294 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# =============================================================================
# Multi-pass remediation pipeline — Invoke-MsixAutoFixLoop
# -----------------------------------------------------------------------------
# Automates the "fix → re-analyse → next fix" cycle. Many MSIX issues are
# chained: fix working dir → reveals DLL issue → fix DLL → reveals updater.
# The module already ships every building block; this stitches them into a
# controlled loop:
#
# Pass N:
# 1. Get-MsixCompatibilityReport (static + optional trace).
# 2. Invoke-MsixAutoFixFromAnalysis -DryRun to produce a plan.
# 3. Stop if the plan is empty (NoNewFixes).
# 4. Apply the plan (no sign yet).
# 5. If -CaptureTrace: run Invoke-MsixProcMonCapture and compare via
# Compare-MsixTrace; stop if Introduced==0 (NoRegressions).
# 6. Loop.
#
# After all passes: sign exactly once.
#
# Per-pass artefacts (report.json, plan.json, optional trace.pml, trace-delta.json)
# are written under $env:TEMP\msix-autofix-loop-<guid>\ so operators can
# post-mortem any pass.
# =============================================================================
function Invoke-MsixAutoFixLoop {
<#
.SYNOPSIS
Runs repeated static-analysis + auto-fix passes until the package is
stable or the maximum pass count is reached.
.DESCRIPTION
Each pass runs Get-MsixCompatibilityReport against the current package
state, plans the next round of fixes via Invoke-MsixAutoFixFromAnalysis,
and applies them (unsigned). The loop continues until:
NoNewFixes — the planner has nothing to do (stable).
NoRegressions — Compare-MsixTrace shows no newly-introduced failures
(only meaningful when -CaptureTrace is set; requires
Procmon to be available via Initialize-MsixToolchain).
Both stop conditions can be combined in -StopOn. The package is signed
exactly once at the end of the last pass (unless -SkipSigning is set).
Per-pass artefacts (report.json, plan.json, trace.pml, delta.json) are
kept under:
$env:TEMP\msix-autofix-loop-<runId>\pass-N\
so operators can post-mortem any pass without losing intermediate state.
-WhatIf / -DryRun performs only the first pass's plan and exits without
writing anything.
.PARAMETER PackagePath
The .msix to act on.
.PARAMETER MaxPasses
Hard cap on pass count (default 5). Prevents runaway loops.
.PARAMETER StopOn
One or more stop conditions:
NoNewFixes — stop when the planner has nothing new to apply.
NoRegressions — stop when Compare-MsixTrace detects no introduced
failures relative to the previous pass.
Default: NoNewFixes.
.PARAMETER MinConfidence
Confidence floor forwarded to Invoke-MsixAutoFixFromAnalysis (default 0.85).
.PARAMETER CaptureTrace
When set, each pass installs the package in the MSIX Sandbox, captures
a ProcMon trace, and feeds it into Compare-MsixTrace for the
NoRegressions stop condition. Requires Hyper-V + Sandbox + ProcMon.
.PARAMETER TraceDurationSeconds
How long to capture the ProcMon trace per pass (default 30 seconds).
.PARAMETER TraceLogPath
Path to an existing TraceFixup .log/.txt to feed into each pass's
compatibility report instead of running a live capture. Incompatible
with -CaptureTrace.
.PARAMETER OutputPath
Write the final fixed package here. Defaults to overwriting PackagePath.
.PARAMETER DryRun
Run only the first pass planner and print the plan; do not write.
.PARAMETER SkipSigning
Signing controls for the final sign-once call.
# --- Auto-fix pass-through parameters (forwarded each pass) ---
.PARAMETER NoSign
Signing controls for the final sign-once call.
# --- Auto-fix pass-through parameters (forwarded each pass) ---
.PARAMETER Pfx
Signing controls for the final sign-once call.
# --- Auto-fix pass-through parameters (forwarded each pass) ---
.PARAMETER PfxPassword
Signing controls for the final sign-once call.
# --- Auto-fix pass-through parameters (forwarded each pass) ---
.PARAMETER VcRuntimeSourceFolder
Forwarded to Invoke-MsixAutoFixFromAnalysis.
.PARAMETER StartupTaskAppId
Forwarded to Invoke-MsixAutoFixFromAnalysis.
.PARAMETER StartupTaskName
Forwarded to Invoke-MsixAutoFixFromAnalysis.
.PARAMETER LoaderPaths
Forwarded to Invoke-MsixAutoFixFromAnalysis.
.PARAMETER IgnoreUpdaters
.PARAMETER IgnorePluginDirectories
.PARAMETER LegacyPluginFix
.PARAMETER IgnoreNestedPackages
Forwarded to Invoke-MsixAutoFixFromAnalysis.
.PARAMETER PreferManifestOverPsf
Forwarded to Invoke-MsixAutoFixFromAnalysis.
.OUTPUTS
[pscustomobject] @{
Output [string] path to the final (signed) package
Passes [object[]] per-pass summary objects
FinalReport [pscustomobject] compatibility report from the last pass
SignedOk [bool]
RunDirectory[string] path to per-pass artefacts
}
.EXAMPLE
Invoke-MsixAutoFixLoop -PackagePath app.msix -MaxPasses 5 `
-Pfx cert.pfx -PfxPassword $pw
.EXAMPLE
# With sandbox trace capture:
Invoke-MsixAutoFixLoop -PackagePath app.msix `
-StopOn NoNewFixes,NoRegressions -CaptureTrace `
-TraceDurationSeconds 30 `
-Pfx cert.pfx -PfxPassword $pw -OutputPath app-fixed.msix
.EXAMPLE
# DryRun — only plan pass 1, do not write:
Invoke-MsixAutoFixLoop -PackagePath app.msix -DryRun
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
[string]$PackagePath,
[ValidateRange(1, 20)]
[int]$MaxPasses = 5,
[ValidateSet('NoNewFixes', 'NoRegressions')]
[string[]]$StopOn = @('NoNewFixes'),
[ValidateRange(0.0, 1.0)]
[double]$MinConfidence = 0.85,
# Trace / sandbox options
[switch]$CaptureTrace,
[int]$TraceDurationSeconds = 30,
[string]$TraceLogPath,
# Output
[string]$OutputPath,
[switch]$DryRun,
[Alias('NoSign')] [switch]$SkipSigning,
[string]$Pfx,
[SecureString]$PfxPassword,
# Pass-through to Invoke-MsixAutoFixFromAnalysis
[string]$VcRuntimeSourceFolder,
[string]$StartupTaskAppId,
[string]$StartupTaskName,
[string[]]$LoaderPaths,
[switch]$IgnoreUpdaters,
[switch]$IgnorePluginDirectories,
[switch]$LegacyPluginFix,
[switch]$IgnoreNestedPackages,
[bool]$PreferManifestOverPsf = $true
)
if ($CaptureTrace -and $TraceLogPath) {
throw '-CaptureTrace and -TraceLogPath are mutually exclusive.'
}
# Per-run working directory
$runId = [guid]::NewGuid().ToString('N').Substring(0, 12)
$runDir = Join-Path -Path $env:TEMP -ChildPath "msix-autofix-loop-$runId"
$null = New-Item -ItemType Directory -Path $runDir -Force
# Determine the working copy path.
$targetPath = if ($OutputPath -and $OutputPath -ne $PackagePath) {
if (-not $DryRun) {
Copy-Item -LiteralPath $PackagePath -Destination $OutputPath -Force
}
$OutputPath
} else {
$PackagePath
}
Write-MsixLog -Level Info -Message ("AutoFixLoop started: runId={0} maxPasses={1} stopOn={2}" `
-f $runId, $MaxPasses, ($StopOn -join ','))
Write-MsixLog -Level Info -Message "AutoFixLoop artefacts: $runDir"
# --- Build the static arg hashtable for Invoke-MsixAutoFixFromAnalysis ---
$fixArgs = @{
MinConfidence = $MinConfidence
PreferManifestOverPsf = $PreferManifestOverPsf
SkipSigning = $true # sign once at end
}
if ($VcRuntimeSourceFolder) { $fixArgs['VcRuntimeSourceFolder'] = $VcRuntimeSourceFolder }
if ($StartupTaskAppId) { $fixArgs['StartupTaskAppId'] = $StartupTaskAppId }
if ($StartupTaskName) { $fixArgs['StartupTaskName'] = $StartupTaskName }
if ($LoaderPaths) { $fixArgs['LoaderPaths'] = $LoaderPaths }
if ($IgnoreUpdaters) { $fixArgs['IgnoreUpdaters'] = $true }
if ($IgnorePluginDirectories){ $fixArgs['IgnorePluginDirectories'] = $true }
if ($LegacyPluginFix) { $fixArgs['LegacyPluginFix'] = $true }
if ($IgnoreNestedPackages) { $fixArgs['IgnoreNestedPackages'] = $true }
$passSummaries = [System.Collections.Generic.List[object]]::new()
$finalReport = $null
$prevTracePath = $null
$stopReason = $null
for ($pass = 1; $pass -le $MaxPasses; $pass++) {
Write-MsixLog -Level Info -Message "AutoFixLoop pass $pass / $MaxPasses"
$passDir = Join-Path -Path $runDir -ChildPath "pass-$pass"
$null = New-Item -ItemType Directory -Path $passDir -Force
# ── 1. Compatibility report ──
$reportArgs = @{ PackagePath = $targetPath }
if ($TraceLogPath) { $reportArgs['TraceLogPath'] = $TraceLogPath }
$report = Get-MsixCompatibilityReport @reportArgs
$finalReport = $report
# Persist report for post-mortem
$report | ConvertTo-Json -Depth 10 -Compress |
Out-File -FilePath (Join-Path -Path $passDir -ChildPath 'report.json') -Encoding utf8
# ── 2. Plan ──
$plan = Invoke-MsixAutoFixFromAnalysis -Report $report @fixArgs -DryRun
$plan | ConvertTo-Json -Depth 10 -Compress |
Out-File -FilePath (Join-Path -Path $passDir -ChildPath 'plan.json') -Encoding utf8
$passSummary = [pscustomobject]@{
Pass = $pass
FindingCount = @($report.Findings).Count
StageCount = if ($plan) { @($plan).Count } else { 0 }
TraceDelta = $null
StopReason = $null
ArtifactPath = $passDir
}
# ── DryRun: only first pass plan, then stop ──
if ($DryRun) {
Write-MsixLog -Level Info -Message '[DryRun] Pass 1 plan produced - exiting without writing.'
$passSummary.StopReason = 'DryRun'
$passSummaries.Add($passSummary)
break
}
# ── 3. Stop: NoNewFixes ──
if ('NoNewFixes' -in $StopOn -and $passSummary.StageCount -eq 0) {
Write-MsixLog -Level Info -Message "AutoFixLoop stopping: no new fixes planned (pass $pass)."
$passSummary.StopReason = 'NoNewFixes'
$stopReason = 'NoNewFixes'
$passSummaries.Add($passSummary)
break
}
# ── 4. Apply ──
if ($PSCmdlet.ShouldProcess($targetPath, "AutoFix pass $pass")) {
Invoke-MsixAutoFixFromAnalysis -Report $report -PackagePath $targetPath @fixArgs |
Out-Null
}
Write-MsixLog -Level Info -Message "AutoFixLoop pass $pass applied."
# ── 5. Optional trace capture + delta ──
if ($CaptureTrace -and 'NoRegressions' -in $StopOn) {
$tracePath = Join-Path -Path $passDir -ChildPath 'trace.pml'
Write-MsixLog -Level Info -Message "AutoFixLoop: capturing trace ($TraceDurationSeconds s)..."
try {
Invoke-MsixProcMonCapture -PackagePath $targetPath -OutputPml $tracePath `
-DurationSeconds $TraceDurationSeconds
} catch {
Write-MsixLog -Level Warning -Message "AutoFixLoop: trace capture failed on pass $pass - $_"
}
if ($prevTracePath -and (Test-Path -LiteralPath $tracePath)) {
$delta = Compare-MsixTrace -Baseline $prevTracePath -Candidate $tracePath
$delta | ConvertTo-Json -Depth 10 -Compress |
Out-File -FilePath (Join-Path -Path $passDir -ChildPath 'trace-delta.json') -Encoding utf8
$passSummary.TraceDelta = $delta.Summary
if ('NoRegressions' -in $StopOn -and $delta.Summary.IntroducedCount -eq 0) {
Write-MsixLog -Level Info -Message "AutoFixLoop stopping: no regressions introduced (pass $pass)."
$passSummary.StopReason = 'NoRegressions'
$stopReason = 'NoRegressions'
$passSummaries.Add($passSummary)
$prevTracePath = $tracePath
break
}
}
$prevTracePath = $tracePath
}
$passSummaries.Add($passSummary)
if ($pass -eq $MaxPasses) {
Write-MsixLog -Level Warning -Message "AutoFixLoop: reached MaxPasses ($MaxPasses) without a stop condition."
$stopReason = 'MaxPasses'
}
}
# ── 6. Sign once ──
$signedOk = $false
if (-not $DryRun) {
if (-not $SkipSigning -and $Pfx) {
try {
if ($PSCmdlet.ShouldProcess($targetPath, 'Sign package')) {
Invoke-MsixSigning -PackagePath $targetPath -Pfx $Pfx -PfxPassword $PfxPassword
$signedOk = $true
Write-MsixLog -Level Info -Message "AutoFixLoop: package signed - $targetPath"
}
} catch {
Write-MsixLog -Level Warning -Message "AutoFixLoop: signing failed - $_"
}
} elseif (-not $SkipSigning -and -not $Pfx) {
Write-MsixLog -Level Warning -Message 'AutoFixLoop: no -Pfx supplied - package left unsigned.'
} else {
Write-MsixLog -Level Info -Message 'AutoFixLoop: skipping signing (-SkipSigning).'
}
}
$stopReasonStr = if ($null -ne $stopReason) { $stopReason } else { 'MaxPasses' }
Write-MsixLog -Level Info -Message ("AutoFixLoop complete: passes={0} stopReason={1} output={2}" `
-f $passSummaries.Count, $stopReasonStr, $targetPath)
return [pscustomobject]@{
Output = $targetPath
Passes = [object[]]$passSummaries
FinalReport = $finalReport
SignedOk = $signedOk
RunDirectory = $runDir
StopReason = $stopReason
}
}