【问题标题】:Send securestring to function in a powershell module发送安全字符串以在 powershell 模块中运行
【发布时间】:2021-05-14 19:06:34
【问题描述】:

我在 Powershell 模块中有一个函数,它应该与 SimplySql 建立一个数据库会话。 SimplySql 接受一个凭证对象。现在我有几个问题,将密码发送到函数中。我尝试了什么:

  1. 当使用 [string]$password 参数时,我收到警告 "Parameter '$password' should use SecureString, otherwise this will expose sensitive information"。绝对有道理,所以我将参数更改为securestring。

  2. 我用"$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,“是”它是一个安全字符串。

  3. 因为我没有找到任何解决方案,所以我尝试将[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)。但随后我收到提示输入我的密码手册。
  • 您能否展示创建凭证对象并调用脚本/函数的完整脚本?一定少了点什么
  • 嗨,抱歉耽搁了,放假了 ;-)。 @马

标签: powershell securestring


【解决方案1】:

抱歉耽搁了,放假了;-)。 我现在再次尝试为您提供完整的代码示例。我不知道我改变了什么,但知道它的工作原理。 我真的很确定,我是用它创建的

$_secString = ConvertTo-SecureString "test" -AsPlainText -Force
[pscredential]$_credObject = New-Object System.Management.Automation.PSCredential("mydbuser", $_secString)

最后一次。我今天用相同的命令完成了它,它可以工作。奇怪的。误会偷走了你的时间....

真的很奇怪。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2016-03-23
    • 2020-07-19
    相关资源
    最近更新 更多