测量对象没有输出正确的结果

时间:2019-09-19 08:02:30

标签: powershell

我无法理解为什么会导致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,但在我看来,这应该没有任何区别。

有任何提示吗?

1 个答案:

答案 0 :(得分:1)

您面临的问题恰好是您已经确定的问题,但似乎已被解决,那就是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;