PowerShell で対話式に文字列入力する場合は Read-Host を使うことが多いです。-Prompt を指定すればガイダンスも表示できるので便利ですね。
| PS C:\> 
		$Message 
		= 
		Read-Host 
		-Prompt 
		"Input message" Input message: abcd PS C:\> $Message abcd  | 
	
パスワードとかのシークレット文字列だと入力文字が見えてしまうので、-AsSecureString を指定します。
| PS C:\> 
		$SecretMessage 
		= 
		Read-Host 
		-Prompt 
		"Input secret message" 
		-AsSecureString Input secret message: ***** PS C:\> $SecretMessage System.Security.SecureString  | 
	
入力文字が伏字になりますが、得られたのはセキュアストリングなので、このままでは入力された文字列を扱うことができません。
平文を得るために、セキュアストリングから平文にコンバートする関数を作ります。
		 | 
	
関数の引数にセキュアストリングを渡すと、平文が得られます。
| PS C:\> 
		$PlainMessage 
		= 
		SecureString2PlainString 
		$SecretMessage PS C:\> $PlainMessage abcde  | 
	
![]()
![]()
Copyright © MURA All rights reserved.