0

I'm trying to run an exe on a remote computer using powershell without success. when I run locally on the server like so it is running successfully

"C:\Program Files\Syncovery\Syncovery.exe" /RUNX="myJobName" /S

I've tried several ways running this remotely, but im getting all sorts of errors

$exe = "C:\Program Files\Syncovery\Syncovery.exe"
$arguments = '/RUNX="myJobName" /S'
$proc = [Diagnostics.Process]::Start($exe, $arguments)
$proc.WaitForExit()

or

Invoke-Command -ComputerName serverName -ScriptBlock {$proc.WaitForExit()}

*Error: Exception calling "Start" with "2" argument(s): "The system cannot find the file specified" At line:3 char:1

  • $proc = [Diagnostics.Process]::Start($exe, $arguments)*

or

Invoke-Command -ComputerName serverName -ScriptBlock {&$exe $arguments}

Error: the expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object.

holder
  • 93
  • 1
  • 10
  • 1
    I believe you need to place all of the code in the scriptblock, not just the `$proc.WaitForExit()` line. – Daniel Feb 14 '21 at 08:06
  • 1
    In short: Because the script block executes _remotely_, it knows nothing about the caller's _local_ variables. The simplest way to reference the caller's variable values is with the `$using:` scope - see [this answer](https://stackoverflow.com/a/35492616/45375). – mklement0 Feb 14 '21 at 09:36
  • sorry but couldn't figure out how to use '$using' in my case. – holder Feb 15 '21 at 11:42
  • I dont have to use variables. I just placed the exe path in a variable for comfort reasons. the external exe is not recognized inside the scriptblock. `$syn = &$exe $arguments` `Invoke-Command -ComputerName $s { $Using:syn } -ArgumentList $arguments` – holder Feb 15 '21 at 12:00

0 Answers0