1

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
andrew.carpenter
  • 516
  • 2
  • 6
  • 14
  • This may be what you're looking for. They suggest using WMI rather than remoting. http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/ead19612-5e7b-4012-8466-0d650232c7a5/ – Adam Driscoll Feb 12 '12 at 17:37

5 Answers5

1

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.

luwei
  • 300
  • 3
  • 3
  • 1
    The `start-process` part would only run the exe in your machine. Not the remote machine because you are not using the session to run it. – Sid Feb 07 '19 at 05:51
1

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
Ambrose Leung
  • 3,704
  • 2
  • 25
  • 36
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.

Tom
  • 1,611
  • 1
  • 13
  • 16
0

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

Community
  • 1
  • 1
Ocelot
  • 1,733
  • 4
  • 29
  • 53
0

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

Adam Driscoll
  • 9,395
  • 9
  • 61
  • 104
  • 1
    Thanks for the suggestion. The -wait parameter does keep the thread running as I need, but the program needs to be able to run while the script continues past the start-process command. Using -wait it won't move on until the program is closed. – andrew.carpenter Feb 11 '12 at 16:06