Checking if a script is running

Hello all.

I have written a file watcher script that loops from the time it starts until about 11:00 PM.

It is an important script so I want a way to make sure it is running.
It is launched via the windows task scheduler using the “Powershell -File c:\Scripts\Script.ps1”

I can see that the script is running as the logs it produces continue to increment.
In my Health Monitor script I am using the following command to try and determinant if the script is running.

Get-WmiObject Win32_Process -Filter “Name=‘powershell.exe’ AND CommandLine LIKE ‘%Script.ps1%’”

Unfortunately it is not returning any info.

Any help is greatly appreciated!

 

Are the logs produced by the script or by the scheduled task?
Meaning if the script produce the log entries then it’s running so I doubt that you’ll catch an issue with FileWatcher that way.

Anyway filters in general can be a bit quirky.
So I would suggest to do it in “two” steps.

E.g.
[pre]
$process = Get-WmiObject Win32_Process -Filter “Name=‘powershell.exe’” | Where-Object {$_.CommandLine -like “script.ps1”}
[/pre]

Do this as a PowerShellJob and just monitor the job.

The script is doing the logging. Not the task scheduler.

I did try your command and it still returns no results.

 

Thanks!

I would recommend you the same answer as postanote.

The commandline I sent you will work if the powershell process that you want to check against is started via commandline.
E.g. from normal cmd.exe.

[pre]
c:>powershell -file path_to_your_script.ps1
[/pre]

Then checking from a second script if it’s still running.
If the first script is started from within a running powershell session then it won’t show up in CommandLine.
Since that was not the command line starting the process.

But again, look into Start-Job and Receive-Job since that will be a better solution.