2012-10-31 17 views
1

我在创建Powershell信任时遇到了一些麻烦。我正在从一个文件读取一个加密的字符串,将该字符串转换为一个安全字符串并使用它来创建证书。我得到的错误是:创建信任错误

新对象:无法转换参数“1”,与价值:“为System.Security.SecureString”>为“PSCredential的”输入“为System.Security.SecureString”:“无法将类型为“System.RuntimeType”的“System.Security.SecureString”值转换为>键入“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 
} 

任何想法,我做错了什么?

回答

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: [email protected]) 
2. $Domain -> Domain name (Example: Contoso) 
3. $User -> User Name (Example: Admin) 

这样做是一个用户名Contoso\Admin用的密码创建变量$cred[email protected]。这最终与命令相同:

$Cred = Get-Credentials "Contoso\Admin" 

只有没有提示。

+0

谢谢尼克。我能够得到这个工作!我很感激帮助。 – mack