我没想到或不希望输出中出现转义序列。第一行输出是:
\n0000000000000000 1B 5B 33 32 3B 31 6D 4C 6F 63 61 6C 41 64 64 72 \xef\xbf\xbd[32;1m本地地址
\nPS C:\\src\\t> Get-NetTCPConnection | Where-Object { $_.State -eq 'Established' } | Out-File -FilePath '.\\task3.txt' -Encoding ascii\nPS C:\\src\\t> Get-Content .\\task3.txt -first 2 | Format-Hex\n\n Label: String (System.String) <5F8755A9>\n\n Offset Bytes Ascii\n 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n ------ ----------------------------------------------- -----\n0000000000000000 1B 5B 33 32 3B 31 6D 4C 6F 63 61 6C 41 64 64 72 \xef\xbf\xbd[32;1mLocalAddr\n0000000000000010 65 73 73 20 20 20 20 20 20 20 20 20 20 20 20 20 ess\n0000000000000020 20 20 20 20 20 20 20 20 20 20 20 4C 6F 63 61 6C Local\n0000000000000030 50 6F 72 74 20 52 65 6D 6F 74 65 41 64 64 72 65 Port RemoteAddre\n0000000000000040 73 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ss\n0000000000000050 20 20 20 20 20 20 20 20 20 52 65 6D 6F 74 65 50 RemoteP\n0000000000000060 6F 72 74 20 53 74 61 74 65 20 20 20 20 20 20 20 ort State\n0000000000000070 41 70 70 6C 69 65 64 53 65 74 74 69 6E 67 20 4F AppliedSetting O\n0000000000000080 77 6E 69 6E 67 50 72 6F 63 65 73 73 1B 5B 30 6D wningProcess**\xef\xbf\xbd[0m**\nRun Code Online (Sandbox Code Playgroud)\n我注意到它没有出现在 Get-ChildItem 的输出中。
\nPS C:\\src\\t> dir *.g* | Out-File -FilePath '.\\task5.txt' -Encoding ascii\nPS C:\\src\\t> Get-Content -Path '.\\task5.txt' -First 2 | Format-Hex\n\n Label: String (System.String) <5A0214B6>\n\n Offset Bytes Ascii\n 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n ------ ----------------------------------------------- -----\n0000000000000000 20 20 20 20 44 69 72 65 63 74 6F 72 79 3A 20 43 Directory: C\n0000000000000010 3A 5C 73 72 63 5C 74 :\\src\\t\nRun Code Online (Sandbox Code Playgroud)\n
Tom 的有用回答提出了一个重要观点:cmdlet 的输出Out-*是供人类眼球使用的,而不是供编程处理的。
抛开这一点:
事实上,ANSI /虚拟终端转义序列(在本例中用于生成彩色输出)出现在输出文件中意味着您正在使用PowerShell (Core) 7.2+,其中 的值$PSStyle.OutputRendering确定这些转义序列是否保留在输出中from - 隐式或显式 - 对Out-*cmdlet 的调用:
用于$PSStyle.OutputRendering = 'Host'限制在格式化输出中使用转义序列以发送到显示器(主机/控制台/终端)的输出,并进行所有其他用途 - 将输出发送到带有(实际上是 的别名)的文件>Out-File或通过管道- 导致删除这些转义序列以生成纯文本输出。
$PSStyle.OutputRendering = 'Host'到您的$PROFILE文件中以使此行为成为将来会话的默认行为。遗憾的是,从 PowerShell 7.2.1 开始,默认值为,它$PSStyle.OutputRendering无条件地将转义序列包含在格式化输出中。'Ansi'
'Host'将产生更明智的默认值,与类 Unix 平台上的 CLI 所表现出的行为一致,当它们的 stdout 输出未连接到终端时,它们本身会绕过转义序列的使用-除非明确要求包含它们,与选项,如--color=always| 归档时间: |
|
| 查看次数: |
1490 次 |
| 最近记录: |