Script to check if a script is running.

Hello all

I have a problem I am trying to resolve.
I have a script that moves files through out the day. The script is started in the morning by the windows task scheduler. That script then is set to loop until 11:30 PM. The problem I am facing is that the script sometimes just terminates for no known reason. This script is mission critical so to help combat this issue I wanted to write a script that can be executed by the task scheduler every 15 minutes to verify the primary script is still running and if it is not then it will write an event to the event log that the primary task will look for and cause the task to kick off again.

I wanted to use the following code to check for the running script

$process = Get-WmiObject -Class Win32_Process -Filter “Name=‘powershell.exe’” | Where {$_.CommandLine -like “script_name”}

The problem is that when the script is launched via the task scheduler this does not return a result. But if I run the script from the PS console it does return.

The script that is being executed from the task scheduler is being launched using the following command

Posershell -File C:\Script.ps1

I would love some help on this.

 

Thanks!!!

It sounds like you’re trying to schedule a repeating (looping) task inside of PowerShell.

Have you considered having the scheduled task run every 15 minutes (or whatever time is appropriate) instead of looping inside your script? Then if the script fails, it would run again at the next time interval.

Well, as per my environment, I am running ps script using a batch file, and from task scheduler I added the bat file to execute with a repetation of every 15min.

Inside the script I write following mechanism to catch the process id of the powershell instance. Which I am redirecting to a log file to monitor its runtime details with cpu usage.

[pre]

$proc = Get-process -name power*|? Starttime -gt (get-date).addseconds(-2)

[/pre]

You can use the same idea to monitor your script status. Once you get the process object, you can manipulate it as per your own way.

Inside bat file, use the following line to execute the ps script.

[pre]

Powershell -file c:\AutoM\script.ps1

[/pre]

HTH

Roy.

Thanks Roy. your info has been helpful in getting me about 90% of the way there.

The one issue I found is that the parameter “? Starttime -gt (get-date).addseconds(-2)” does not return a result.

Is there a way to make it work?

Thanks!

Actually I write in very short way.

[pre]

$proc = Get-process -name power*|where $_.Starttime -gt (get-date).addseconds(-2)

[/pre]

Here starttime is a property of a process object. And using get-date with addseconds method, I tried to mention such processes which start just 2 seconds ago. So if you use this thing correctly in your script, you’ll get the exact powershell process.

For test change the 2 to 60, then u may find some output. Also the process object is getting stored in proc variable. To get output you have to execute the variable.

I am assuming that you already have some basic idea abt PS.

HTH.

Roy.

Thanks Roy.

I have tried this approach as well and still coming up empty. I even set the time back 10 minutes and still no results.

Post the code here inside pre tag. Ensure the pre key word in small letter.

Get-Process -Name Powershell | Select Id | Where $_.Starttime -gt (Get-Date).AddSeconds(-5)

That’s the issue. Filtering always need to be done as left as possible. Remove select id. Use where first, the select.

I’ve got it now.

 

Thanks!!!