RonBurgundy
RonBurgundy

Reputation: 55

Passing parameters to Invoke-Command

I'm having issues passing parameters to Invoke-Command, I've tried using -Args and -ArgumentList to no avail.

function One {
 $errcode = $args
 $username = "Ron"
 $password = ConvertTo-SecureString -String "Baxter" -AsPlainText -Force
 $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
 $cred
 $Result = Invoke-Command -ComputerName MyPc -ScriptBlock { & cmd.exe /c "C:\Scripts\test.bat" Param1 $errcode ; $lastexitcode}  -Credential $cred
 echo $result
}


One 10

Upvotes: 0

Views: 823

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13207

You can update your function to pass in your parameter as $errcode rather than using $args, this is better code as it's less confusing. (I'd recommend readng up on parameters and functions as it'll certainly help)

Then you need to pass $errcode into Invoke-Command using the ArgumentList parameter, and use $args[0] in its place:

function One ($errcode) {
    $username = "Ron"
    $password = ConvertTo-SecureString -String "Baxter" -AsPlainText -Force
    $cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

    $Result = Invoke-Command -ComputerName MyPc -ScriptBlock { & cmd.exe /c "C:\Scripts\test.bat" Param1 $args[0] ; $lastexitcode} -Credential $cred -ArgumentList $errcode
    echo $Result
}
One 10

Upvotes: 1

Related Questions