我试图使用Powershell脚本自动化分支操作,如下所示:
$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只要对SVN的调用有效,它就会正常工作;不过,我想要的是捕获结果,并在调用失败时使用错误消息停止脚本。上面的脚本显然由于无效的URL而失败,但是结果并没有像我预期的那样在$copyResult和$switchResult中捕获。结果输出如下所示;状态消息显示在我的两个结果变量的输出之前,它们是空的:
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 >这与我正在进行的其他几次调用不同,在这些调用中,可以在变量中捕获输出结果并在稍后进行检查。
这两个电话也有办法做到这一点吗?
发布于 2014-02-07 12:37:24
外部命令的退出代码存储在$LastExitCode环境变量中。如果希望脚本在发生错误时停止,则应检查该变量:
$copyResult = & svn $arguments
if ($LastExitCode -ne 0) {
exit
}不过,我建议捕获日志文件中的常规输出和错误输出,而不是变量:
& svn $arguments >'C:\path\to\output.log' 2>'C:\path\to\error.log'
if ($LastExitCode -ne 0) {
exit
}https://stackoverflow.com/questions/21625452
复制相似问题