2

I'm working on a way to remotely stop and restart a process via PS and WMI. I can easily stop/terminate the process but I'd like to restart the process as well and I'm having issues doing that. Below is the script I use to remotely stop/terminate the process. Any idea what I can add to this that will then restart that same process?

$comp = read-host -Prompt "Enter computer name"
(Get-WmiObject Win32_Process -ComputerName $comp | ?{     $_.ProcessName -match "PhotoScreensaver.scr" }).Terminate()

1 Answers1

1

While I'm certain you could do this with WMI, there's also an easier way. Powershell has a built in cmdlet called Invoke-Command which is for remote code execution. There's also Start-Process and Stop-Process for controlling processes.

Invoke-Command -ComputerName $Computer -ScriptBlock {
    Start-Process $ProcessName
    Stop-Process $ProcessName
}

If you're in a domain environment you might have to use the -Authentication Kerberos option to gain rights to the remote computer.

I'd also like to note that these processes will open in a different session on the remote computer. If a user is logged in, it will not execute in their session. You'd need another tool like PSExec from the SysInternals guys for that.

Colyn1337
  • 1,655
  • 2
  • 18
  • 27
  • Thank you Colyn1337! I think you've answered my question. I actually used the Invoke-Command cmndlet in a very similar way to the one you have suggested and I wasn't getting the desired result. Thanks to your input, I am certain that it is because the process is executing in a different session. Do you know of anyway I can accomplish this without having to go to the higher ups for PSExec? – EternalStudent Jan 21 '15 at 16:51
  • Every once and awhile I poke at that and haven't figured it out yet. It might have to be done at the .net layer. – Colyn1337 Jan 21 '15 at 16:55
  • Understood. Thank you again sincerely for your help. Just so I understand, if I were to be running the above script in PSExec, would it be able to remotely start the process in the same session that the current user is in? Or, would I need to add some other info? – EternalStudent Jan 21 '15 at 18:39
  • 1
    Yes..... But you wouldn't use the Invoke-Command to do that. Since PSExec runs on your local machine and remotely connects to sessions on other hosts. In your script you'd just call the psexec and wrap around it the error handling and condition settings. – Colyn1337 Jan 21 '15 at 18:44
  • Ok. Thank you. Also, just to get your thoughts...When I tried the above command in PS, I actually got an error that didn't seem to pertain to what we've talked about. Here is what I entered: $comp = read-host -Prompt "Enter computer name" (Get-WmiObject Win32_Process -ComputerName $comp | ?{ $_.ProcessName -match "PhotoScreensaver.scr" }).Terminate() Invoke-Command -ComputerName $Computer -ScriptBlock { Start-Process $ProcessName – EternalStudent Jan 21 '15 at 18:55
  • Well that posted horribly. Looks like I can't post code in the comments. Hmm... – EternalStudent Jan 21 '15 at 19:02