Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created May 31, 2012 14:25
Show Gist options
  • Save jpoehls/2843721 to your computer and use it in GitHub Desktop.
Save jpoehls/2843721 to your computer and use it in GitHub Desktop.
PowerShell Exec function from psake
# Taken from psake https://github.com/psake/psake
<#
.SYNOPSIS
This is a helper function that runs a scriptblock and checks the PS variable $lastexitcode
to see if an error occcured. If an error is detected then an exception is thrown.
This function allows you to run command-line programs without having to
explicitly check the $lastexitcode variable.
.EXAMPLE
exec { svn info $repository_trunk } "Error executing SVN. Please verify SVN command-line client is installed"
#>
function Exec
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][scriptblock]$cmd,
[Parameter(Position=1,Mandatory=0)][string]$errorMessage = ($msgs.error_bad_command -f $cmd)
)
& $cmd
if ($lastexitcode -ne 0) {
throw ("Exec: " + $errorMessage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment