title: Error Creating Crediential
body: <p>I'm having some trouble creating a Powershell credential. I am reading an encrypted string from a file, converting the string to a securestring and using that to create the credential. The error I get is: </p>
  
  <blockquote>
    <p>New-Object : Cannot convert argument "1", with value: "System.Security.SecureString", >for "PSCredential" to type "System.Security.SecureString": "Cannot convert >the "System.Security.SecureString" value of type "System.RuntimeType" to >type "System.Security.SecureString"."</p>
  </blockquote>
  
  <p>Here is the code I'm using:</p>
  
  <pre><code>$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 
  }
  </code></pre>
  
  <p>Any idea what I am doing wrong?</p>