相关疑难解决方法(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 ISE在git checkout上抛出错误

在PowerShell中,git checkout运行时没有任何错误消息.在ISE中,当git checkout静止不动时,ISE会给出错误消息.

> git checkout master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
git : Switched to branch 'master'
At line:1 char:1
+ git checkout master
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Switched to branch 'master':String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
Run Code Online (Sandbox Code Playgroud)

这不是一个主要问题,因为git checkout仍然有效.但这很烦人,所以我想知道为什么ISE会在标准的PowerShell没有时抱怨,更重要的是,我们怎样才能防止这种烦恼.

我看过为什么Powershell ISE显示Powershell控制台没有显示的错误?,这解释了ISE只是显示正常shell正在经历的内容.这个答案并不能解释如何平息这种烦人的行为.

git powershell powershell-ise powershell-3.0

11
推荐指数
3
解决办法
3589
查看次数

阻止 git 将非错误写入 stderr

我有一个脚本,用于自动同步各种远程 git 存储库。我试图用我的脚本做的一件事是从每个命令中捕获 stderr 的输出,并将所有这些错误写入一个文本文件,然后在脚本完成后通过电子邮件发送给我。这将提醒我需要解决的任何问题。不过,我在以下两行中遇到了问题:

{
    git fetch --prune-tags github-fetch master
    git push github master 
} 2> '/tmp/stderr-contents-sync_git_repositories.txt'
Run Code Online (Sandbox Code Playgroud)

问题是该git fetch行正在将以下内容写入 stderr:

From https://github.com/XJDHDR/xjdhdr-random-code.wiki
 * branch            master     -> FETCH_HEAD
   13af304..333d602  master     -> github/master
Run Code Online (Sandbox Code Playgroud)

git pull条线是这样写的:

To ssh://github.com/XJDHDR/xjdhdr-random-code.wiki.git
   333d602..da65970  master -> master
Run Code Online (Sandbox Code Playgroud)

我的问题是这些都不是错误,每次运行脚本时都会通过电子邮件发送。我想知道是否有可能阻止 git 将这些非错误写入 stderr 或从 stderr 输出中过滤出这些类型的消息,同时保留真正的错误。

git bash stderr

11
推荐指数
1
解决办法
1381
查看次数

不要在从外部命令写入 stderr 时抛出 PowerShell 异常

当外部命令(例如 git)写入 stderr 时,PowerShell 会生成 NativeCommandError 异常。我想正常地看到输出和标准输出,类似于它在标准 UNIX/Linux 系统上的外观。此脚本需要运行许多本机命令,如果可能,我更喜欢不会给每个命令增加太多混乱和维护的解决方案。

在 Linux 上,可以这样检出分支:

$ git checkout devel
Branch 'devel' set up to track remote branch 'devel' from 'origin'.
Switched to a new branch 'devel'
Run Code Online (Sandbox Code Playgroud)

其中微妙的部分是,无论出于何种原因,最后一行都已写入 stderr。但是,git 的退出状态为零表示它成功了。在 PowerShell 下,我得到了这个:

PS> git checkout devel
Branch 'devel' set up to track remote branch 'devel' from 'origin'.
git : Switched to a new branch 'devel'
At line:1 char:1
+ git checkout devel
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Switched to a new branch 'devel':String) …
Run Code Online (Sandbox Code Playgroud)

git error-handling powershell

9
推荐指数
2
解决办法
1291
查看次数

从Powershell运行"Git-Clone"即使看起来有效也会出错

如何解释来自调用"Git Clone"(实际使用GitLab)的PowerShell脚本的错误.我可以克隆一个空目录并使其工作而不会出错吗?

$path1 = "d:\GitPath" 
$path2 = "${path1}\mycompany-mygroup-nealtestautomation01-map"
$gitLabHttp = "http://git.mycompany.com/MyGroup/nealtestscriptaddedproject.git"
$gitLabHttp = "http://git.mycompany.com/MyGroup/mycompany-gwcustomers-nealtestautomation01-map.git" 
rd $path2 
cd $path1
git clone $gitLabHttp --local --verbose
Run Code Online (Sandbox Code Playgroud)

输出:

git : Cloning into 'mycompany-mygroup-nealtestautomation01210-map'...
At D:\Scripts\GitCloneTest.ps1:7 char:1
+ git clone $gitLabHttp --local --verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Cloning into '...mation01-map'...:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

warning: --local is ignored
warning: You appear to have cloned an empty repository.
Run Code Online (Sandbox Code Playgroud)

我想要使​​用此代码的任何更大的脚本,我包括:

$ErrorActionPreference = "Stop"  #Special Poweshell Syntax to Stop on First Error 
Run Code Online (Sandbox Code Playgroud)

这样它就会在第一个错误时停止.

即使我在一个非空的项目上运行它,我看到红色和类似于上面的错误(在此次运行中使用--local和--verbose): …

git powershell-4.0

6
推荐指数
1
解决办法
1141
查看次数

PowerShell捕获Git输出

问题

我正在通过PowerShell运行一些git命令,但我在捕获输出时遇到问题.

在我尝试使用之前,我所研究的许多方法都未能捕获任何输出Out-File.不幸的是,尽管这对我来说有多好,但我发现git push命令出了问题,导致我无法捕获输出...

我的代码如下:

git add . | Out-File $logFilePath -Append
git status . | Out-File $logFilePath -Append
git commit -m "Some Relevant update message" | Out-File $logFilePath -Append
git push origin $branchname | Out-File $logFilePath -Append
Run Code Online (Sandbox Code Playgroud)

当我查看日志文件时,它会显示除了输出之外的所有内容git push.因此,在对此进行故障排除时,我| Out-File从行中删除了,并注意到即使输出窗口也没有显示输出git push.只有当我完全删除Out-File所有git命令时,我才能使输出再次按预期显示.不幸的是,这让我回到原点.

有谁知道如何使用PowerShell最好地捕获git命令的输出,或者可以指出我做错了什么?

git powershell logging

4
推荐指数
1
解决办法
1733
查看次数