I`m trying to automate a branching operation using a Powershell script like the following:
$fromUrl = 'svn://some/from/url'
$toUrl = 'svn://some/to/url.'
[Array]$arguments = "copy", "-q", $fromUrl, $toUrl, "-m", "New branch"
echo "Branching from: " $fromUrl
echo "Branching to: " $toUrl
$copyResult = & svn $arguments
echo "Result: " $copyResult
echo "Switching to: " $toUrl
$switchResult = svn switch -q $toUrl ho-til-test
echo "Result: " $switchResult
As long as the call to SVN works, this should work fine; what I'd like however, is to capture the result, and stop the script with an error message if the call fails. The script above obviously fails because of invalid URLs, but the result is not captured in $copyResult
and $switchResult
, as I would have expected. The resulting output is instead as follows; the status-messages are shown before the output of my two result variables, and they are empty:
PS > .\PsExampleForSO.ps1
Branching from:
svn://some/from/url
Branching to:
svn://some/to/url.
svn: E731001: Unable to connect to a repository at URL 'svn://some/from/url'
svn: E731001: Unknown hostname 'some'
Result:
Switching to:
svn://some/to/url.
svn: E731001: Unable to connect to a repository at URL 'svn://some/to/url.'
svn: E731001: Unknown hostname 'some'
Result:
PS >
This differs from a few other calls I'm making, where the output result can be captured in a variable and checked later.
Is there any way to achieve this for these two calls as well?
The exit code of an external command is stored in the $LastExitCode
environment variable. If you want the script to stop in case of an error, you should check that variable:
$copyResult = & svn $arguments
if ($LastExitCode -ne 0) {
exit
}
I'd recommend capturing regular and error output in log files rather than variables, though:
& svn $arguments >'C:\path\to\output.log' 2>'C:\path\to\error.log'
if ($LastExitCode -ne 0) {
exit
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With