IT Process Automation

Expand all | Collapse all

How to Encode a file to base64 ?

Jump to Best Answer
  • 1.  How to Encode a file to base64 ?

    Posted 10-04-2016 09:53 AM

    Hello,
    I need to encode a file (not a string variable) to base64 with a process. Is there any solution for that?
    Thanks!



  • 2.  Re: How to Encode a file to base64 ?

    Posted 10-04-2016 10:52 AM

    Does anyone have an example of JavaScript to convert the file to base64?  How to run this on an agent server where the file would exist?  Thank you for your help in answering Ahras.



  • 3.  Re: How to Encode a file to base64 ?

    Posted 10-04-2016 01:05 PM

    The JS functionf to encode string to base64 (btoa()) or to decode base64 string (atob()) are not supported in PAM.

    You can create these functions yourself and use it. Or you can use powershell to encode the file if you are on a Windows Server.

     

    Encoding :

    $Text = ‘This is a secret and should be hidden’
    $Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text)
    $EncodedText =[Convert]::ToBase64String($Bytes)

     

    Decoding :

    $EncodedText = “VABoAGkAcwAgAGkAcwAgAGEAIABzAGUAYwByAGUAdAAgAGEAbgBkACAAcwBoAG8AdQBsAGQAIABiAGUAIABoAGkAZABlAG4A”
    $DecodedText = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($EncodedText))
    $DecodedText

     

     

    And if you want to convert a file, it should look something like :

     

    $fileName = "File1.txt"
    $fileContent = get-content $fileName
    $fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
    $fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
    $fileContentEncoded set-content ($fileName + ".b64")

     

     

    Code snippet are not from me but from various websites. I did not test them.

     

     

    Hope this helps.



  • 4.  Re: How to Encode a file to base64 ?

    Posted 10-05-2016 03:53 AM

    Hello POssq

     

    Yes this is very helpful. Here is the right code and it works. 

     

    $fileName = "File1.txt"
    $fileContent = get-content $fileName
    $fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
    $fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
    $fileContentEncoded | set-content ($fileName + ".b64")

     

    Thanks. But When I set this script on Run_Script operator, operator not getting fail but not ending. I attached screenshot

     

     

     

     



  • 5.  Re: How to Encode a file to base64 ?
    Best Answer

    Posted 10-05-2016 04:48 AM

    When I create .ps1 file and set .cmd run script operator it workled.

     

    here is the cmd command:

     

    powershell.exe -executionpolicy remotesigned -File "C:\Users\service.servicedesk\Desktop\convertbase65.ps1"

     

    here is the .ps1 file:

     

    $fileName = "C:\UnzippedFiles\meltem.jpeg"
    $fileContent = get-content $fileName
    $fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
    $fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
    $fileContentEncoded | set-content($fileName + ".b64")

     

     

    Thanks!



  • 6.  Re: How to Encode a file to base64 ?

    Posted 10-05-2016 08:01 AM

    This is what i was about to tell you. You need to run powershell.exe to exec a ps1 file

     

    I'm my env, i also have problem when trying to run powershell script with "run script"

     

    Anyway, glade you achieved what you wanted!

     

    Have a nice day