2012-10-31 36 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