Solved

Antonymous Process power shell script

Posted on 2015-02-18
9
94 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
  • 5
  • 3
9 Comments
 
LVL 70

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
A brief introduction to what I consider to be the best editor for PowerShell.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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