为什么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) 我有一个运行外部EXE文件的脚本.当该EXE文件失败时(将errorlevel设置为1),PowerShell脚本将失败.
我正在运行curl.exe并得到这个:
- CategoryInfo:NotSpecified:(%Total%... Time Current:String)[],RemoteException + FullyQualifiedErrorId:NativeCommandError
如何忽略/捕获外部EXE文件的失败并继续我的脚本?
因此,我们在自动化中使用PsExec来安装虚拟机,因为我们无法在Windows 2003机器上使用ps远程会话.一切都很好,并没有问题,但PsExec不断抛出错误,即使每个命令都没有正确执行.例如:
D:\tools\pstools\psexec.exe $guestIP -u $global:default_user -p $global:default_pwd -d -i C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "Enable-PSRemoting -Force"
Run Code Online (Sandbox Code Playgroud)
在guest虚拟机上启用PsRemoting,但也会抛出此错误消息:
psexec.exe :
Bei D:\Scripts\VMware\VMware_Module5.ps1:489 Zeichen:29
+ D:\tools\pstools\psexec.exe <<<< $guestIP -u $global:default_user -p $global:default_pwd -d -i C:\Windows\System32\WindowsPowerShell\
v1.0\powershell.exe -command "Enable-PSRemoting -Force"
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
Connecting to 172.17.23.95...Starting PsExec service on 172.17.23.95...Connecting with PsExec service on 172.17.23.95...Starting C:\Windows\
System32\WindowsPowerShell\v1.0\powershell.exe on 172.17.23.95...
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe started on 172.17.23.95 …Run Code Online (Sandbox Code Playgroud) 当外部命令(例如 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 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): …
我有一个简单的 PowerShell 脚本来使文件夹与其 git 存储库保持同步。
Param(
[Parameter(Mandatory = $true)][string]$branch,
[string]$location,
[int]$depth
)
Get-ChildItem -Recurse -Directory -Depth $depth -Filter _$branch -Name -Path $location |
ForEach-Object {
Write-Output "`n$("{0:MM/dd/yy} {0:HH:mm:ss}" -f (Get-Date)): Getting latest from $location$_"
git -C $location$_ pull
} *>> logs\$branch.log
Run Code Online (Sandbox Code Playgroud)
当存储库已经是最新的时,它可以正常工作。但是,如果不是,则输出如下:
07/29/19 14:47:27:从某个路径获取最新信息
git :来自 https://gitlab.com/someplace
在某个地方\UpdateBranch.ps1:10 字符:5
+ git -C $location$_ 拉
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (来自 https://gi...n/somerepo:String) [], RemoteException
+ FullQualifiedErrorId : NativeCommandError
b34e5d0..3ec9561 开发 -> 起源/开发
b6d33b1..65fb520 功能/功能 1 -> 原点/功能/功能 1
* [新分支] …