I am using Powershell to request a password from a user if not provided, based upon another answer. I then pass the password (no pun intended) to some program, do-something.exe
. Rather than have an intermediate variable, I tried to convert the password to a normal string "inline":
[CmdletBinding()]Param( [Parameter(Mandatory, HelpMessage="password?")] [SecureString]$password)do-something password=${[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))}
That doesn't work. I could only get it to work using a temporary, intermediate variable:
[CmdletBinding()]Param( [Parameter(Mandatory, HelpMessage="password?")] [SecureString]$password)$pwd=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))do-something.exe password=$pwd
Did I make a mistake trying to evaluate the password inline when invoking do-something.exe
? How can this be done?