Techibee.com

Query scheduled tasks from remote computer using Powershell

The below script helps you to query scheduled tasks from remote computer which is running with Windows 7/Windows 2008  or above. You can use this script to query multiple computers to get all the scheduled tasks. You can also query the computer by using a scheduled task name as well.

This script output contains details about when a task is last ran, what is the next execution time as well. These details helps you to quickly look at what all the tasks ran recently and what tasks will run in future at what time. This script also gives the path where this task is present. This useful to identify the location of task in task scheduler folder hierarchy that you you see in GUI.

There are managed APIs like Task Scheduler Managed Wrapper which can give you much more details about scheduled tasks running on a computer. This wrapper has many  other functions like you can create, delete and modify tasks.

 

[cmdletbinding()]                        
param (                        
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]                        
 [string[]] $ComputerName = $env:computername,                        
 [string] $TaskName                        
)                        
#function to get all scheduled task folder details.                        
function Get-TaskSubFolders {                        
[cmdletbinding()]                        
param (                        
 $FolderRef                        
)                        
$ArrFolders = @()                        
$folders = $folderRef.getfolders(1)                        
if($folders) {                        
 foreach ($folder in $folders) {                        
  $ArrFolders = $ArrFolders + $folder                        
  if($folder.getfolders(1)) {                        
   Get-TaskSubFolders -FolderRef $folder                        
  }                        
 }                        
}                        
return $ArrFolders                        
}                        

#MAIN                        

foreach ($Computer in $ComputerName) {                        
 $SchService = New-Object -ComObject Schedule.Service                        
 $SchService.Connect($Computer)                        
 $Rootfolder = $SchService.GetFolder("\")            
 $folders = @($RootFolder)             
 $folders += Get-Tasksubfolders -FolderRef $RootFolder                        
 foreach($Folder in $folders) {                        
  $Tasks = $folder.gettasks(1)                        
  foreach($Task in $Tasks) {                        
   $OutputObj = New-Object -TypeName PSobject                         
   $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name TaskName -Value $Task.Name                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name TaskFolder -Value $Folder.path                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name IsEnabled -Value $task.enabled                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name LastRunTime -Value $task.LastRunTime                        
   $OutputObj | Add-Member -MemberType NoteProperty -Name NextRunTime -Value $task.NextRunTime                        
   if($TaskName) {                        
    if($Task.Name -eq $TaskName) {                        
     $OutputObj                        
    }                        
   } else {                        
     $OutputObj                        
   }                         
  }                        

 }                        

}

Output:

Feel Free to post here if you have any trouble in using this script. Happy to help!!!

Exit mobile version