Get SecureString as a Plain Text Parameter

本小妞迷上赌 提交于 2021-01-29 06:51:20

问题


I'm trying to get a SecureString as plain text parameter to a command line PowerShell.

I know what is the form of the secure string. For example, the string "abc" would be a Secure String of "71289371289".

Then, I want to pass "71289371289" as a parameter to the script (Running it from command line), that would be my Secure String and then Decrypt it to a clear text to pass it to another program i'm calling from Powershell.

How would I do something like this?

Update:

I ended up using Credfile with PSCredential to persist the credentials across reboots until the script is complete.


回答1:


You can convert it back to a clear text password with SecureStringToBSTR:

Param(
    $securestring = (Read-Host -AsSecureString)
)
Write-Host "Encrypted Password: $(ConvertFrom-SecureString $securestring)"
$ClearText = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($securestring))
Write-Host "Original Password: $ClearText"


来源:https://stackoverflow.com/questions/54370482/get-securestring-as-a-plain-text-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!