PowerShell – Creating Scheduled Task from within your script

Creating a scheduled task on very large number of servers? Why not using a PowerShell script to do the job, using the “ScheduledTasks” – module. Lets make the module visible and point the various functions to the correct part of the task scheduler.

When taking a closer look at the module,  following functions are included.

Get-Command

We will discuss here only the functions that are needed to created a proper scheduled task on a your Windows machine. Lets go ahead and define the various functions of the module:

  • New-ScheduledTaskAction – function is related to the “Action” tab
    Information: link
    Actions Tab.PNG
  • New-ScheduledTaskTrigger – function is related to the “Triggers” tab
    Information: link
    Triggers Tab.PNG
  • New-ScheduledTaskPrincipal – function is related to the “Security Options” in the “General” tab.
    Information: link
    Security Options
  • Register-ScheduledTask – function will be used to add the Scheduled task to the task scheduler. More information see link
    Register-ScheduledTask
     [-Force]
     [[-Password] ]
     [[-User] ]
     [-TaskName] 
     [[-TaskPath] ]
     [-Action] <CimInstance[]>
     [[-Description] ]
     [[-Settings] ]
     [[-Trigger] <CimInstance[]>]
     [[-RunLevel] ]
     [-CimSession <CimSession[]>]
     [-ThrottleLimit ]
     [-AsJob]
     []

     Example Code

    #Import Module ScheduledTasks
    Import-Module -Name "ScheduledTasks"
    
    #Define the task action parameters.
    $Sta = New-ScheduledTaskAction -Execute "powershell" -Argument ".\TestScript.ps1" -WorkingDirectory "C:\Scripts\"
    #end
    #Define the task trigger parameters. Task will start daily at 01AM.
    $Stt = New-ScheduledTaskTrigger -Daily -At 1am
    #end
    #Define the task principal
    $Stp =  New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest -LogonType ServiceAccount
    #end
    #Variable includes description/TaskName for the scheduled task.
    $StTaskName="TEST SCRIPT 01"
    $StDescript="Schedule task that will launch a test script on the local machine"
    #end
    #Register the task in task scheduler.
    Register-ScheduledTask -TaskName $StTaskName -Action $Sta -Trigger $Stt -Principal $Stp -Description $StDescript 
    #end

    After running the script you will find a new task is the task scheduler. Task Scheduler General Result
    Triggers
    Task Scheduler triggers Result
    Actions
    Task Scheduler Actions Result
    Have fun creating your scheduled tasks in a wink.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.