Question

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
Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top