I am feeling surprised by the difference between two seemingly identical scripts.
我对两个看似相同的脚本之间的区别感到惊讶。
first.ps1:
"A"
if ($true) { "B" }
"C"
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
4 个解决方案
#1
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)上报告此问题。
#2
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
#3
Edit:
Yep, Jay proved it. The root problem is that Powershell does not support the '<' operator. 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. It only supports '>'.
杰伊,杰伊证明了这一点。根本问题是Powershell不支持'<'运算符。我一整天都在寻找网上的官方文件,但还没找到。我刚刚发现我检查了一些旧笔记,我发现v1中没有支持这个引用。它只支持'>'。
I'll try to update if I find something more official than my memory. Leaving original text just for completnes.
如果我找到比我的记忆更正式的东西,我会尝试更新。留下原始文本只是为了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. Powershell would run the Bat file, but at the point where the binary redirection took place it would just stop. 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.
我遇到了一些复杂而古老的Bat脚本,这些脚本依赖于一些时髦的花哨二进制重定向。 Powershell会运行Bat文件,但是在二进制重定向发生的时候它会停止。使用[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. 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。似乎有点适得其反。你写了一个powershell脚本,似乎浪费不使用专门提供的参数来处理运行输入。
#4
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. 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.
我不认为这是一个错误。尝试在控制台上键入每一行,您将看到发生了什么。当您键入并打开括号并且不关闭它时,PowerShell将进入多行输入模式。要退出此模式,您需要一个右括号和后面的空白行。如果在“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忽略了我发送给它的东西。
#1
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)上报告此问题。
#2
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
#3
Edit:
Yep, Jay proved it. The root problem is that Powershell does not support the '<' operator. 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. It only supports '>'.
杰伊,杰伊证明了这一点。根本问题是Powershell不支持'<'运算符。我一整天都在寻找网上的官方文件,但还没找到。我刚刚发现我检查了一些旧笔记,我发现v1中没有支持这个引用。它只支持'>'。
I'll try to update if I find something more official than my memory. Leaving original text just for completnes.
如果我找到比我的记忆更正式的东西,我会尝试更新。留下原始文本只是为了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. Powershell would run the Bat file, but at the point where the binary redirection took place it would just stop. 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.
我遇到了一些复杂而古老的Bat脚本,这些脚本依赖于一些时髦的花哨二进制重定向。 Powershell会运行Bat文件,但是在二进制重定向发生的时候它会停止。使用[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. 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。似乎有点适得其反。你写了一个powershell脚本,似乎浪费不使用专门提供的参数来处理运行输入。
#4
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. 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.
我不认为这是一个错误。尝试在控制台上键入每一行,您将看到发生了什么。当您键入并打开括号并且不关闭它时,PowerShell将进入多行输入模式。要退出此模式,您需要一个右括号和后面的空白行。如果在“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忽略了我发送给它的东西。