PowerShell/docs/check-ps1-file.md

80 lines
2.0 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *check-ps1-file.ps1* Script
===========================
2023-05-26 12:20:18 +02:00
2023-07-29 09:45:37 +02:00
This PowerShell script checks the given PowerShell script file(s) for validity.
2023-05-26 12:20:18 +02:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2023-05-26 12:20:18 +02:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/check-ps1-file.ps1 [[-filePattern] <String>] [<CommonParameters>]
2023-05-26 12:20:18 +02:00
-filePattern <String>
Specifies the file pattern to the PowerShell file(s)
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2023-05-26 12:20:18 +02:00
```powershell
PS> ./check-ps1-file *.ps1
2024-11-08 12:35:11 +01:00
✅ Valid PowerShell in myfile.ps1
2023-05-26 12:20:18 +02:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2023-05-26 12:20:18 +02:00
Author: Markus Fleschutz | License: CC0
2023-07-29 10:04:38 +02:00
Related Links
-------------
2023-05-26 12:20:18 +02:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2023-05-26 12:20:18 +02:00
```powershell
<#
.SYNOPSIS
Checks PowerShell file(s) for validity
.DESCRIPTION
2023-07-29 09:45:37 +02:00
This PowerShell script checks the given PowerShell script file(s) for validity.
2023-05-26 12:20:18 +02:00
.PARAMETER filePattern
Specifies the file pattern to the PowerShell file(s)
.EXAMPLE
PS> ./check-ps1-file *.ps1
2024-11-08 12:35:11 +01:00
✅ Valid PowerShell in myfile.ps1
2023-05-26 12:20:18 +02:00
.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)" }
2023-07-29 09:45:37 +02:00
$files = Get-ChildItem -path "$filePattern" -attributes !Directory
2023-05-26 12:20:18 +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-11-08 12:35:11 +01:00
"✅ Valid PowerShell in $($file.Name)"
2023-05-26 12:20:18 +02:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:51)*