0

I've a simple command that I need to execute via PowerShell on remote machine.

E:\Programs\GMM\bin\GMMFailoverTool.exe -mssql="Server=SomeServer;Database=GMM01" list

The problem I'm having is executing it properly with PowerShell even without trying to do this via Invoke-Command.

$binary = "E:\Programs\GMM\bin\GMMFailoverTool.exe"
$command = "-mssql=`"Server=SomeServer;Database=gmm01`" list"

Write-Host BINARY: $binary -ForegroundColor Yellow
write-Host ARGS: $command -ForegroundColor Yellow
Write-Host FullCommand: $binary $command -ForegroundColor Yellow
& $binary $command

Output:

BINARY: E:\Programs\GMM\bin\GMMFailoverTool.exe
ARGS: -mssql="Server=SomeServer;Database=gmm01" list
FullCommand: E:\Programs\GMM\bin\GMMFailoverTool.exe -mssql="Server=SomeServer;Database=gmm01" list

And the return of the command is like it didn't get any parameters at all (or those were incorrect).

The question is how to properly pass those arguments where $command is already defined as it should? If I do it by hand without having it all in variables it works…

& "E:\Programs\GMM\bin\GMMFailoverTool.exe" -mssql="Server=SomeServer;Database=gmm01" list

1 Answer 1

2

Pass the arguments as an array:

$command = '-mssql="Server=SomeServer;Database=gmm01"', 'list'
& $binary $command

Also, I had some situations where the only way of correctly passing arguments to an external program was to run the command with cmd.exe:

$command = '-mssql="Server=SomeServer;Database=gmm01" list'
cmd /c "$binary $command"

To run the command remotely you need to either define the variables inside the scriptblock:

Invoke-Command -Computer 'remotehost.example.com' -ScriptBlock {
  $binary  = ...
  $command = ...
  & $binary $command
} 

or (perhaps better, if the value of $command is generated by some other function) pass them into the scriptblock via the parameter -ArgumentList:

$binary  = ...
$command = ...

Invoke-Command -Computer 'remotehost.example.com' -ScriptBlock {
  & $args[0] $args[1]
} -ArgumentList $binary, $command

because the content of the scriptblock doesn't know anything about the rest of your script.

Sign up to request clarification or add additional context in comments.

9 Comments

When trying & $binary --% $command the output is GMMFailoverTool.exe : Unknown options: $command so it seems like it tries to take it directly. Array approach doesn't change anything either.
*slaps self* Sorry, my mistake. Of course --% won't work, because it's supposed to treat everything after as a literal string. See updated answer.
It works only with Array approach. For both approaches. CMD and the other one. I just had conversion in other part of the code where the return was Array and forcing it into the string.
Is there any way to give it "RUN PATH" ? as in go into directory and then execute it?
Also if i copy the code into Invoke-Command it doesn't work. Is there anything special I should be doing ? except making sure it's an array?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.