Public/Helpers/Convert-SecureStringToString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function Convert-SecureStringToString {
    [cmdletbinding()]
    param(
        [parameter(ValueFromPipeline, Mandatory=$true)]
        [securestring]$Secure
    )
    $ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Secure)
    try {
        [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
    }
    finally {
        [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
    }
}