1

There is an application on remote machine say Vesper.exe. What I am doing right now is that I remotely login to machine using powershell session.

Requirement: Need to start an application on remote machine and application should be keep running after the powershell script finish its execution

I have tried two methods and here is the summary:

  • Invoke-Command: If I use this cmdlet to start application then my script get stuck and wait for the application to finally stop
  • Start-Process: If I use this cmdlet, the application starts running but exits right away. The reason I found for this is that I was using cmdlet Remove-PSSession and then I found that I should use Exit-PSSession to keep the process alive on remote machine. But the problem with this is that once the build completes execution(or if I close the powershell window), the application on remote machine stops itself. I don't know what to do here.

Following is the powershell code:

# Create a new session on the remote machine with the above credentials
try {
    $newsession = New-PSSession -ComputerName $loadvm -Credential $loadvm_credentials
    Write-Host "Connected successfully..."
} catch [Exception] {
    echo "Error while connecting to the remote machine", $_.Exception.GetType().FullName, $_.Exception.Message
    exit 1
}
try {
    Invoke-Command -Session $newsession -Scriptblock {
      Write-Host "Cd'ing and starting Vesper on"
      cd C:\vesper_cpt
      Start-Process -file Vesper.exe -ArgumentList "-auto"
    } -ErrorAction Stop
} catch [Exception] {
    echo "Error while running the remote command", $_.Exception.GetType().FullName, $_.Exception.Message
    Remove-PSSession $newsession
    exit 1
}

Exit-PSSession
rapport89
  • 109
  • 3
  • 14

1 Answers1

0

How about running your binary as a service? Powershell will then be used only to start/stop services, which is pretty straight forward.

You can use nssm to easily turn any executable into a service.

Raf
  • 9,681
  • 1
  • 29
  • 41