Register-ScheduledTasksEx add-on for PowershellPack

To manage the Task Scheduler service with Powershell on Windows 8/2012, you can use the new Scheduledtasks module. On Windows Vista/7/2008/2008 R2 you have to download the PowershellPack (also included in Windows 7 Reskit) and install the taskscheduler module.

But there is one thing missing in the taskscheduler module, when you want to create a new task with the Register-ScheduledTasks : you can’t specified in witch folder you want it. So I’ve created a new cmdlet (Register-ScheduledTasksEx) that allow you to specified to folder to use. To install this new cmdlet (kind of a add-on) with the taskscheduler module, just follow these 2 steps :

Step 1 : Create the file Register-ScheduledTasksEx.ps1

Create a new file named Register-ScheduledTasksEx.ps1 in the directory where the taskscheduler module is installed (generally %USERPROFILE%\Documents\WindowsPowerShell\Modules) and paste the following code in it.

function Register-ScheduledTaskEx 
{
    <#
    .Synopsis
        Registers a scheduled task.
    .Description
        Registers a scheduled task.
        Modified to support Folders (path)
    #>
    param(
    # The name of the scheduled task to register
    [Parameter(Mandatory=$true,Position=0)]
    [string]
    $name,

    # The path of the scheduled task to register
    [Parameter(Mandatory=$false)]
    [ValidateNotNull()]
    [string]
    $path="",
        
    # The Scheduled Task to Register
    [Parameter(ValueFromPipeline=$true,
        Mandatory=$true)]
    [__ComObject]
    $Task,
        
    # The name of the computer to connect to.
    [string[]]
    $ComputerName,
    
    # The credential used to connect
    [Management.Automation.PSCredential]
    $Credential    
    )  
    
    begin {
        Set-StrictMode -Off
    }
    process {
        if ($task.Definition) { $Task = $task.Definition } 
        foreach ($c in $computerName) {
            $scheduler = Connect-ToTaskScheduler -ComputerName $c -Credential $Credential            
            if ($scheduler -and $scheduler.Connected) {
                $folder = $scheduler.GetFolder($path)
                if ($Credential) {
                    $folder.RegisterTaskDefinition($Name, 
                        $Task, 
                        6,
                        $credential.UserName,
                        $credentail.GetNetworkCredential().Password,
                        6,
                        $null)
                } else {
                    $folder.RegisterTaskDefinition($Name, 
                        $Task, 
                        6,
                        "",
                        "",
                        3,
                        $null)                
                }                
            } 
        }
    }
}

Step 2 : Modify TaskScheduler.psm1 file

Add the following line to the TaskScheduler.psm1 file (In the TaskScheduler module directory).

. $psScriptRoot\Register-ScheduledTaskEx.ps1

Example

Here’s how to use the new cmdlet :

import-module TaskScheduler
$ScheduledTask = New-Task
$ScheduledTask | add-TaskAction -path "Notepad.exe" -Arguments "" -WorkingDirectory ""| out-null
# Note : The folder "\MyCustomFolder" must already  exist in the TaskScheduler
$ScheduledTask | Register-ScheduledTaskEx -Name "MyCustomScheduledTask" -path "\MyCustomFolder" -ComputerName Localhost | out-null

Source : powershelleverydayfaq.blogspot.ca

Next PostNewer Post Previous PostOlder Post Home