1

我在创建 Powershell 凭据时遇到了一些问题。我正在从文件中读取加密字符串,将字符串转换为安全字符串并使用它来创建凭据。我得到的错误是:

新对象:无法转换参数“1”,其值为:“System.Security.SecureString”,>“PSCredential”以键入“System.Security.SecureString”:“无法转换>“System.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 
}

知道我做错了什么吗?

4

1 回答 1

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"

只有没有提示。

于 2012-11-01T15:11:20.680 回答