相关疑难解决方法(0)

$ LastExitCode = 0,但在PowerShell中$?= False.将stderr重定向到stdout会产生NativeCommandError

为什么PowerShell在下面的第二个例子中显示出令人惊讶的行为?

首先,一个理智行为的例子:

PS C:\> & cmd /c "echo Hello from standard error 1>&2"; echo "`$LastExitCode=$LastExitCode and `$?=$?"
Hello from standard error
$LastExitCode=0 and $?=True
Run Code Online (Sandbox Code Playgroud)

没有惊喜.我打印一条消息到标准错误(使用cmd's echo).我检查变量$?$LastExitCode.正如预期的那样,它们分别等于True和0.

但是,如果我要求PowerShell通过第一个命令将标准错误重定向到标准输出,我会得到一个NativeCommandError:

PS C:\> & cmd /c "echo Hello from standard error 1>&2" 2>&1; echo "`$LastExitCode=$LastExitCode and `$?=$?"
cmd.exe : Hello from standard error
At line:1 char:4
+ cmd <<<<  /c "echo Hello from standard error 1>&2" 2>&1; echo "`$LastExitCode=$LastExitCode and `$?=$?"
    + CategoryInfo          : NotSpecified: (Hello from …
Run Code Online (Sandbox Code Playgroud)

windows powershell command-line

43
推荐指数
4
解决办法
3万
查看次数

Powershell与Git命令错误处理

我使用Git来部署我的Web应用程序.所以在Teamcity我准备我的应用程序(编译,缩小JS和HTML,删除未使用的文件等等)然后我有一个Powershell构建步骤:

$ErrorActionPreference = "Stop"
git init
git remote add origin '%env.gitFolder%'
git fetch
git reset --mixed origin/master
git add .
git commit -m '%build.number%'
git push origin master
Run Code Online (Sandbox Code Playgroud)

但是如果抛出异常,脚本会继续(即使我设置$ErrorActionPreference = "Stop")并且构建成功.

我希望脚本在出现错误时停止,并且构建失败.

我试图进行Format stderr output as: error构建步骤Fail build if: an error message is logged by build runner,因此构建失败,但脚本继续,因此它创建了一个愚蠢的提交.

我尝试在我的脚本中添加一个try-catch,但它没有进入catch ...

有没有人有想法停止脚本并失败构建错误?

抱歉我的英语,我是法国人...... ^^

git powershell teamcity

8
推荐指数
2
解决办法
2873
查看次数

PowerShell 相当于 cmd“IF %ERRORLEVEL% NEQ 0”

寻找与此 cmd 错误检查等效的 PowerShell:

\n\n

IF %ERRORLEVEL% NEQ 0

\n\n

这是我试图编写的 PowerShell 代码:

\n\n
Write-Information "Installing .NET 3 from DVD:"\n$NetFX3_Source = "D:\\Sources\\SxS"\ndism /online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:$NetFX3_Source /NoRestart\nIF (****TheCommandYouTellMe****) {\nWrite-Information "DVD not found, installing from online sources, the Win default method"\nDISM.EXE /Online /Add-Capability /CapabilityName:NetFx3~~~~\nAdd-WindowsCapability \xe2\x80\x93Online -Name NetFx3~~~~\n}\n
Run Code Online (Sandbox Code Playgroud)\n

powershell powershell-3.0

3
推荐指数
1
解决办法
6698
查看次数