ユーザに文字列を入力させる(2)

ユーザに文字列を入力させる(1) - PowerShell Memoの続きです。

SecureStringとは?

「read-host」の「SecureString」オプションを使うと、戻り値の型は「System.Security.SecureString」になります。
「SecureString」はメモリ上で暗号化されるセキュアなオブジェクトです。

MSH C:\> $password = read-host EnterPassword -SecureString
EnterPassword: *******
MSH C:\> $password
System.Security.SecureString

パスワードを格納した変数を表示しようとすると、「System.Security.SecureString」と表示され、内容は分かりません。

SecureStringの操作方法

SecureStringを操作するには「System.Runtime.InteropServices.Marshal」クラスを利用します。

MSH C:\> $password = read-host EnterPassword -SecureString
EnterPassword: *******
MSH C:\> $ptr = [System.Runtime.InteropServices.Marshal]
 ::SecureStringToBSTR($password)
MSH C:\> [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr)
newpops