2023-05-26 09:27:08 +02:00
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
2023-05-26 11:55:16 +02:00
|
|
|
|
Checks PowerShell file(s) for validity
|
2023-05-26 09:27:08 +02:00
|
|
|
|
.DESCRIPTION
|
2023-05-30 11:31:59 +02:00
|
|
|
|
This PowerShell script checks the given PowerShell script file(s) for validity.
|
2023-05-26 11:55:16 +02:00
|
|
|
|
.PARAMETER filePattern
|
|
|
|
|
Specifies the file pattern to the PowerShell file(s)
|
2023-05-26 09:27:08 +02:00
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS> ./check-ps1-file *.ps1
|
|
|
|
|
✔️ Valid PowerShell in myfile.ps1
|
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
.NOTES
|
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
|
#>
|
|
|
|
|
|
2023-05-26 11:55:16 +02:00
|
|
|
|
param([string]$filePattern = "")
|
2023-05-26 09:27:08 +02:00
|
|
|
|
|
|
|
|
|
try {
|
2023-05-26 11:55:16 +02:00
|
|
|
|
if ($filePattern -eq "" ) { $path = Read-Host "Enter the file pattern to the PowerShell file(s)" }
|
2023-05-30 11:31:59 +02:00
|
|
|
|
|
|
|
|
|
$files = Get-ChildItem -path "$filePattern" -attributes !Directory
|
2023-05-26 09:27:08 +02:00
|
|
|
|
foreach ($file in $files) {
|
|
|
|
|
$syntaxError = @()
|
|
|
|
|
[void][System.Management.Automation.Language.Parser]::ParseFile($file, [ref]$null, [ref]$syntaxError)
|
|
|
|
|
if ("$syntaxError" -ne "") { throw "$syntaxError" }
|
2023-05-30 11:31:59 +02:00
|
|
|
|
"✔️ Valid PowerShell in $($file.Name)"
|
2023-05-26 09:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
exit 0 # success
|
|
|
|
|
} catch {
|
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|