Powershell Security.SecureString参数不接受字符串
将字符串作为参数传递到此参数时:Powershell Security.SecureString参数不接受字符串,powershell,passwords,parameter-passing,Powershell,Passwords,Parameter Passing,将字符串作为参数传递到此参数时: [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [Security.SecureString]$password=$(Throw "Password required.") 我在使用-password参数时出现此错误 无法将System.String类型转换为System.Security.SecureString类型 如果我没有传递-password参数,则会显示一个提示,提示接受输入。您不能传
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[Security.SecureString]$password=$(Throw "Password required.")
我在使用-password
参数时出现此错误
无法将System.String类型转换为System.Security.SecureString类型
如果我没有传递
-password
参数,则会显示一个提示,提示接受输入。您不能传递字符串;您必须传递一个安全字符串
function Get-PasswordThing {
Param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[Security.SecureString]$password=$(Throw "Password required.")
)
Process {
Write-Host "cool"
}
}
[string]$password = "hello"
[Security.SecureString]$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
Get-PasswordThing -password $securePassword
# inline
Get-PasswordThing -password (ConvertTo-SecureString $password -AsPlainText -Force)
另一种选择是将密码作为字符串,然后在函数中将其转换为SecureString
Function DoSomething {
Param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]$password=$(Throw "Password required.")
)
$password = $password | ConvertTo-SecureString -AsPlainText -Force
#your script continues here
}
问题是什么?您需要一个安全字符串并传递一个字符串以获取此错误?您在主题(?)中回答了自己的问题。在某个时候,必须有人创建安全字符串,您可以获取一个字符串并自己执行,也可以让调用方执行。但这不会自动发生。这很好用。一定还有别的办法,因为我一次都没转换就成功了。