PowerShell/Docs/check-ps1-file.md
2023-05-26 12:20:18 +02:00

2.0 KiB
Raw Blame History

The check-ps1-file.ps1 Script

This PowerShell script checks the given PowerShell file(s) for validity.

Parameters

/home/mf/Repos/PowerShell/Scripts/check-ps1-file.ps1 [[-filePattern] <String>] [<CommonParameters>]

-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.

Example

PS> ./check-ps1-file *.ps1
 Valid PowerShell in myfile.ps1

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Source Code

<#
.SYNOPSIS
	Checks PowerShell file(s) for validity
.DESCRIPTION
	This PowerShell script checks the given PowerShell file(s) for validity.
.PARAMETER filePattern
	Specifies the file pattern to the PowerShell file(s)
.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 = "")

try {
	if ($filePattern -eq "" ) { $path = Read-Host "Enter the file pattern to the PowerShell file(s)" }
	$files = Get-ChildItem $filePattern
	foreach ($file in $files) {
		$syntaxError = @()
		[void][System.Management.Automation.Language.Parser]::ParseFile($file, [ref]$null, [ref]$syntaxError)
		if ("$syntaxError" -ne "") { throw "$syntaxError" }
		$basename = (Get-Item "$file").Basename
		"✔️ Valid PowerShell in $basename"
	}
	exit 0 # success
} catch {
	"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
	exit 1
}

Generated by convert-ps2md.ps1 using the comment-based help of check-ps1-file.ps1