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

ぐ巨炮叔叔 提交于 2019-12-24 08:31:46

问题


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

回答1:


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.




回答2:


The solution suggested by Tom works for me. Here are the commands I used:

  New-PSSession -computername TestMachine
  Start-Process yourservice.exe
  Exit-PSSession

Make sure to use Exit-PSSession and not Remove-PSSession so that the process yourservice.exe remains active after exiting the session.




回答3:


This WMI solution worked best for me: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/ead19612-5e7b-4012-8466-0d650232c7a5/invokecommand-and-startprocess?forum=ITCG – Adam Driscoll (pointed to the same link)

Use:

([WMICLASS]"\\localhost\ROOT\CIMV2:win32_process").Create("something.exe argument1Here")

Instead of:

Start-Process



回答4:


In the Start-Process call try using the -Wait parameter.




回答5:


I had the exact same issue. Got it to work cleanly, with a combination of [WMICLASS] 's create() and Start-Process.

Check my answer here



来源:https://stackoverflow.com/questions/9236949/powershell-remote-invoke-command-start-process-app-immediately-closes-after-laun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!