Powershell Get-ChildItem 输出延迟?

标签 powershell

这个问题在这里已经有了答案:





PowerShell output is crossing between functions

(1 个回答)


2年前关闭。




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 ..."

输出:
Step 1...

Step 2...

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

基本上,Get-ChildItem 的输出正在发生之后 随后的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 不会同步写入控制台,除非您在 Write-Output 上显式调用 Format-Table。

引用:
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

关于Powershell Get-ChildItem 输出延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599976/

相关文章:

powershell - 如何使用 PowerShell 2.0 执行 ‘Pause’?

winforms - 如何使用 Powershell 合并 DataGridView 中的单元格

python - 使用基于身份的身份验证的 Azure Function App Blob 触发器返回 powershell 错误

Powershell - "Toggle"的官方祝福动词

c# - 如何枚举真实的 Windows 文件资源管理器窗口

powershell - 如何在Powershell中添加最大循环?

c++ - 与 powershell get-physicaldisk 命令等效的 Windows API

Powershell - SET-ADUSER 修改samaccountname

azure - 在Azure Web App上运行PowerShell脚本,可能吗?

powershell - 调用-AzureRmResourceAction : The pipeline has been stopped