Disable/Enable Scheduled tasks using Powershell and COMObject

Disable/Enable Scheduled tasks using Powershell and COMObject

December 11, 2018 0 By Amir Joseph Sayes

While working on a project for a client where many of the Windows Systems are 2008R2 with PowerShell 3.0 or lower, there was a need to automate disabling scheduled tasks to ensure consistency across the XenApp/XenDesktop estate.

This is something that can be done easily in PowerShell 4.0+ using Get-scheduledTask cmdlet. But in PowerShall 3.0 and earlier, it gets a bit trickier.

Based on the script by Jaap Brasser where he enumerates all tasks subfolders, I have written a new code which can accept an array of schedule tasks names, along with a switch to enable/disable them.

How to use

  1. Add the below two functions in your PowerShell session on the system where the tasks should be disabled.
  2. Call the  Set-TaskStatus function and pass the status (Enabled, Disabled) and the tasks names (Array)

Example

Set-TaskStatus -Tasklist "WrongName","Office Automatic Updates" -Status disabled

The output will be something like this

PS_Scheduledtasks

The Script

function Get-AllTaskSubFolders {
    [cmdletbinding()]
    param (
        # Set to use $Schedule as default parameter so it automatically list all files
        # For current schedule object if it exists.
        $FolderRef = $Schedule.getfolder("\")
    )
    if ($FolderRef.Path -eq '\') {
        $FolderRef
    }
    if (-not $RootFolder) {
        $ArrFolders = @()
        if(($folders = $folderRef.getfolders(1))) {
            $folders | ForEach-Object {
                $ArrFolders += $_
                if($_.getfolders(1)) {
                    Get-AllTaskSubFolders -FolderRef $_
                }#end of IF
            }#end of ForEach
        }#end of IF
        $ArrFolders
    }#end of IF
}



function Set-TaskStatus {    

    [cmdletbinding()]
    Param (
        [ValidateSet("Enabled","Disabled")][string]$Status,
        
        [parameter(ValueFromPipeline = $True,ValueFromPipeLineByPropertyName = $True)]        
        [string]$Computername = 'localhost',
        
        [parameter(Mandatory)]      
        [string[]]$Tasklist
    )

    try {
       $schedule = New-Object –ComObject ("Schedule.Service")
    } catch {
       Write-Warning "Schedule.Service COM Object not found, this script requires this object"
       return
    }

    $Schedule.connect($computername)

    $AllFolders =Get-AllTaskSubFolders

    if ($Status -eq "Enabled") {$TaskStatus = $True}
    if ($Status -eq "Disabled") {$TaskStatus = $False}

    $Hits = @()

    foreach ($tTask in $TaskList) {
            $AllFolders | % {
                $curFolder = $schedule.GetFolder($_.path)    
                $curFolder.gettasks(1) | % {
                    if ($_.name -eq $tTask) {                                    
                        $_.enabled = $TaskStatus
                        $Hits += $_.name
                        Write-host "The task: $tTask is now set to $TaskStatus"
                    } #end of IF
                } #end inner loop            
            } #End Folders loop
            If ($Hits -notcontains $tTask) {
                Write-host "The task: $tTask does not exist"        
            }
    }
}