title: Powershell Invoke-Command with PSCredential Cannot process argument transformation on parameter 'Credential'
body: <p>I'm trying to run a powershell invoke-command call with a PSCredential as parameter.</p>
  
  <p>But the call fails with the error:</p>
  
  <blockquote>
    <p>Invoke-Command : Cannot process argument transformation on parameter
    'Credential'. userName"</p>
  </blockquote>
  
  <p>Here is the detailed output I get :</p>
  
  <pre><code>error 17-Jun-2015 14:33:53 Invoke-Command : Cannot process argument transformation on parameter 'Credential'. userName
  error 17-Jun-2015 14:33:53 At C:\Windows\system32\config\systemprofile\AppData\Local\Temp\PRISMA-AMR-JOB1-68-ScriptBuildTask-7900604773745573277.ps1:31 char:71
  error 17-Jun-2015 14:33:53 +     Invoke-Command "sqlcmd -E -S "$server" -Q `"$query`" " -Credential &lt;&lt;&lt;&lt;  
  error 17-Jun-2015 14:33:53 ($credential)
  error 17-Jun-2015 14:33:53     + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParentContain 
  error 17-Jun-2015 14:33:53    sErrorRecordException
  error 17-Jun-2015 14:33:53     + FullyQualifiedErrorId : ParameterArgumentTransformationError,Microsoft.P 
  error 17-Jun-2015 14:33:53    owerShell.Commands.InvokeCommandCommand
  </code></pre>
  
  <p>Basically I'm just trying to run a sqlcmd with impersonation through a Invoke-Command call with PSCredential.</p>
  
  <p>Any idea on what's going on here ?</p>
  
  <p>Edit:
  Here is the powershell code:</p>
  
  <pre><code>function GetPSCredential($userId){
      $userIdFileNameWoExt = $userId.Replace("\",".")
      $password = (Get-Content "somefile.pwd" | ConvertTo-SecureString -Key (Get-Content "somefile.key"))
      $psCredential = New-Object `
          -TypeName System.Management.Automation.PSCredential `
          -ArgumentList "$userId", $password
      return $psCredential
  }
  
  function ImpersonateSql ($credential, $server, $query) {
      Invoke-Command "sqlcmd -E -S "$server" -Q `"$query`" " -Credential ($credential)
  }
  
  ...
  
  ImpersonateSql($defaultCredential, $serverInstance, "SOME SQL QUERY HERE")
  </code></pre>