locked
Filter scheduled tasks for ones whose action properties contain file names from a file RRS feed

  • Question

  • I am trying to create a utility that will search all scheduled tasks for tasks whose action contains executing a file path found in a csv file. The part that is tripping me up is that I can not get the output to display correctly. I simply just want to output the tasks that meet the criteria to a file. As it stands it only outputs blank lines.

    #Scan for scheduled tasks

    $taskPath = "\*"

    Get-ScheduledTask -TaskPath $taskPath |

        ForEach-Object { [pscustomobject]@{

         Name = $_.TaskName

         Path = $_.TaskPath

         LastResult = $(($_ | Get-ScheduledTaskInfo).LastTaskResult)

         NextRun = $(($_ | Get-ScheduledTaskInfo).NextRunTime)

         Status = $_.State

         Command = $_.Actions.execute

         Arguments = $_.Actions.Arguments }} |

            Export-Csv -Path $outcsv -Force -Append -NoTypeInformation

    #Import the task scheduler file and search for specific task criteria
    $myinput = Get-Content $List
    $test = Import-Csv $outcsv |
        Where-Object {$_.Command -match $myinput} |
        ForEach-Object {$_.Command} | Out-File $Log -Encoding ascii -Append
    $test2 = Select-String -Path $outcsv -pattern $myinput -SimpleMatch | Out-File $Log -Encoding ascii -Append

    Wednesday, July 12, 2017 8:41 PM

Answers

  • Just do it in a loop or use "-in"

    Where{ $_.Actions.Execute -in (Get-Contents d:\test\names.txt') }


    \_(ツ)_/

    • Marked as answer by LeeLee2cold Wednesday, July 19, 2017 5:53 PM
    Wednesday, July 12, 2017 9:54 PM

All replies

  • This works just fine for me:

    Get-ScheduledTask -TaskPath \*|
    	ForEach-Object {
    		[pscustomobject]@{
    			Name	   = $_.TaskName
    			Path	   = $_.TaskPath
    			LastResult = $(($_ | Get-ScheduledTaskInfo).LastTaskResult)
    			NextRun    = $(($_ | Get-ScheduledTaskInfo).NextRunTime)
    			Status	   = $_.State
    			Command    = $_.Actions.execute
    			Arguments  = $_.Actions.Arguments
    		}
    	} | 
    	Where-Object { $_.Command -eq 'd:\test\test.bat' }


    \_(ツ)_/

    Wednesday, July 12, 2017 8:51 PM
  • This is rally all you need:

    Get-ScheduledTask -TaskPath \* | Where{ $_.Actions.Execute -eq 'd:\test\test.bat' }


    \_(ツ)_/

    Wednesday, July 12, 2017 9:06 PM
  • That works for searching for a single file path but I want to be able to search for multiple file paths that I have stored in another file. The code you provided would only search for the d:\test\test.bat string where I would rather search for multiple file path strings that are stored in a file. So for example I have a csv file called list.csv and it contains:

    C:\Test.exe

    C:\Users\Test2.exe

    C:ProgramFiles\Test3.exe

    Sorry if I did not explain myself well enough the first time, I hope this helps.

    Wednesday, July 12, 2017 9:42 PM
  • Just do it in a loop or use "-in"

    Where{ $_.Actions.Execute -in (Get-Contents d:\test\names.txt') }


    \_(ツ)_/

    • Marked as answer by LeeLee2cold Wednesday, July 19, 2017 5:53 PM
    Wednesday, July 12, 2017 9:54 PM