garyknight
asked on
Using Powershell to Base64 Encode an MP3 Audio File
I'm trying to use Powershell to Base64 encode an mp3 audio file (for uploading via API call), but for some reason the resulting Base64 encoded text isn't right. Here is my code:
I compare my resulting text from this script with the resulting text from one of the free online base64 encoders (which I know is correct), and I get a different string of text.
Can anyone help me?
$recording="c:\myfile.mp3"
$fileContent = Get-Content $recording -Encoding UTF8
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$fileContentEncoded | set-content ("c:\output.b64")
I compare my resulting text from this script with the resulting text from one of the free online base64 encoders (which I know is correct), and I get a different string of text.
Can anyone help me?
ASKER
Thanks for your quick reply. I tried to encode using your method and got the following error. I'm using Powershell 6.0.4...not sure if that makes a difference.
Get-Content : Cannot bind parameter 'Encoding'. Cannot convert the "Byte" value of type "System.String" to type "System.Text.Encoding".
At C:\encoder.ps1:7 char:71
+ ... rt]::ToBase64String((Get-C ontent -Path $inFile -Encoding Byte)) | Set ...
+ ~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMes sage,Micro soft.Power Shell.Comm ands.GetCo ntentComma nd
Get-Content : Cannot bind parameter 'Encoding'. Cannot convert the "Byte" value of type "System.String" to type "System.Text.Encoding".
At C:\encoder.ps1:7 char:71
+ ... rt]::ToBase64String((Get-C
+ ~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMes
It seems like PowerShell 2.0 is about as tenacious as Windows XP ...
Seriously, PowerShell 2.0 is from 2009. PS 5.1 is even supported on Windows 7 / Server 2008 R2, and PS3.0 is already a lot better than PS 2.0. Consider upgrading.
Windows Management Framework 5.1
https://www.microsoft.com/en-us/download/details.aspx?id=54616
Encode on a legacy system:
Seriously, PowerShell 2.0 is from 2009. PS 5.1 is even supported on Windows 7 / Server 2008 R2, and PS3.0 is already a lot better than PS 2.0. Consider upgrading.
Windows Management Framework 5.1
https://www.microsoft.com/en-us/download/details.aspx?id=54616
Encode on a legacy system:
$inFile = "c:\myfile.mp3"
$b64File = "c:\output.b64"
[IO.File]::WriteAllText($b64File, ([System.Convert]::ToBase64String(([IO.File]::ReadAllBytes($inFile)))))
Decode on a legacy system:$b64File = "c:\output.b64"
$recoveredFile = "c:\myfile.b64.mp3"
[IO.File]::WriteAllBytes($recoveredFile, ([System.Convert]::FromBase64String(([IO.File]::ReadAllText($b64File)))))
& fc.exe $inFile $recoveredFile
@oBdA, that might be an issue with PS Core (!), not PS 2.0, as the OP stated they use 6.0.4
ASKER
Here is what I get from $PSVersionTable:
PS C:\Program Files\PowerShell\6.0.4> $Psversiontable
Name Value
---- -----
PSVersion 6.0.4
PSEdition Core
GitCommitId v6.0.4
OS Microsoft Windows 10.0.14393
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks to you both!!
@Qlemo - glad you found that link, as I thought I had read something similar, but couldn't find it. I did find this one (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/providers/filesystem-provider/get-content-for-filesystem?view=powershell-6) which has incorrect info, and the built-in help also has bad info.
@garyknight, please also select oBdA's first comment as solution, not only as Helpful. He found the basic issue, I have been just correcting the new option as he was not aware of a change there.
@footech, that's very bad, because the obvious location to look for is yours. No one will look into MIcrosoft Powershell Management for Get-Content :<, and I have just been lucky to get the correct link by searching with the correct keywords.
Encode and save:
Open in new window
Decode and compare:Open in new window