Powershell Remote Invoke-Command Start-Process App Immediately Closes After Launch

andrew.carpenter picture andrew.carpenter · Feb 11, 2012 · Viewed 25.9k times · Source

I am working on a script to remotely kill two processes, delete some folders, and launch a service as an application. Eventually this will be deployed to run against multiple servers, but I'm currently testing it against a single server. Everything works great except for the final step which is to launch the remote service (which is being run as an application using the -cl argument due to some compatibility issues with it running as a service). The application launches via the script but immediately shuts back down as the script moves on to the next step. I'm a total noob so I've been digging quite a bit but have had no luck finding a solution. It seems like I may end up having to launch the process in a new runspace, but I'm not having any luck finding a good noob guide for doing that either.

Also the script performs just as it should when localized and run on the machine.

Here's what I've got

$a = Get-ChildItem "\\TestMachine\c$\DataFiles"

$pass = cat "C:\cred.txt" | convertto-securestring 

$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "username",$pass

if ($a.Count -gt 10)
{
Invoke-Command -ComputerName TestMachine -cred $mycred -scriptBlock {stop-process -name TestProcess -force -EA SilentlyContinue}

Invoke-Command -ComputerName TestMachine -cred $mycred -scriptBlock {stop-process -name Winword -force -EA SilentlyContinue}

Get-ChildItem -Path \\TestMachine\c$\WordDocs -Recurse -EA SilentlyContinue | Where-Object {$_.PsIsContainer} | Remove-Item -Recurse -Force -EA SilentlyContinue

Invoke-Command -ComputerName TestMachine -cred $mycred -scriptBlock {Start-Process "C:\Program Files (x86)\Test\TestUploadManager\TestUploadManager.exe" -Verb Runas -ArgumentList -cl}

echo "Success: TestMachine Was successfully reset"
}

else

{
echo "Failed: Reset was not necessary"
}

exit

Answer

Tom picture Tom · Feb 13, 2012

You could try to open a remote-session with Enter-PSSession and then use the cmdlet Start-Process without Invoke-Command

You could also run the program like this & "C:\Program Files (x86)\Test\TestUploadManager\TestUploadManager.exe" -cl (within the remote-session)

Finally you should exit the session with Exit-PSSession

Another possibility is to run the script "locally". Write the script as you would run it on a local machine. Copy it to an accessable share. Then use a remoting method to run the script on the machine.