Powershell Get-ChildItem 输出延迟? [复制]

2024-05-06

Powershell 版本信息如下:

Name                           Value
----                           -----
PSVersion                      5.0.10586.494
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.494
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

我有以下 powershell 代码:

Write-Host "Step 1..."

$find = "string-to-find"

Get-ChildItem -Path D:\path\path\ -Include *.dat1, *.dat2, *.dat3, *.dat4, -Recurse `
 | Select-String -SimpleMatch $find `
 | Select-Object -Unique Path

Write-Host "Step 2 ..."

Output:

Step 1...

Step 2...

Path
----
D:\path\path\test.dat1

基本上,输出Get-ChildItem正在发生AFTER随后的Write-Host声明——为什么???

鉴于此代码在以前的版本中运行得很好 - 应该使用什么正确的输出方法来按照执行顺序显示输出?

提前致谢。

仍在尝试使用以下方法使其正常工作:

Get-ChildItem -Path D:\path\path\ -Include *.dat1, *.dat2, *.dat3, *.dat4, -Recurse `
 | Select-String -SimpleMatch $find `
 | Select-Object -Unique Path `
 | ForEach-Object { $_ } | Write-Host

但输出看起来像:

@{Path=D:\path\path\something.dat1}
@{Path=D:\path\path\something.dat1}

我想要的只是列出的完整路径名(就像它在 v5 之前工作一样)。


老问题,但添加我的答案,因为它是谷歌上的最佳结果之一。

如果您想继续使用 Write-Host,比如由于它的着色功能,可以通过显式管道传输到来完成Format-Table。完整的代码是:

Write-Host "Step 1..."

$find = "string-to-find"

Get-ChildItem -Path D:\path\path\ -Include *.dat1, *.dat2, *.dat3, *.dat4, -Recurse `
 | Select-String -SimpleMatch $find `
 | Select-Object -Unique Path `
 | Format-Table

Write-Host "Step 2 ..."

Get-ChildItem(和类似的命令),或不以变量\文件结尾的管道,最终通过隐式调用输出到控制台Write-Output.

控制台输出出现乱序,因为 Write-Output 不会同步写入控制台,unless您在 Write-Output 上显式调用 Format-Table。

参考:
https://serverfault.com/questions/693549/powershell-write-host-not-synchronous https://serverfault.com/questions/693549/powershell-write-host-not-synchronous
https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/14925213-bug-console-output-appears-out-of-order https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/14925213-bug-console-output-appears-out-of-order

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Powershell Get-ChildItem 输出延迟? [复制] 的相关文章

随机推荐