0

I am trying to run a powershell script periodically using task scheduler. I would like to return the name of the process currently in use. I am using the code found here to do so. This works when I run the script from powershell. The process returned is "powershell" as expected. However, when I run this using task scheduler, all of the currently running processes are returned, not just the foreground window process. The script saves the process details in the file "process_file.out". The script I am using is below:

#set working directory to path of script
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Set-Location -Path $scriptPath -Passthru

Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class Tricks {
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();
}
"@

$a = [tricks]::GetForegroundWindow()

$b=get-process | ? { $_.mainwindowhandle -eq $a } | Get-Unique
$c=$b.ProcessName
$c | Out-File -FilePath process_file.out

An example of the first portion of the output file "process_file.out" when run using task scheduler is below:

acrotray
aesm_service
AGMService
AGSService
ApplicationFrameHost
armsvc
audiodg
backgroundTaskHost
chrome
cmd
...etc
                              

Any advice would be greatly appreciated.

  • Is the PowerShell process currently in the output of `process_file.out`? If you could please explain your requirements better it would also be useful as I don't understand why you just want to verify a process that is already clearly running as part of the script itself. – alexzelaya Jul 08 '21 at 23:19
  • Yes, powershell is also in the output file. I am trying to check every minute to see if a certain process is currently in use (Ansys) or if it is in the background not being used. I then want to store how long Ansys is in the background for and close Ansys if it has been open but not in use for over an hour. I figured if this were run from task scheduler in the background, the returned foreground process would be whatever process I am currently working on. – Isaac Rose Jul 09 '21 at 15:25
  • `GetForegroundWindow()` will retrieve the handle to the active window, not process. Defining if a process is "in use" is a tricky task, CPU below a threshold, no output to a certain log, etc. You might want to consider using `Get-Process` as it can query certain perfomance data and then create some kind of do/until loop that if breached x amount of times will close the application. – alexzelaya Jul 10 '21 at 02:06
  • Sorry, I didn't phrase that very well. I just need to return the name of process corresponding to the active window. I was able to do this successfully in python/task scheduler using the code [here](https://stackoverflow.com/questions/65362756/getting-process-name-of-focused-window-with-python/65363087). I was then able to write a python code that successfully closes Ansys after it is idle for an hour. However, I would prefer to use powershell instead of python. Any advice would be great. – Isaac Rose Jul 12 '21 at 15:13

0 Answers0