Solved

Antonymous Process power shell script

Posted on 2015-02-18
9
96 Views
Last Modified: 2015-02-19
I've been looking for a script that can check if a certain process is running and if it's not start it back up and keep monitoring if it stops start it back up again.
0
Comment
Question by:Network Zero
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 40619177
All you really need is an infinite loop.
while ($true) {
  $Process = Get-Process ExeName
  if (-not $Process) {
    # Insert code to start the process here. Could be as simple as:
    Start-Process ExeName
  }
  # Sleep a little while to control the speed of the loop
  Start-Sleep -Seconds 10
}

Open in new window

Now the problem of monitoring is moved a step back, what watches the watcher?

Chris
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40619196
In fact I would use a process termination event trigger instead. But that does not change much, it would still be an "endless loop" monitoring the process.
0
 
LVL 7

Author Comment

by:Network Zero
ID: 40619269
Qlemo how it look with a process termination event trigger?
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 7

Author Comment

by:Network Zero
ID: 40619272
Like this?

---------

while ($true) {
  $Process = Get-Process ExeName
  if (-not $Process) {
    # Insert code to start the process here. Could be as simple as:
    Start-Process Time.exe
  }
  # Sleep a little while to control the speed of the loop
  Start-Sleep -Seconds 10
}
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40619348
No, it is much more simple. E.g. for NotePad, starting Calc (to not get into an endless loop when testing):
Register-WmiEvent -query 'select * from Win32_ProcessStopTrace where Processname=''notepad.exe''' -action { Start-Process Calc.exe }

Open in new window

Keep the PS you run that in opened, otherwise the registration will be removed again. In an all-in-one command, e.g. in Task Scheduler, you would use
PowerShell -NoExit -Command "& {Register-WmiEvent -query 'select * from Win32_ProcessStopTrace where Processname=''notepad.exe''' -action { Start-Process Calc.exe }}"

Open in new window

0
 
LVL 7

Author Comment

by:Network Zero
ID: 40619932
Qlemo when I ran

Register-WmiEvent -query 'select * from Win32_ProcessStopTrace where Processname=''notepad.exe''' -action { Start-Process Calc.exe }

Open in new window


It came up with calculators like a million times I had to shut down my computer is there any way to prevent it from opening more then once.

or will the task manager stop that. I want to run it on a server so don't want it to crash with a zillion exe's lol.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40620074
This should only start one (!) calculator if Notepad is closed (!).
0
 
LVL 7

Author Comment

by:Network Zero
ID: 40620221
I ran the script

I had IE not running,  nothing happened

I opened IE and closed it.

And calc opened. I closed it then 3 more calcs came up

Calc
I opened and close IE again now I have 4 calculators LOL

I feel like I'm being attacked by calculators lol.. how can we keep it one calculator or one exe
Screenshot-6.png
0
 
LVL 7

Author Closing Comment

by:Network Zero
ID: 40620433
BOMB !!!  THIS WAS WHAT  I WAS LOOK FOR! I was able to take the script and make it work and I learned a bunch of stuff in the way!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question