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