当前位置:网站首页>$LastExitCode=0, but $?= False in PowerShell. Redirecting stderr to stdout gives NativeCommandError
$LastExitCode=0, but $?= False in PowerShell. Redirecting stderr to stdout gives NativeCommandError
2022-06-12 02:59:00 【Rich in starch】
problem :
Why does PowerShell show the surprising behaviour in the second example below? Why? PowerShell The second example below shows surprising behavior ?
First, an example of sane behaviour: First , An example of rational behavior :
PS C:\> & cmd /c "echo Hello from standard error 1>&2"; echo "`$LastExitCode=$LastExitCode and `$?=$?"Hello from standard error$LastExitCode=0 and $?=TrueNo surprises. No surprises .I print a message to standard error (using cmd 's echo ). I print messages to standard errors ( Use cmd Of echo ).I inspect the variables $? I check the variables $?and $LastExitCode . and $LastExitCode .They equal to True and 0 respectively, as expected. As expected , They are equal to respectively True and 0.
However, if I ask PowerShell to redirect standard error to standard output over the first command, I get a NativeCommandError: however , If I ask PowerShell Redirect standard errors to standard output with the first command , I will receive NativeCommandError:
PS C:\> & cmd /c "echo Hello from standard error 1>&2" 2>&1; echo "`$LastExitCode=$LastExitCode and `$?=$?"cmd.exe : Hello from standard errorAt line:1 char:4+ cmd <<<< /c "echo Hello from standard error 1>&2" 2>&1; echo "`$LastExitCode=$LastExitCode and `$?=$?" + CategoryInfo : NotSpecified: (Hello from standard error :String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError$LastExitCode=0 and $?=FalseMy first question, why the NativeCommandError? My first question , Why NativeCommandError?
Secondly, why is $? secondly , Why $?False when cmd ran successfully and $LastExitCode is 0? When cmd Run successfully and $LastExitCode by 0 When is False?PowerShell's documentation about automatic variables doesn't explicitly define $?PowerShell About automatic variables The documentation for is not clearly defined $?..I always supposed it is True if and only if $LastExitCode is 0, but my example contradicts that. If and only if $LastExitCode by 0 when , I always thought it was True, But my example contradicts this .
Here's how I came across this behaviour in the real-world (simplified). This is how I encounter this behavior in the real world ( simplify ).It really is FUBAR. It's really FUBAR.I was calling one PowerShell script from another. I am calling one from another PowerShell Script .The inner script: Internal script :
cmd /c "echo Hello from standard error 1>&2"if (! $?){ echo "Job failed. Sending email.." exit 1}# Do something elseRunning this simply as .\\job.ps1 , it works fine, and no email is sent. Simply regard it as .\\job.ps1 function , It works , And don't send email .However, I was calling it from another PowerShell script, logging to a file .\\job.ps1 2>&1 > log.txt . however , I am from another PowerShell Script calls it , Record to file .\\job.ps1 2>&1 > log.txt .In this case, an email is sent! under these circumstances , Will send an email !What you do outside the script with the error stream affects the internal behaviour of the script. What you do outside the script using the error flow affects the internal behavior of the script .Observing a phenomenon changes the outcome. Observation will change the result .This feels like quantum physics rather than scripting! It feels like quantum physics, not a script !
[Interestingly: .\\job.ps1 2>&1 may or not blow up depending on where you run it][ Interestingly : .\\job.ps1 2>&1 May or may not explode , It depends on where you run it ]
Solution :
Reference resources : https://stackoom.com/en/question/ikjt边栏推荐
- Application of acrelcloud-6000 secure power cloud platform in a commercial plaza
- 2022 Fujian Provincial Safety Officer C certificate (full-time safety officer) examination simulation 100 questions and answers
- I2C协议概述
- Red's deleted number
- 如何防止商场电气火灾的发生?
- Package a mobile APK app containing watch app - ticwear
- Comparaison de la taille des fractions
- Demand and business model innovation - demand 10- observation and document review
- errno: -4078, code: ‘ECONNREFUSED‘, syscall: ‘connect‘, address: ‘127.0.0.1‘, port: 3306;postman报错
- A single quarter of educational technology revenue of 230million: a year-on-year decrease of 51% and a sharp narrowing of net loss
猜你喜欢

Sparse tensor based point cloud attribute compression

微信小程序項目實例——體質計算器

Application of ankery anti shake electric products in a chemical project in Hebei

Kubernetes affinity learning notes

Maya foreground rendering plug-in Mel scripting tool

The road of global evolution of vivo global mall -- multilingual solution

How to use DAO to build a knowledge database with collective wisdom and sustainable incentive mechanism

maya前台渲染插件mel脚本工具

Inverted string - two solutions

一起教育科技单季营收2.3亿:同比降51% 净亏大幅收窄
随机推荐
oracle之用户和表空间
Acl2022 | DCSR: a sentence aware contrastive learning method for open domain paragraph retrieval
maya前臺渲染插件mel脚本工具
分数大小的比较
Drawcall, batches, setpasscall in unity3d
Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
ACL 2022 - strong combination of pre training language model and graphic model
Calculus review 2
Unique paths for leetcode topic resolution
Android HTML5 page load cache optimization
Graduation design of fire hydrant monitoring system --- thesis (add the most comprehensive hardware circuit design - > driver design - > Alibaba cloud Internet of things construction - > Android App D
Application of residual pressure monitoring system in high-rise civil buildings
Unity3D中DrawCall、Batches、SetPassCall
Demand and business model innovation - demand 11 - overview of demand analysis
min25筛
Wave view audio information
【点云压缩】Sparse Tensor-based Point Cloud Attribute Compression
Force deduction programming problem - solution summary
xml
Demand and business model innovation - demand 6- stakeholder analysis and hard sampling
