Created
May 31, 2012 14:25
-
-
Save jpoehls/2843721 to your computer and use it in GitHub Desktop.
PowerShell Exec function from psake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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