提问人:Silex 提问时间:3/9/2023 更新时间:3/9/2023 访问量:905
Powershell:将 SecureString (或 PSCredential) 作为脚本参数传递
Powershell: Pass SecureString (or PSCredential) as script parameter
问:
我有一个名为这样的脚本:script.ps1
Param([Parameter(Mandatory)][SecureString] $Password)
# do some stuffs
如果我像这样运行它,没问题:
powershell.exe -File script.ps1
但是,如果我尝试显式传递 SecureString,它将不起作用:
powershell.exe -File script.ps1 -Password (Read-Host -AsSecureString)
我怀疑您只能将经典字符串作为脚本参数传递,但我无法在任何地方找到对此的确认。
我知道你可以做到这一点,而且有效:
& ./script.ps1 -Password (Read-Host -AsSecureString)
但是,您并不总是在 powershell 会话中开始。
TL/DR:使用脚本参数时,我应该使用纯字符串作为密码吗?
PS:如果对我的情况效果更好,那也没关系PSCredential
答: 暂无答案
评论
Get-Credential 'dummyUserName' |Export-CliXml path\to\output.xml
)powershell.exe -Command "& { .\script.ps1 -Password (Read-Host -AsSecureString) }"
"& { ... }"
-Command
-c
"..."
& { ... }
powershell.exe