Check a Powershell Script for syntax errors
I recently had the unfortunate experience of sending off Powershell scripts to a client, only for them to come back because they did not run. A missing double quote was the culprit, but there must be a way to nip the problem in the bud.
The solution came from this great post:
param($path, [switch]$verbose)
if ($verbose) {
$VerbosePreference = ‘Continue’
}
trap { Write-Warning $_; $false; continue }
& `
{
$contents = get-content $path
$contents = [string]::Join([Environment]::NewLine, $contents)
[void]$ExecutionContext.InvokeCommand.NewScriptBlock($contents)
Write-Verbose "Parsed without errors"
$true
}
A simple script to check scripts (who watches the watchmen?). It taks the path of the script to be checked and returns a simple boolean result of the validity of the syntax. True genius. I have definitely added this as a step in my deployment process.
The solution came from this great post:
param($path, [switch]$verbose)
if ($verbose) {
$VerbosePreference = ‘Continue’
}
trap { Write-Warning $_; $false; continue }
& `
{
$contents = get-content $path
$contents = [string]::Join([Environment]::NewLine, $contents)
[void]$ExecutionContext.InvokeCommand.NewScriptBlock($contents)
Write-Verbose "Parsed without errors"
$true
}
A simple script to check scripts (who watches the watchmen?). It taks the path of the script to be checked and returns a simple boolean result of the validity of the syntax. True genius. I have definitely added this as a step in my deployment process.
Comments
Post a Comment