首页 > 解决方案 > PowerShell Core 添加内容字节失败

问题描述

我正在使用 PowerShell Core v6.0.2,并尝试将字节数组写入文件。这在常规 PowerShell 中运行良好,但在 PowerShell Core 中失败

$jsonstr = Get-Content $inputfilename
$jsonfile = ConvertFrom-Json $jsonstr
$bytes = [Convert]::FromBase64String($jsonfile.data)

$outputfilename = "test.xlsx";

Add-Content -Path $outputfilename -Value $bytes -Encoding Byte

错误:

在此处输入图像描述

这是一个错误还是由于二进制排序问题而不能再使用 Byte ?

标签: powershellpowershell-core

解决方案


根据这篇博文,在 PowerShell Core 上,您需要使用带有 AsByteStream 参数的 Set-Content

我已将脚本更改为以下内容:

$jsonstr = Get-Content $inputfilename
$jsonfile = ConvertFrom-Json $jsonstr
$bytes = [Convert]::FromBase64String($jsonfile.data)

$outputfilename = "test.xlsx";
Set-Content -Path $outputfilename -Value $bytes -AsByteStream

推荐阅读