Checking if a script is running

Welcome Forums General PowerShell Q&A Checking if a script is running

Viewing 4 reply threads
  • Author
    Posts
    • #161988
      Participant
      Topics: 16
      Replies: 32
      Points: 80
      Rank: Member

      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!

       

    • #162021
      Participant
      Topics: 12
      Replies: 232
      Points: 466
      Helping Hand
      Rank: Contributor

      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.

      $process = Get-WmiObject Win32_Process -Filter “Name=’powershell.exe'” | Where-Object {$_.CommandLine -like “*script.ps1*”}
      
    • #162062
      Participant
      Topics: 2
      Replies: 1013
      Points: 2,093
      Helping Hand
      Rank: Community Hero

      Do this as a PowerShellJob and just monitor the job.

    • #162078
      Participant
      Topics: 16
      Replies: 32
      Points: 80
      Rank: Member

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

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

       

      Thanks!

    • #162158
      Participant
      Topics: 12
      Replies: 232
      Points: 466
      Helping Hand
      Rank: Contributor

      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.

      c:\>powershell -file _path_to_your_script_.ps1
      

      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.

Viewing 4 reply threads
  • The topic ‘Checking if a script is running’ is closed to new replies.