PowerShell/scripts/check-ps1-file.ps1

34 lines
981 B
PowerShell
Raw Normal View History

2023-10-31 12:05:32 +01:00
<#
2023-05-26 09:27:08 +02:00
.SYNOPSIS
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.
.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
#>
param([string]$filePattern = "")
2023-05-26 09:27:08 +02:00
try {
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
}