-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun-PSScriptAnalyzer.ps1
More file actions
75 lines (61 loc) · 2.48 KB
/
Run-PSScriptAnalyzer.ps1
File metadata and controls
75 lines (61 loc) · 2.48 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
# Run-PSScriptAnalyzer.ps1
#
# .SYNOPSIS
# Runs PSScriptAnalyzer on the project's main script and reports issues.
#
# .DESCRIPTION
# This utility script installs PSScriptAnalyzer (if missing) and runs it against
# Shrink-DockerDataVHDX.ps1. It reports errors, warnings, and informational messages.
#
# .EXAMPLE
# .\Run-PSScriptAnalyzer.ps1
#
[CmdletBinding()]
param()
# Check if PSScriptAnalyzer is installed
if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) {
Write-Warning "PSScriptAnalyzer module not found."
$confirm = Read-Host "Do you want to install it now? (Y/N)"
if ($confirm -eq 'Y') {
Write-Host "Installing PSScriptAnalyzer..." -ForegroundColor Cyan
Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser -SkipPublisherCheck
} else {
Write-Error "PSScriptAnalyzer is required to run this check."
exit 1
}
}
$targetScript = ".\Shrink-DockerDataVHDX.ps1"
if (-not (Test-Path $targetScript)) {
Write-Error "Target script not found: $targetScript"
exit 1
}
Write-Host "Running PSScriptAnalyzer on $targetScript..." -ForegroundColor Cyan
Write-Host "===========================================================" -ForegroundColor Cyan
# Run the analyzer (exclude PSAvoidUsingWriteHost - Write-Host is intentionally used for user-facing messages)
$results = Invoke-ScriptAnalyzer -Path $targetScript -Recurse -ExcludeRule PSAvoidUsingWriteHost
# Display results
if ($results) {
$results | Select-Object RuleName, Severity, Line, Message | Format-Table -AutoSize
Write-Host "===========================================================" -ForegroundColor Cyan
# Summary
$errors = ($results | Where-Object { $_.Severity -like '*Error*' }).Count
$warnings = ($results | Where-Object { $_.Severity -like '*Warning*' }).Count
$infos = ($results | Where-Object { $_.Severity -like '*Information*' }).Count
Write-Host "Summary:" -ForegroundColor Yellow
if ($errors -gt 0) {
Write-Host " Errors: $errors" -ForegroundColor Red
} else {
Write-Host " Errors: $errors" -ForegroundColor Green
}
if ($warnings -gt 0) {
Write-Host " Warnings: $warnings" -ForegroundColor Yellow
} else {
Write-Host " Warnings: $warnings" -ForegroundColor Green
}
Write-Host " Information: $infos"
if ($errors -gt 0) {
exit 1
}
} else {
Write-Host "No issues found! The script is clean." -ForegroundColor Green
}