Advertisement
kminchau

ConvertFrom-SecureToPlainText

Jun 19th, 2013
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Converts a SecureString to Plain Text
  4. .DESCRIPTION
  5. The ConvertFrom-SecureToPlainText converts a SecureString to Plain Text
  6. .PARAMETER SecurePassword
  7. The SecureString that is to be converted
  8. .INPUTS
  9. System.Security.SecureString. Reqires the SecureString to convert from.
  10. .OUTPUTS
  11. System.String. ConvertFrom-SecureToPlainText returns the plain text version of the SecureString
  12. .EXAMPLE
  13. ConvertFrom-SecureToPlainText $SecurePassword
  14. .EXAMPLE
  15. $SecurePassword = Read-Host -AsSecureString "Please enter your Password"
  16. $PlainTextPassword = ConvertFrom-SecureToPlainText $SecurePassword
  17. .LINK
  18. ConvertFrom-SecureToPlainText
  19. .LINK
  20. Source: http://www.powershelladmin.com/wiki/Powershell_prompt_for_password_convert_securestring_to_plain_text
  21. #>
  22. Function ConvertFrom-SecureToPlainText
  23. {
  24.     param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][System.Security.SecureString] $SecurePassword )
  25.  
  26.     # Create a "password pointer"
  27.     $PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
  28.    
  29.     # Get the plain text version of the password
  30.     $PlainTextPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PasswordPointer)
  31.    
  32.     # Free the pointer
  33.     [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($PasswordPointer)
  34.    
  35.     # Return the plain text password
  36.     Write-Output $PlainTextPassword
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement