-
-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathcheck-ps1-file.ps1
executable file
·33 lines (30 loc) · 975 Bytes
/
check-ps1-file.ps1
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
<#
.SYNOPSIS
Checks PowerShell file(s) for validity
.DESCRIPTION
This PowerShell script checks the given PowerShell script file(s) for validity.
.PARAMETER filePattern
Specifies the file pattern to the PowerShell file(s)
.EXAMPLE
PS> ./check-ps1-file *.ps1
✅ Valid PowerShell in myfile.ps1
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$filePattern = "")
try {
if ($filePattern -eq "" ) { $path = Read-Host "Enter the file pattern to the PowerShell file(s)" }
$files = Get-ChildItem -path "$filePattern" -attributes !Directory
foreach ($file in $files) {
$syntaxError = @()
[void][System.Management.Automation.Language.Parser]::ParseFile($file, [ref]$null, [ref]$syntaxError)
if ("$syntaxError" -ne "") { throw "$syntaxError" }
"✅ Valid PowerShell in $($file.Name)"
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}