PowerShell/scripts/check-ps1-file.ps1

34 lines
975 B
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02: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
2024-10-01 13:37:53 +02:00
Valid PowerShell in myfile.ps1
2023-05-26 09:27:08 +02:00
.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" }
2024-10-01 13:37:53 +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
}