I have a credential that is stored in a CLIXML file that I need to use to access a REST API. The API requires the credential to sent in Base64. Is there an simple way of converting the System.Security.SecureString
password object of the CLIXML into a Base64 String without having to convert it to clear text and storing in a variable?
This is what I have so far, but I really don't like having to store the password in a variable in clear text.
$m_credential = Import-Clixml -Path $xmlPath
$m_credentialBSTR = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($m_credential.Password)
$m_credentialString = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($m_credentialBSTR)
$64encode = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($m_credentialString))