Alternative to base64 decode in powershell

1/14/2022

I was following a guide to connect a database to kubernetes: https://itnext.io/basic-postgres-database-in-kubernetes-23c7834d91ef

after installing Kubernetes (minikube) on Windows 10 64 bit: https://minikube.sigs.k8s.io/docs/start/

I am encountering an issue with 'base64' where the DB is trying to connect and store the password. As PowerShell doesn't recognise it. I was wondering if anyone has any ideas how I could either fix this and still use windows or an alternative means that would enable me to continue with the rest of the guide?

Error Code:

base64 : The term 'base64' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:131
+ ... postgresql -o jsonpath="{.data.postgresql-password}" | base64 --decod ...
+                                                            ~~~~~~
    + CategoryInfo          : ObjectNotFound: (base64:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

export : The term 'export' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ export POSTGRES_PASSWORD=$(kubectl get secret --namespace default pos ...
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (export:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Windows Powershell Error message

-- Tanik
kubernetes
minikube
postgresql
powershell
windows

2 Answers

1/14/2022

You're trying to execute command lines on Windows that were written for Unix-like platforms, both in terms of:

  • the external utilities they expect (base64)
  • the shell syntax they use (export POSTGRES_PASSWORD=$(...); written for POSIX-compatible shells such as bash).

Mathias' helpful answer shows you how to emulate a base64 utility in PowerShell, and you may even be able to emulate the export shell command with additional, nontrivial effort, but not all cases can be handled that way, such as a command line that uses \" to escape " characters (this will break PowerShell's syntax, try echo "3\" of snow.").

Therefore, I suggest either running your commands as-is via WSL, if feasible, or taking the time to translate the command lines into PowerShell-native equivalents.

-- mklement0
Source: StackOverflow

1/14/2022

The base64 cli found in Mac OS and some *nix distros is not available on Windows.

You could write a small function named base64 that mimics the behavior of the base64 unix tool though:

function base64 {
  # enumerate all pipeline input
  $input |ForEach-Object {
    if($MyInvocation.UnboundArguments -contains '--decode'){
      # caller supplied `--decode`, so decode 
      $bytes = [convert]::FromBase64String($_)
      [System.Text.Encoding]::ASCII.GetString($bytes)
    } else {
      # default mode, encode ascii text as base64
      $bytes = [System.Text.Encoding]::ASCII.GetBytes($_)
      [convert]::ToBase64String($bytes)
    }
  }
}

This should work as a drop-in replacement for conversion between ASCII/UTF7 text and base64:

PS ~> 'Hello, World!' |base64 --encode
SGVsbG8sIFdvcmxkIQ==
PS ~> 'Hello, World!' |base64 --encode |base64 --decode
Hello, World!

To use with your existing scripts, simple dot-source a script with the function definition in your shell before executing the others:

PS ~> . .\path\to\base64.ps1

The above will work from a script as well. If you have a multi-line paste-aware shell (Windows' default Console Host with PSReadLine should be okay), you can also just paste the function definition directly into the prompt :)

-- Mathias R. Jessen
Source: StackOverflow