简体   繁体   English

将测试路径 output 重定向到文本文件

[英]redirecting test-path output to text file

The txt file is just a bunch of UNC paths, i am trying to get a list of UNC paths from this text file put into another text file after the test-path is validated. txt 文件只是一堆 UNC 路径,我试图在验证测试路径后从此文本文件中获取 UNC 路径列表,并将其放入另一个文本文件中。 it shows the validated paths on screen but the text file does not populate.它在屏幕上显示经过验证的路径,但不会填充文本文件。

$cfgs = Get-Content .\cfgpath.txt
$cfgs | % {
  if (Test-Path $_) { write-host "$_" | Out-File -FilePath c:\temp\1.txt -Append }
}

To complement Zam's helpful answer with background information :背景信息补充Zam 的有用答案

  • Write-Host writes to the host [1] (typically, the console aka terminal), which bypasses PowerShell's success output stream and therefore sends nothing trough the pipeline . Write-Host写入主机[1] (通常是控制台又名终端),它绕过PowerShell 的成功输出流,因此不会通过管道发送任何内容

    • See the bottom section of this answer for when Write-Host is appropriate;请参阅此答案的底部以了解何时适合Write-Host in short: you should generally only use it for display-only output .简而言之:您通常应该只将它用于仅显示输出
  • Write-Output is the appropriate cmdlet for producing data output , but it is rarely necessary , because you can rely on PowerShell's convenient implicit output behavior , as shown in Steven's answer and explained in this answer . Write-Output是用于生成数据输出的适当 cmdlet ,但很少需要,因为您可以依赖 PowerShell 方便的隐式输出行为,如 Steven 的回答中所示并在此回答中进行了解释。


Also, your command will perform much better if you simply pipe the % ( ForEach-Object ) command's output as a whole to a single Out-File call , rather than calling Out-File -Append for each input path .此外,如果您只是% ( ForEach-Object ) 命令的输出作为一个整体通过管道ForEach-Object单个Out-File调用,而不是为每个输入路径调用Out-File -Append ,那么您的命令将执行得更好

Instead of using % with conditional explicit output, you can more elegantly implement your command with the Where-Object cmdlet:您可以使用Where-Object cmdlet 更优雅地实现您的命令,而不是将%与条件显式输出一起使用:

Get-Content .\cfgpath.txt |
  Where-Object { Test-Path $_ } |
    Out-File -FilePath c:\temp\1.txt

Also note that for saving strings to a file it is more efficient to use Set-Content instead of另请注意,要将字符串保存到文件中,使用Set-Content而不是更有效
Out-File , though note that in Windows PowerShell the default output character encoding differs (no longer a concern in PowerShell [Core] 6+, which consistently defaults to BOM-less UTF-8); Out-File ,但请注意,在Windows PowerShell 中,默认输出字符编码不同(在 PowerShell [Core] 6+ 中不再是问题,它始终默认为无 BOM 的 UTF-8); see this answer for when to choose which cmdlet.请参阅此答案以了解何时选择哪个 cmdlet。

By contrast, Out-File and > (its effective alias) use PowerShell's formatting system to write for-display representations of any non-string input objects to the output file , the same way that output renders to the display by default.相比之下, Out-File> (其有效别名)使用 PowerShell 的格式系统将任何非字符串输入对象的显示表示写入输出文件,与默认情况下输出呈现到显示器的方式相同。
In other words: To save objects to a file in a way that is suitable for later programmatic processing , you need to use a structured file format , such as CSV ( Export-Csv ) or JSON ( ConvertTo-Json , combined with Set-Content ).换句话说:要将对象以适合以后编程处理的方式保存到文件中,需要使用结构化的文件格式,例如 CSV( Export-Csv )或 JSON( ConvertTo-Json ,结合Set-Content )。


[1] In PowerShell 5.0 and higher, Write-Host now writes to a new stream, the information stream (number 6 ), which by default prints to the host. [1] 在 PowerShell 5.0 及更高版本中, Write-Host现在写入一个新流,即信息流(编号6 ),默认情况下会打印到主机。 See about_Redirection .请参阅about_Redirection Therefore, a 6> redirection now technically does allow you to send Write-Host output through the pipeline (though doing so is not a good idea) or capture / redirect it;因此,现在技术上的6>重定向确实允许您通过管道发送Write-Host输出(尽管这样做不是一个好主意)或捕获/重定向它; eg,例如,
Write-Host hi 6>&1 | % { "[$_]" }
Write-Host hi 6>&1 | % { "[$_]" } . Write-Host hi 6>&1 | % { "[$_]" } Note that the type of the objects output by this redirection is System.Management.Automation.InformationRecord .请注意,此重定向输出的对象类型是System.Management.Automation.InformationRecord

Write-Host only writes to the console. Write-Host只写入控制台。 I believe what you want there is Write-Output .我相信你想要的是Write-Output

$cfgs = Get-Content .\cfgpath.txt
$cfgs | % {
  if (Test-Path $_) { write-output "$_" | Out-File -FilePath c:\temp\1.txt -Append }
}

Additionally you can just omit the Write-Output and that works too.此外,您可以省略Write-Output也可以。

$cfgs = Get-Content .\cfgpath.txt
$cfgs | % {
  if (Test-Path $_) { "$_" | Out-File -FilePath c:\temp\1.txt -Append }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM