我在创建Powershell凭据时遇到了一些麻烦。我正在从文件中读取加密的字符串,将字符串转换为securestring并使用它来创建凭证。我得到的错误是:
New-Object:无法转换参数“1”,值为:“System.Security.SecureString”,>“PSCredential”键入“System.Security.SecureString”:“无法转换>”系统。 Security.SecureString“类型为”System.RuntimeType“的值为>类型”System.Security.SecureString“。”
以下是我正在使用的代码:
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "athenpoly", $(Read-EncString F:\Scripting\1-Dev\RSA\p_ftp_dellpoly.rsa)
Function Read-EncString {
param ([String]$InputFile)
$encrypted = Import-Clixml $InputFile
$key = (3,42,5,77,67,12,76,9,8,9,4,5,6,55,32,81,23,12,3,55,2,9,6,1,5,32,4,55,6,8,56,12)
$csp = New-Object System.Security.Cryptography.CspParameters
$csp.KeyContainerName = "SuperSecretKeyContainer"
$csp.Flags = $csp.Flags -bor [System.Security.Cryptography.CspProviderFlags]::UseMachineKeyStore
$rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider -ArgumentList 5120,$csp
$rsa.PersistKeyInCsp = $true
$password = [char[]]$rsa.Decrypt($encrypted, $true) -join "" | ConvertTo-SecureString -Key $key
}
知道我做错了吗?
答案 0 :(得分:1)
以下是我从文件中读取时设置凭据的方法:
$PassSec = ConvertTo-SecureString $($Pass) -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $($Domain + "\" + $User),$passSec
细分:
1. $Pass -> Password that is imported (Example: P@ssw0rd)
2. $Domain -> Domain name (Example: Contoso)
3. $User -> User Name (Example: Admin)
这样做的是创建变量$cred
,其用户名为Contoso\Admin
,密码为P@ssw0rd
。这最终与命令相同:
$Cred = Get-Credentials "Contoso\Admin"
仅在没有提示的情况下。