mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-22 07:53:21 +01:00
Add check-ps1-file.ps1
This commit is contained in:
parent
4096acc962
commit
9ed577c0d8
32
Scripts/check-ps1-file.ps1
Normal file
32
Scripts/check-ps1-file.ps1
Normal file
@ -0,0 +1,32 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Checks a PowerShell file for validity
|
||||
.DESCRIPTION
|
||||
This PowerShell script checks the given PowerShell file for validity.
|
||||
.PARAMETER pattern
|
||||
Specifies the file pattern to the PowerShell file(s) to check
|
||||
.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]$pattern = "")
|
||||
|
||||
try {
|
||||
if ($pattern -eq "" ) { $path = Read-Host "Enter the file pattern to the PowerShell file(s)" }
|
||||
$files = Get-ChildItem $pattern
|
||||
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"
|
||||
}
|
||||
exit 0 # success
|
||||
} catch {
|
||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||
exit 1
|
||||
}
|
Loading…
Reference in New Issue
Block a user