Character encoding Powershell 7字节编码图像文件

Character encoding Powershell 7字节编码图像文件,character-encoding,powershell-7.0,Character Encoding,Powershell 7.0,我正在使用PowerShell通过API将文件上载到网站 在PS5.1中,这将获得另一端API处理的正确B64编码的图像: $b64 = [convert]::ToBase64String((get-content $image_path -encoding byte)) 在PS7中,出现以下错误: Get-Content: Cannot process argument transformation on parameter 'Encoding'. 'byte' is not a suppo

我正在使用PowerShell通过API将文件上载到网站

在PS5.1中,这将获得另一端API处理的正确B64编码的图像:

$b64 = [convert]::ToBase64String((get-content $image_path -encoding byte))
在PS7中,出现以下错误:

Get-Content: Cannot process argument transformation on parameter 'Encoding'. 'byte' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. (Parameter 'name')
我尝试过用其他编码读取内容,然后使用[system.Text.encoding]:GetBytes()进行转换,但字节数组总是不同的。乙二醇

PS 5.1> $bytes = get-content -Path $image -Encoding byte ; Write-Host "bytes:" $bytes.count ; Write-Host "First 11:"; $bytes[0..10] 
bytes: 31229
First 11:
137
80
78
71
13
10
26
10
0
0
0
但在PowerShell7上:

PS7> $enc = [system.Text.Encoding]::ASCII
PS7> $bytes = $enc.GetBytes( (get-content -Path $image -Encoding ascii | Out-String)) ; Write-Host "bytes:" $bytes.count ; Write-Host "First 11:"; $bytes[0..10]
bytes: 31416   << larger
First 11:
63 << diff
80 << same
78 << 
71
13
10
26
13 << new
10
0
0
PS7>$enc=[system.Text.Encoding]::ASCII
PS7>$bytes=$enc.GetBytes((获取内容-路径$image-编码ascii |输出字符串));写入主机“字节:$bytes.count;写主机“第一个11:”$字节[0..10]

字节:31416问题原来是Get内容。我使用以下方法绕过了该问题:

$bytes = [System.IO.File]::ReadAllBytes($image_path)
注意:$image_路径必须是绝对路径,而不是相对路径

所以我的基线变成:

$b64 = [convert]::ToBase64String([System.IO.File]::ReadAllBytes($image_path))

问题在于获取内容。我使用以下方法绕过了该问题:

$bytes = [System.IO.File]::ReadAllBytes($image_path)
注意:$image_路径必须是绝对路径,而不是相对路径

所以我的基线变成:

$b64 = [convert]::ToBase64String([System.IO.File]::ReadAllBytes($image_path))

对于PowerShell,6字节不再是Enconding参数的有效参数。您应该结合参数Raw尝试AsByTestStream参数,如下所示:

$b64 = [convert]::ToBase64String((get-content $image_path -AsByteStream -Raw))

“获取内容帮助”中甚至有一个示例解释了如何使用这些新参数。

带PowerShell 6字节的已不再是Enconding参数的有效参数。您应该结合参数Raw尝试AsByTestStream参数,如下所示:

$b64 = [convert]::ToBase64String((get-content $image_path -AsByteStream -Raw))
“获取内容帮助”中甚至有一个示例解释了如何使用这些新参数