forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_powershell_syntax.ps1
More file actions
44 lines (36 loc) · 1.18 KB
/
check_powershell_syntax.ps1
File metadata and controls
44 lines (36 loc) · 1.18 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
param(
[string[]]$Paths = @("scripts")
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$scriptFiles = @()
foreach ($path in $Paths) {
if (-not (Test-Path -LiteralPath $path)) {
continue
}
$scriptFiles += Get-ChildItem -LiteralPath $path -Recurse -File -Filter '*.ps1'
}
if (-not $scriptFiles -or $scriptFiles.Count -eq 0) {
Write-Host 'No PowerShell scripts found.' -ForegroundColor Yellow
exit 0
}
$hadErrors = $false
foreach ($file in $scriptFiles | Sort-Object FullName -Unique) {
$tokens = $null
$errors = $null
[System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$tokens, [ref]$errors) | Out-Null
if ($errors -and $errors.Count -gt 0) {
$hadErrors = $true
Write-Host "Parse errors in $($file.FullName):" -ForegroundColor Red
foreach ($error in $errors) {
$line = $error.Extent.StartLineNumber
$column = $error.Extent.StartColumnNumber
Write-Host " Line ${line}, Col ${column}: $($error.Message)" -ForegroundColor Red
}
} else {
Write-Host "OK: $($file.FullName)" -ForegroundColor Green
}
}
if ($hadErrors) {
exit 1
}