PoshCode Archive  Artifact [de7ddaf80d]

Artifact de7ddaf80db4d5e73e9d07578e5c6e8342a88bf18a5ee30a7ad9277c3e25039b:

  • File Read-HostMasked.ps1 — part of check-in [a495d787b7] at 2018-06-10 12:56:18 on branch trunk — Read a string from the host using SecureString input, but output it as a plain string for use in functions that don’t accept SecureStrings (user: Joel Bennett size: 785)

# encoding: ascii
# api: powershell
# title: Read-HostMasked
# description: Read a string from the host using SecureString input, but output it as a plain string for use in functions that don’t accept SecureStrings
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Read-HostMasked
# x-poshcode-id: 104
# x-archived: 2017-04-30T12:48:39
# x-published: 2008-01-08T21:01:00
#
#
function Read-HostMasked([string]$prompt="Password") {
  $password = Read-Host -AsSecureString $prompt; 
  $BSTR = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($password);
  $password = [System.Runtime.InteropServices.marshal]::PtrToStringAuto($BSTR);
  [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR);
  return $password;
}