Private/Strings/ConvertFrom-SecureString.ps1
<#
.SYNOPSIS Converts a secure string to an (un)encrypted standard string. .PARAMETER SecureString Specifies the secure string to convert to an encrypted standard string. .PARAMETER SecureKey Specifies the encryption key as a secure string. The secure string value is converted to a byte array before being used as the key. .PARAMETER Key Specifies the encryption key as a byte array. .PARAMETER AsPlainText Specified the returned result should be an unencrypted standard string. #> Function ConvertFrom-SecureString { [CmdletBinding(DefaultParameterSetName="Secure", HelpUri="http://go.microsoft.com/fwlink/?LinkID=113287")] Param( [Parameter(Position = 1, Mandatory = $true, ValueFromPipeline = $true)] [securestring]$SecureString , [Parameter(ParameterSetName="Secure", Position = 2)] [securestring]$SecureKey , [Parameter(ParameterSetName="Open")] [byte[]]$Key , [Parameter(ParameterSetName="Decrypt")] [switch]$AsPlainText ) Begin { Try { $outBuffer = $null If ($PSBoundParameters.TryGetValue("OutBuffer", [ref]$outBuffer)) { $PSBoundParameters["OutBuffer"] = 1 } $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand("Microsoft.PowerShell.Security\ConvertFrom-SecureString", [System.Management.Automation.CommandTypes]::Cmdlet) If ($PSBoundParameters["AsPlainText"]) { $scriptCmd = { & ConvertFrom-SecureString2 $SecureString } } Else { $scriptCmd = { & $wrappedCmd @PSBoundParameters } } $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin) $steppablePipeline.Begin($PSCmdlet) } Catch { Throw } } Process { Try { $steppablePipeline.Process($_) } Catch { Throw } } End { Try { $steppablePipeline.End() } Catch { Throw } } <# .ForwardHelpTargetName Microsoft.PowerShell.Security\ConvertFrom-SecureString .ForwardHelpCategory Cmdlet #> } <# .SYNOPSIS Converts a secure string to an unencrypted standard string. .PARAMETER SecureString Specifies the secure string to convert to an encrypted standard string. #> Function ConvertFrom-SecureString2 { Param( [Parameter(Position = 1, Mandatory = $true, ValueFromPipeline = $true)] [securestring]$SecureString ) Process { $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString) [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) } } |