如何在 powershell 不更改编码的情况下将命令的输出通过管道传输到文件?
How to pipe the output of a command to a file without powershell changing the encoding?
我想将命令的输出通过管道传输到文件:
PS C:\Temp> create-png > binary.png
我注意到 Powershell 更改了编码,我可以手动提供编码:
PS C:\Temp> create-png | Out-File "binary.png" -Encoding OEM
但是没有 RAW 编码选项,甚至 OEM 选项也将换行字节 (0xA
resp 0xD
) 更改为 windows 换行字节序列 (0xD 0xA
)从而破坏任何二进制格式。
如何防止 Powershell 在传输到文件时更改编码?
相关问题
- PowerShellscript, bad file encoding conversation
- Write output to a text file in PowerShell
- Using PowerShell to write a file in UTF-8 without the BOM
根据this well written blog article
When using curl with PowerShell, never, never redirect to file with >.
Always use the –o or –out switch. If you need to stream the
output of curl to another utility (say gpg) then you need to sub-shell
into cmd for the binary streaming or use temporary files.
尝试使用 set-content:
create-png | set-content -path myfile.png -encoding byte
如果您需要有关设置内容的更多信息,只需 运行
get-help set-content
您也可以使用 'sc' 作为设置内容的快捷方式。
经过以下测试,生成可读的 PNG:
function create-png()
{
[System.Drawing.Bitmap] $bitmap = new-object 'System.Drawing.Bitmap'([Int32]32,[Int32]32);
$graphics = [System.Drawing.Graphics]::FromImage($bitmap);
$graphics.DrawString("TEST",[System.Drawing.SystemFonts]::DefaultFont,[System.Drawing.SystemBrushes]::ActiveCaption,0,0);
$converter = new-object 'System.Drawing.ImageConverter';
return([byte[]]($converter.ConvertTo($bitmap, [byte[]])));
}
create-png | set-content -Path 'fromsc.png' -Encoding Byte
如果您正在调用一个非 PowerShell 可执行文件(如 ipconfig)并且您只想从标准输出中捕获字节,请尝试 Start-Process:
Start-Process -NoNewWindow -FilePath 'ipconfig' -RedirectStandardOutput 'output.dat'
创建包含行的批处理文件
create-png > binary.png
并通过
从 Powershell 调用它
& cmd /c batchfile.bat
如果您希望将命令作为命令行参数传递给 cmd:
$x = "create-png > binary.png"
& cmd /c $x
我想将命令的输出通过管道传输到文件:
PS C:\Temp> create-png > binary.png
我注意到 Powershell 更改了编码,我可以手动提供编码:
PS C:\Temp> create-png | Out-File "binary.png" -Encoding OEM
但是没有 RAW 编码选项,甚至 OEM 选项也将换行字节 (0xA
resp 0xD
) 更改为 windows 换行字节序列 (0xD 0xA
)从而破坏任何二进制格式。
如何防止 Powershell 在传输到文件时更改编码?
相关问题
- PowerShellscript, bad file encoding conversation
- Write output to a text file in PowerShell
- Using PowerShell to write a file in UTF-8 without the BOM
根据this well written blog article
When using curl with PowerShell, never, never redirect to file with >. Always use the –o or –out switch. If you need to stream the output of curl to another utility (say gpg) then you need to sub-shell into cmd for the binary streaming or use temporary files.
尝试使用 set-content:
create-png | set-content -path myfile.png -encoding byte
如果您需要有关设置内容的更多信息,只需 运行
get-help set-content
您也可以使用 'sc' 作为设置内容的快捷方式。
经过以下测试,生成可读的 PNG:
function create-png()
{
[System.Drawing.Bitmap] $bitmap = new-object 'System.Drawing.Bitmap'([Int32]32,[Int32]32);
$graphics = [System.Drawing.Graphics]::FromImage($bitmap);
$graphics.DrawString("TEST",[System.Drawing.SystemFonts]::DefaultFont,[System.Drawing.SystemBrushes]::ActiveCaption,0,0);
$converter = new-object 'System.Drawing.ImageConverter';
return([byte[]]($converter.ConvertTo($bitmap, [byte[]])));
}
create-png | set-content -Path 'fromsc.png' -Encoding Byte
如果您正在调用一个非 PowerShell 可执行文件(如 ipconfig)并且您只想从标准输出中捕获字节,请尝试 Start-Process:
Start-Process -NoNewWindow -FilePath 'ipconfig' -RedirectStandardOutput 'output.dat'
创建包含行的批处理文件
create-png > binary.png
并通过
从 Powershell 调用它& cmd /c batchfile.bat
如果您希望将命令作为命令行参数传递给 cmd:
$x = "create-png > binary.png"
& cmd /c $x