Thursday, June 22, 2017

Configure to run a Powershell Script into task scheduler

The Requirement

To create a periodic action that reports in CSV the top 20 processes into a Folder in Desktop, every hour.


I have been doing a lot of Powershell Scripting in the last years. So I have to work with Task Scheduler a lot in recent days. I will show the configuration


    List of steps:
  1. Create your PowerShell script.
  2. I'll use an advanced PowerShell liner script for this tasks

         
    $start= Get-date
    $file = $start.Tostring("yyyyMMdd-hhmmss")
    Get-Process -IncludeUserName * | select @{l="Date";e={$($start)}},ProcessName,CPU,Id,StartTime,Username | Sort-Object CPU -Descending | select -First 10 | ConvertTo-Csv -NoTypeInformation  | Out-File ".\$file.csv"
    

    This script will get all the processes in the computer you run it, also it will add the 'Date' where you are doing the query, select some variables of interest, then sort descending using the "WorkingSet" property, select the first 20, convert the object to CSV, no type information about the class and finally the output would be the Output.CSV file. (In the same folder of running the script).

  3. Configure the Task

  4. Open task manager, by clicking the Windows icon, and typing "task scheduler"


    Once opened, create a Tasks by clicking the "Create Task" link in the "Actions section."

    At the start, you will be located in the "General" tab, on the next screen add a name and make sure that the checkbox "Run it with the highest privileges" is checked.
    Then move to the Triggers, here we configure that it should execute every hour. To do so we need to click the "New" Button and then set as shown in the next image, and then click ok.
    Now on the "Actions" Tab is the important one. we click on "new" on the program script add: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe (without quotes since there's no space in there)

    In arguments would be the path of the file, (if contain spaces add quotes): -file "C:\Users\j0rt3g4\Desktop\scripts\Startcoco.ps1"

    and in working directory make sure that you don't add any quotes so the path should never contain any blank space. (this will be the location of the reports): C:\Users\j0rt3g4\Desktop\scripts

    Finally, the settings should be seen like this:
    And in the final configuration tab is called "Settings" in here you just need to check that "allow the task to be run on demand" and "Do not Start a new instance" are checked and selected respectively.


  5. Make sure that is working or troubleshoot what you could have missed

  6. Enable all the history for all tasks

    Make sure that the "start in contains the file".
    Successful code is 0x000000.
    And it should be generating every single hour, in the "start in" path0 configured in the "Action" of the task.
    At the end in your "Start in" you should see something like this:

If you enjoy and worked for you, thank you so much for reading and please consider to donate.