为什么所有的 Powershell 输出都没有写入 txt 文件 [英] Why is all the Powershell output not written to txt file

查看:69
本文介绍了为什么所有的 Powershell 输出都没有写入 txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Powershell Core 中运行了以下 Powershell 脚本:

I have the following Powershell script that I am running in Powershell Core:

 PS C:\> Get-ChildItem -Recurse -File -Filter *.pbix | 
Sort-Object -Property LastAccessTime | 
Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10 
>> files.txt

打开文件 files.txt 时,LastAccessTime、LastWriteTime 没有列 - 为什么会这样,我该如何修改脚本以便将它们包含在文本文件中?

Opening the file files.txt there are no columns for LastAccessTime, LastWriteTime - why is this and how do I amend the script so they are included in the text file?

推荐答案

两点:

  • >>Out-File -Append 的(虚拟)别名;如果意图不是追加(到预先存在的文件)而是只捕获当前命令的(整个)输出,请仅使用 >/Out-File 没有 -Append.

  • >> is a (virtual) alias for Out-File -Append; if the intent is not to append (to a preexisting file) but to only capture the current command's (entire) output, use just > / Out-File without -Append.

Out-File 并且因此也 >/>> 捕获 for-displayem> 文件中输入对象的表示,适合后续的程序化处理.对于后者,请使用生成结构化输出的 cmdlet,例如 Export-Csv.

Out-File and therefore also > / >> capture the for-display representation of the input objects in a file, which is not suitable for subsequent programmatic processing. For the latter, use a cmdlet that produces structured output, such as Export-Csv.

也就是说,考虑到 file 而不是显示,期望 Out-File 捕获所有 for-display 数据是合理的是目标.

That said, it's reasonable to expect Out-File to capture all for-display data, given that a file rather than the display is the target.

遗憾的是,从 PowerShell [Core] v7.0 开始,Out-File/>/>> 是(仍然) 对控制台(终端)窗口的宽度敏感,并且仅捕获当前适合屏幕的宽度.

Regrettably, as of PowerShell [Core] v7.0, Out-File / > / >> are (still) sensitive to the console (terminal) window's width and capture only as much as would currently fit on the screen.

您可以使用 Out-File 使用 -Width 参数来解决这个问题:

You can use Out-File with the -Width parameter to work around that:

Get-ChildItem -Recurse -File -Filter *.pbix |
  Sort-Object -Property LastAccessTime |
    Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10 |
      Out-File -Width ([int]::MaxValue-1) files.txt # Add -Append, if needed.

注意:从 v7.0 开始,[int]::MaxValue-1 似乎是可接受的最高值;[int]::MaxValue悄悄忽略.另外,请确保不要在 Windows PowerShell 中使用如此高的值,其中每个输出行都无条件地右空格填充到该长度,这会导致过大文件.

Note: As of v7.0, [int]::MaxValue-1 is seemingly the highest value accepted; [int]::MaxValue is quietly ignored. Also, be sure not to use such a high value in Windows PowerShell, where each output line is unconditionally right-space-padded to that length, which would result in excessively large files.

这篇关于为什么所有的 Powershell 输出都没有写入 txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆