【发布时间】:2016-07-26 21:13:08
【问题描述】:
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 的输出发生在 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-hostandwrite-outputare different 和write-output调用are implicit。您通过两条完全不同的路线发送两种不同类型的输出(实际上可能根本不会一起出现在屏幕上),然后询问为什么需要不同的时间。 -
感谢到目前为止的回复...但是鉴于此脚本在以前版本的 powershell 中工作得很好——正确的输出方法是像以前那样按顺序输出东西?
-
已解决——根据这篇文章将所有“Write-Host”更改为“Write-Output”:jsnover.com/blog/2013/12/07/write-host-considered-harmful
标签: powershell