安全字符串Powershell
尝试将securestring传递到powershell脚本时遇到问题,但返回时出现此错误 无法处理参数“Password”上的参数转换。 无法将类型为“System.String”的“ConvertToSecureString”值转换为类型为“System.Security.SecureString” 它调用的脚本是这样的安全字符串Powershell,powershell,Powershell,尝试将securestring传递到powershell脚本时遇到问题,但返回时出现此错误 无法处理参数“Password”上的参数转换。 无法将类型为“System.String”的“ConvertToSecureString”值转换为类型为“System.Security.SecureString” 它调用的脚本是这样的 param( ##MasterKey for KeePass currently stored in a DB. [Parameter(Positio
param(
##MasterKey for KeePass currently stored in a DB.
[Parameter(Position=0,mandatory=$true)]
[string] $ServiceAccountOU,
##Account to be created in AD
[Parameter(Position=1,mandatory=$true)]
[string] $Account,
[Parameter(Position=2,mandatory=$true)]
[SecureString] $Password
)
NEW-ADUSER -Name $Account -AccountPassword $Password -Path $SrviceAccountOU -Enabled $True -PasswordNeverExpires $True -CannotChangePassword $True
您的密码未作为安全字符串传递给PS。因此,您必须转换并传递它 替换此项:
NEW-ADUSER -Name $Account -AccountPassword $Password -Path $SrviceAccountOU -Enabled $True -PasswordNeverExpires $True -CannotChangePassword $True
致:
最好的方法是直接传递凭据:
$User = "MyUserName"
$File = "C:\Temp 2\Password.txt"
$password = Get-Content "C:\Passwords\password.txt" | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential("Luke",$password)
$MyCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
New-ADUser -credential $MyCredential # Followed by whatever you need except the username and password.
若要通过PowerShell的CLI传递
[securestring]
()实例(在Windows PowerShell中,在PowerShell(Core)7+中,必须将要执行的命令(-command
/-c
)作为[1]
- 注意:虽然使用
(假设输入字符串不是通过纯文本表示(如问题所示)获得的)无疑比使用纯文本密码要好,但不鼓励在新项目中使用[securestring]
,因为它在类Unix平台上不提供保护,在Windows上只提供有限的保护。见和[securestring]
powershell -c {
$scriptFile, $passThruArgs = $args
& $scriptFile @passThruArgs
} -args $ADAccountScript, $ServiceAccountOU, $AgentAccount, $SecureServiceAccountPassword
注意,这依赖于位置参数的传递
如果您想像在自己的尝试中那样使用命名参数,那么必须求助于基于哈希表的方法
[1] 如果将
-Command
/-c
参数作为字符串传递-这是从PowerShell外部调用PowerShell的CLI时的唯一选项-将[securestring]
作为其字符串表示形式传递,这是其完整的类型名称:'System.Security.securestring'
;也就是说,实际的“安全”内容丢失。相反,传递脚本块会在后台触发基于CLIXML的序列化,在后台传递和接收对象而不仅仅是文本是受支持的-有关详细信息,请参阅。错误说明传递的是字符串,而不是安全字符串,请执行此操作
NEW-ADUSER-Name$Account-AccountPassword(ConvertTo SecureString-AsPlainText-Force)$Password
但这就是传入的内容?$secureservicecomportpassword=ConvertTo SecureString“somethingstong!”-AsPlainText-force请允许我向新来者提供标准建议:如果你有答案,你将通过向未来读者展示解决问题的方法来帮助他们。要接受答案,请单击大标题✓ 答案左侧大数字下方的符号(您将获得2个信誉点)。如果您至少有15个信誉点,您还可以向上投票其他有用的答案(也可以选择接受的答案)。如果您的问题尚未解决,请提供反馈,或者,如果您自己找到了解决方案。
$User = "MyUserName"
$File = "C:\Temp 2\Password.txt"
$password = Get-Content "C:\Passwords\password.txt" | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential("Luke",$password)
$MyCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
New-ADUser -credential $MyCredential # Followed by whatever you need except the username and password.
powershell -c {
$scriptFile, $passThruArgs = $args
& $scriptFile @passThruArgs
} -args $ADAccountScript, $ServiceAccountOU, $AgentAccount, $SecureServiceAccountPassword
# Create the hashtable whose entries map onto the target
# script's parameters.
$htArgs = @{
ServiceAccountOU = 'foo'
Account = 'bar'
Password = ConvertTo-SecureString "somethingstrong!" -AsPlainText -Force
}
powershell -c {
$scriptFile, $passThruArgs = $args
& $scriptFile @passThruArgs
} -args $ADAccountScript, $htArgs