I'm using PowerShell v4 to call the svn command line tool and can't figure out of way carrying out basic error handling. I've tried capturing the results any svn command to a variable, using try-catch blocks, even trapping the exception doesn't work
The following checkout intentionally generates an 'Authentication failed' error in the console:
trap [SystemException]
{
Write-Output "ERROR: $_";exit 1
}
try
{
$log = svn.exe checkout $Url $Path --no-auth-cache --non-interactive --username "username" --password "Password"
}
catch
{
write-host "$_"
}
In the console pane, the error is displayed:
svn.exe : svn: E215004: Authentication failed and interactive prompting is disabled; see the --force-interactive option At C:\\dev\\Temp\\script.ps1:30 char:8 + $log = svn.exe checkout $Url $Path --no-auth-cache --non-interactive --username ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (svn: E215004: A...eractive option:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError svn: E215004: Unable to connect to a repository at URL ' http://source.com/ ' Authentication failed
The $log
variable is never assigned the above output, the try-catch block and generic trap are both ignored.
Surely someone out there has automated some SVN before, and how effective can automation be without even basic error handling?
Couple of easy options, depending on what you want to do.
If you want to capture the output of the command, use Invoke-Expression
:
$log = Invoke-Expression "svn.exe checkout $Url $Path --no-auth-cache --non-interactive --username `"username`" --password `"Password`""
If you want to check if the command was successful, use $?
svn.exe checkout $Url $Path --no-auth-cache --non-interactive --username "username" --password "Password" | out-null
echo $?
You could also look at using the Start-Process
cmdlet or even use a System.Diagnostics.Process
object to get more control over the process execution.
The solution I finally went with didn't include a checkout as TeamCity does this automatically.
The following is how the final script checks if the currrent PowerShell session is authenticated to SVN and if the $PSScriptRoot
script directory is an SVN working copy:
svn info "$PSScriptRoot" 2>&1 | Tee-Object -Variable svnResult
if (($svnResult -notmatch '\d{6}:'))
{
write-host "`nVerified the connection to SVN"
}
else
{
throw "`nAn error occurred verifying the connection to SVN"
}
2>&1
redirects all errors to the standard output steam, then Tee-Object
cmdlet sends the output the $svnResult
variable.
As all SVN error messages contain 6 digits followed by a colon, the \\d{6}:
regex checks the $svnResult
variable for an occurrence of this, if it's found, an error is thrown.
More information on PowerShell redirection here:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.