简体   繁体   English

如果从命令提示符运行,powershell脚本将在“ if”子句中退出

[英]powershell script exits in “if” clause if run from command prompt

I am feeling surprised by the difference between two seemingly identical scripts. 我对两个看似相同的脚本之间的差异感到惊讶。

first.ps1: first.ps1:

"A"
if ($true) { "B" } 
"C"

second.ps1: second.ps1:

"A"
if ($true) { "B" 
} 
"C"

Now open a CMD window, and run these scripts like this: 现在打开一个CMD窗口,并运行以下脚本:

powershell - < first.ps1
    powershell - < second.ps1

first produces: 首先生产:

A
B
C

while second produces just 而第二只产生

A

Not sure why the redirection to input doesn't work, but if you just specify the script as an input argument to powershell, it seems to work: 不知道为什么重定向到输入无效,但是,如果您仅将脚本指定为powershell的输入参数,则它似乎可以工作:

C:\fa2>powershell.exe C:\fa2\tc.ps1
Comparison starts
There was a difference
Comparison over

This has to be a bug. 这必须是一个错误。

In the redirection under cmd.exe case, the function completes normally and correctly if the if and else blocks are individually all on one line: 在使用cmd.exe进行重定向的情况下,如果if和else块分别全部放在一行上,则该函数将正常正常地完成:

$candidate = '.\foo.txt'
$current= '.\bar.txt'

"Comparison starts"
if ($(diff $(get-content $current) $(get-content $candidate)).length -gt 0){"There was a difference"} 
else {"There was not a difference"}
"Comparison over"

But if either block is split up onto more than one line and that branch is taken, the script aborts with no warning/output. 但是,如果将任一块划分为多行并采用了该分支,则脚本将中止,并且不会发出警告/输出。

I'd report this on the PowerShell feedback site (connect.microsoft.com) . 我会在PowerShell反馈网站(connect.microsoft.com)上报告此情况。

Edit: 编辑:

Yep, Jay proved it. 是的,周杰伦证明了这一点。 The root problem is that Powershell does not support the '<' operator. 根本问题是Powershell不支持'<'运算符。 I've been searching all day for some official documentaion on the web, but have not found it. 我整天都在网上搜索一些官方文档,但没有找到。 I just occured to me to check some old notes, and I found a reference to this not being supported in v1. 我只是想起我来检查一些旧笔记,发现在v1中不支持对此内容的引用。 It only supports '>'. 它仅支持“>”。

I'll try to update if I find something more official than my memory. 如果我发现比我的记忆更正式的东西,我会尝试更新。 Leaving original text just for completnes. 将原始文本留作补充。


I dont think the accepted answer is enitrely true here. 我不认为公认的答案在这里是正确的。

Take a look at Lee Holmes blog: link 看看Lee Holmes博客: 链接

He is one of the devs on the Powershell team, and wrote the Powershell Cookbook, just to give a little credence to his words. 他是Powershell团队的一名开发人员,并且撰写了Powershell Cookbook,只是为了给他的话一点点信任。

I've run into this kind of problem with some complicated and archaic Bat scripts that relied on some funky fancy binary redirection. 我遇到了一些复杂而古老的Bat脚本,这些脚本依赖于一些时髦的奇特二进制重定向,从而遇到了这种问题。 Powershell would run the Bat file, but at the point where the binary redirection took place it would just stop. Powershell将运行Bat文件,但在二进制重定向发生的那一刻,它将停止。 Using [Process]:Start as described in the blog post worked wonderfully, and allowed me to parse the output of the Bat file like any other nicely behaved script to boot. 使用[Process]:如博客文章中所述,启动非常有效,并且使我能够像其他任何行为良好的脚本一样解析Bat文件的输出。

In your case I assume "diff" is an actuall exe and not a function, and its outputing binary and not text. 在您的情况下,我假定“ diff”是实际的exe而不是函数,并且其输出二进制文件而不是文本。

On a side note, I really don't see the need for redirecting the output of the script to Powershell like youre doing. 附带一提,我真的不认为需要像您一样将脚本的输出重定向到Powershell。 Seems kind of counterproductive. 似乎适得其反。 You wrote a powershell script, seems like a waste not to use the paramter specifically provided to handle running input. 您编写了一个powershell脚本,不使用专门提供的参数来处理运行的输入似乎是一种浪费。

I don't think this is a bug. 我不认为这是一个错误。 Try typing each of those lines in on the console and you will see what is happening. 尝试在控制台上键入每行,您将看到正在发生的事情。 When you type and open bracket and don't close it, PowerShell goes into a multiline entering mode. 当您键入并打开方括号而不将其关闭时,PowerShell将进入多行输入模式。 To exit this mode, you need a closing bracket AND a blank line afterward. 要退出此模式,您需要使用右方括号和后面的空白行。 If you have a blank line before or after the "C" it should work. 如果在“ C”之前或之后有空白行,则应该可以使用。

Of course, maybe it is a bug and just has the same effect as multiline input. 当然,也许这是一个错误,并且与多行输入具有相同的效果。 :) :)

I can't get this to work myself, powershell is ignoring what I send into it. 我无法自己使用此功能,powershell忽略了我发送给它的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM