【发布时间】:2021-05-14 19:06:34
【问题描述】:
我在 Powershell 模块中有一个函数,它应该与 SimplySql 建立一个数据库会话。 SimplySql 接受一个凭证对象。现在我有几个问题,将密码发送到函数中。我尝试了什么:
-
当使用 [string]$password 参数时,我收到警告
"Parameter '$password' should use SecureString, otherwise this will expose sensitive information"。绝对有道理,所以我将参数更改为securestring。 -
我用
"$SecurePassword = "myPassword" | ConvertTo-SecureString -AsPlainText -Force"创建了安全字符串,再次导入了模块并调用了函数。现在我收到以下消息:Cannot process argument transformation on parameter 'Password'. Cannot convert the "System.Security.SecureString" value of type "System.String" to type "System.Security.SecureString"。我用$myLocalSecString.GetType()检查了SecureString,“是”它是一个安全字符串。 -
因为我没有找到任何解决方案,所以我尝试将
[PSCredential]$cred设置为输入对象。我用[pscredential]$_credObject = New-Object System.Management.Automation.PSCredential ("myusername", $SecurePassword)创建了对象。当我现在调用该函数时,我收到以下消息:
PowerShell credential request Enter your credentials. Password for user System.Management.Automation.PSCredential:
我认为我在场景 2 和 3 中以错误的方式创建了 SecureString,但我没有做错什么。该脚本应该可以在 Powershell Core 上运行。
如果能在这里得到一个很好的提示会很棒。
提前致谢
戴夫
【问题讨论】:
-
“SimplySql 需要一个凭证对象”——你能详细说明一下吗?如果目标模块接受
[pscredential],那么你能做的最好的事情就是接受一个并将其按原样传递给模块,永远不要触摸明文密码 -
是的,SImplySql 需要一个凭证对象。我尝试创建它并将其提供给我的函数,如上所述(子弹编号 3)。但随后我收到提示输入我的密码手册。
-
您能否展示创建凭证对象并调用脚本/函数的完整脚本?一定少了点什么
-
嗨,抱歉耽搁了,放假了 ;-)。 @马