度量对象未输出正确的结果



我不明白为什么会导致0

[String] $outputVariable = "INFO: Configuring the component HPOvPerlA";
write-host $outputVariable | Select-String -pattern "INFO:" -SimpleMatch | Measure-Object -Line;
Lines Words                         Characters                    Property
----- -----                         ----------                    --------
0

好像我在下面运行,然后我得到正确的结果。

"INFO: Configuring the component HPOvPerlA" | Select-String -pattern "INFO:" | Measure-Object -Line;
Lines Words                         Characters                    Property
----- -----                         ----------                    --------
1

我看到的唯一区别是write-host和$variable但在我看来这应该没有任何区别。

有什么提示吗?

您面临的问题正是您已经确定的,但似乎被忽略了,这就是Write-Host

Write-Host$outputVariable/string 内容发送到控制台,但你的目的是通过管道发送它。为此,您应该改用Write-Output

例子:

[String] $outputVariable = "INFO: Configuring the component HPOvPerlA";
Write-Output $outputVariable | Select-String -Pattern "INFO:" -SimpleMatch | Measure-Object -Line;
Write-Output "INFO: Configuring the component HPOvPerlA" | Select-String -Pattern "INFO:" | Measure-Object -Line | Select-Object -ExpandProperty Lines;

最新更新