Get-Process from path

Welcome Forums General PowerShell Q&A Get-Process from path

This topic contains 7 replies, has 4 voices, and was last updated by

 
Participant
2 weeks, 2 days ago.

  • Author
    Posts
  • #113141

    Participant
    Points: 0
    Rank: Member

    Hi Comunity,

    One of our developers uses Task Scheduler to run his programs. The problem comes when the process fails, the task has no idea about this and the task will not be re-triggered. This is the point where a script comes in that checks that the process is running, and if not the task is triggered to start the process. I have come so far:

    $taskPath = "\"
    
    Get-ScheduledTask -TaskPath $taskPath |
    ForEach-Object { if ($_.actions.execute -match ".exe") {
    if ()
    
    

    the link missing is how do i now connect the task with the process so i can check the state...

  • #113153

    Keymaster
    Points: 1
    Rank: Member

    Get-Process can't query processes by their filename. However, the WMI/CIM Win32_Process class can be filtered by path.

  • #113164

    Participant
    Points: 0
    Rank: Member

    Thank you for the answer, Don.  Stupid question following: how can i filter a CimInstance. I have tried with path and did not work,

    
    Get-CimInstance Win32_Process -Filter "Path = 'C:\Windows\system32\notepad.exe'"
    
    

    But this works:

    
    Get-CimInstance Win32_Process -Filter "name = 'notepad.exe'"
    
    

    What am i missing?

  • #113167

    Participant
    Points: 10
    Rank: Member

    Is this what you are after?

    (Get-Process -Name notepad).Path
    C:\WINDOWS\system32\notepad.exe
    

    Yet, what you are doing should work, as shown below, with another example of how you could approach it.

    Get-CimInstance Win32_Process -Filter "name = 'notepad.exe'"
    
    ProcessId Name        HandleCount WorkingSetSize VirtualSize  
    --------- ----        ----------- -------------- -----------  
    1320      notepad.exe 239         18063360       2203497508864
    
    
    
    (Get-CimInstance Win32_Process) -match 'notepad'
    
    ProcessId Name        HandleCount WorkingSetSize VirtualSize  
    --------- ----        ----------- -------------- -----------  
    1320      notepad.exe 239         18063360       2203497508864
    
  • #113168
    js

    Participant
    Points: 6
    Rank: Member

    Filtering Path within the wmi command seems to be a special case. Maybe it's created dynamically after the query?

    https://social.technet.microsoft.com/Forums/en-US/f8a58bb4-81f5-4d67-8f2d-c958edb84f96/filter-win32groupuser-class-gt-invalid-query?forum=winserverpowershell

    Try "commandline" or "executablepath" instead. Like in sql, you also have to double up the backslashes. Ah, the obstacle course of scripting.

    Get-Ciminstance Win32_Process -Filter "ExecutablePath = 'C:\\Windows\\system32\\notepad.exe'"
    
    ProcessId Name        HandleCount WorkingSetSize VirtualSize
    --------- ----        ----------- -------------- -----------
    6748      notepad.exe 230         14123008       2199194226688
  • #113206

    Participant
    Points: 0
    Rank: Member

    Thank you postanote, I was trying to match by Path.

    The double backslashes were the missing link, that works, thank you js!

    The problem is that I need a property from Get-ScheduledTask that i can then pipe to something that checks if the process is running, however if i need to use double backslashes, i cannot use the Path property to feed Get-CimInstance. I also do not have a Name that i can use. Any suggestions on how i can overcome this?

  • #113218
    js

    Participant
    Points: 6
    Rank: Member

    You can use replace. Have to double the backslashes once again for the first argument, since it's a regex.

    $path = 'c:\windows\system32\notepad.exe'
    $path = $path -replace '\\','\\'
    $path
    
    c:\\windows\\system32\\notepad.exe
    

    Hmm, wouldn't this be easier?

    get-process | where path -eq c:\windows\system32\notepad.exe
  • #113233

    Participant
    Points: 0
    Rank: Member

    Uuu, much easier indeed. Thank you, js! I've got it running now.

You must be logged in to reply to this topic.