将错误代码从CMD返回到Powershell [英] Returning an error code from CMD to Powershell
问题描述
我正在使用一个Mssql安装脚本,我想获得一个静默的mssql安装的结果。在我的PowerShell脚本中,我运行这个命令:
I'm working on a Mssql install script and I want to get the results of a silent mssql installation. In my PowerShell script I run this command:
$result = (start cmd "/c D:\SQL2008R2\SQL2008R2\setup.exe /CONFIGURATIONFILE=sqlconfig.ini && exit 0 || exit 1")
失败时返回0,通过时返回1。不幸的是,我没有得到任何输出。任何想法?
Which should return 0 on fail and 1 on pass. Unfortunately, I don't get any output back. Any ideas?
推荐答案
Start-Process
语法简洁的调用操作符&
。
An alternative to Start-Process
is the more syntactically terse call operator &
.
& cmd.exe /c 'ping.exe doesnotexist && exit 0 || exit 1'
退出代码将包含在内置变量 $ LASTEXITCODE
因此:
The exit code will be contained in the built-in variable $LASTEXITCODE
so:
Write-Host $LASTEXITCODE
这将包含程序运行的退出代码,所以你不需要运行CMD.exe你可以做: p>
This will contain the exit code of the program run so you don't necessary have to run it with CMD.exe you could just do:
& ping.exe doesnotexist ; Write-Host $LASTEXITCODE
应用于您的命令行程序:
Applied to your command line program:
& cmd.exe /c 'D:\SQL2008R2\SQL2008R2\setup.exe /CONFIGURATIONFILE=sqlconfig.ini && exit 0 || exit 1'
或只是:
& D:\SQL2008R2\SQL2008R2\setup.exe /CONFIGURATIONFILE=sqlconfig.ini
对于成功,情况 $ LASTEXITCODE
应为0,否则为非零(如果外部程序正确写入)。
In both cases $LASTEXITCODE
should be 0 for success, non-zero otherwise (if the external program was written correctly).
这篇关于将错误代码从CMD返回到Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!