【问题标题】:Error Creating Crediential创建凭据时出错
【发布时间】:2012-10-21 03:37:03
【问题描述】:

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

New-Object:无法转换参数“1”,其值为:“System.Security.SecureString”,>对于“PSCredential”,输入“System.Security.SecureString”:“无法转换>”System.Security。 “System.RuntimeType”类型的 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 
}

知道我做错了什么吗?

【问题讨论】:

    标签: powershell credentials


    【解决方案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"
    

    只有没有提示。

    【讨论】:

    • 谢谢尼克。我能够让它工作!感谢您的帮助。
    猜你喜欢
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2017-01-06
    相关资源
    最近更新 更多