Christer Löwing
Christer Löwing

Reputation: 75

Powershell start-process can't find file

I am trying to upgrade a server with a particular application from a client by PowerShell remote:

Invoke-Command -ComputerName $server -Credential $mycreds {Start-Process -FilePath "C:\temp\xxx.exe"   -ArgumentList "-default", "-acceptEULA" -wait }

Whatever I try, I get messages like "Can't find the file specified..." what do I do wrong? FilePath is on the local (client) computer.

Upvotes: 1

Views: 1421

Answers (1)

mklement0
mklement0

Reputation: 437418

Your C:\temp\xxx.exe executable must be present on the server (the remote machine) for your command to work, because that is where your script block ({ ... }) executes.

Note: By contrast, if you use Invoke-Command with the -FilePath parameter in order to run a locally present script file (.ps1) remotely, PowerShell automatically copies it to the remote machine; from the docs: "When you use this parameter, PowerShell converts the contents of the specified script file to a script block, transmits the script block to the remote computer, and runs it on the remote computer."

To copy the executable there from your local (client-side) machine, you need a 4-step approach (PSv5+, due to use of Copy-Item -ToSession[1]):

  • Create a remoting session to $server explicitly, using New-PSSession

  • Copy the local (client-side) executable to that session (the remote computer) with Copy-Item and its -ToSession parameter

  • Run your Invoke-Command command with the -Session parameter (rather than
    -ComputerName) in order to run in the explicitly created session (this isn't strictly necessary, but there's no need to create another (ad hoc) session).

  • Run Remove-PSSession to close the remote session.

Important: In a PowerShell remoting session, you cannot run external programs that require interactive user input:

  • While you can launch GUI applications, they invariably run invisibly.

  • Similarly, interactive console applications aren't supported (although output from console applications is received by the client).

However, interactive prompts from PowerShell commands are supported.

To put it all together:

# Specify the target server(s)
$server = 'w764' # '.'

# Establish a remoting session with the target server(s).
$session = New-PSSession -ComputerName $server

# Copy the local executable to the remote machine.
# Note: Make sure that the target directory exists on the remote machine.
Copy-Item C:\temp\xxx.exe -ToSession $session -Destination C:\temp

# Now invoke the excutable on the remote machine.
Invoke-Command -Session $session {
  # Invoke *synchronously*, with -Wait.
  # Note: If the program is a *console* application,
  #       you can just invoke it *directly* - no need for Start-Process.
  Start-Process -Wait -FilePath C:\temp\xxx.exe -ArgumentList "-default", "-acceptEULA"
}

# Close the remote session.
# Note: This will terminate any programs that still
#       run in the remote session, if any.
Remove-PSSession $session

[1] If you're running Powershell v4 or below, consider downloading psexec.

Upvotes: 1

Related Questions