Powershell Invoke-Command with PSCredential Cannot process argument transformation on parameter 'Credential'

9.6k views Asked by At

I'm trying to run a powershell invoke-command call with a PSCredential as parameter.

But the call fails with the error:

Invoke-Command : Cannot process argument transformation on parameter 'Credential'. userName"

Here is the detailed output I get :

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 <<<<  
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

Basically I'm just trying to run a sqlcmd with impersonation through a Invoke-Command call with PSCredential.

Any idea on what's going on here ?

Edit: Here is the powershell 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")
1

There are 1 answers

0
Varvara Kalinina On BEST ANSWER

You have a mistake calling ImpersonateSql function. You should not use any parenthesis or commas for specifying parameters when calling function (unless you deliberately want to pass a subexpression or an array as an argument), it's not like C# or javascript function you are used to. Look at an example:

function func ($a, $b, $c) {
    "a: {0}, b: {1}, c: {2}" -f $a, $b, $c
}

PS C:\Windows\system32> func(5,6,7)
a: System.Object[], b: , c: 

PS C:\Windows\system32> func 5,6,7
a: System.Object[], b: , c: 

PS C:\Windows\system32> func 5 6 7
a: 5, b: 6, c: 7

In your script you are passing only one argument to you function - and it's an array of objects, which naturally won't do as a -Credential parameter for Invoke-Command cmdlet. So, first of all you have to correct your script like this

ImpersonateSql $defaultCredential $serverInstance "SOME SQL QUERY HERE"

I would even say that you'd better define Param() block inside your function, it gives you more control over passing arguments to function.