当前位置:网站首页>$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边栏推荐
- Alertmanager alarm configuration
- How to reduce the complexity of cloud computing infrastructure?
- The program actively carries out telephone short message alarm, and customizes telephone, short message and nail alarm notifications
- WPS table learning notes - highlight duplicate values
- How to make div 100% page (not screen) height- How to make a div 100% of page (not screen) height?
- Wechat applet project example - renju for two
- Force deduction solution summary 450- delete nodes in the binary search tree
- [digital signal processing] correlation function (energy signal | cross correlation function of energy signal | autocorrelation function of energy signal)
- errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
- Xiaopi can't start the MySQL database. Please find out!
猜你喜欢

分數大小的比較

Drawcall, batches, setpasscall in unity3d

分数大小的比较

如何防止商場電氣火灾的發生?

Demand and business model innovation - demand 11 - overview of demand analysis

余压监控系统在高层民用建筑的应用

Exemple de projet d'applet Wechat - calculatrice de constitution

Sparse tensor based point cloud attribute compression

ACL 2022 - strong combination of pre training language model and graphic model

Wechat applet project example - Fitness calculator
随机推荐
小红的删数字
Alertmanager alarm configuration
Ue4\ue5 touch screen touch event: single finger and double finger
Recommend 6 office software, easy to use and free, double the efficiency
Apply concentrated load to nodes in batch in ABAQUS
xml
Machine learning - dimensionality reduction (data compression, data visualization)
Abaqus中批量对节点施加集中力荷载
Start stop script and distribution script of shell
Wechat applet project example - renju for two
$. map(data,function(item,index){return XXX})
Transformation of geographical coordinates of wechat official account development
Demand and business model innovation - demand 10- observation and document review
String number with special style
分数大小的比较
Force deduction programming problem - solution summary
Demand and business model innovation - demand 11 - overview of demand analysis
[digital signal processing] correlation function (energy signal | cross correlation function of energy signal | autocorrelation function of energy signal)
[digital signal processing] correlation function (power signal | cross correlation function of power signal | autocorrelation function of power signal)
Red's deleted number
