I need to get a list of Scheduled tasks from a Windows server out to a third party. So, I was searching for a way to do this via a script. This is the solution I came up with:
param([String]$schedPath="\") function getTasks($path) { $out = @() # Get root tasks $schedule.GetFolder($path).GetTasks(0) | % { $xml = [xml]$_.xml $out += New-Object psobject -Property @{ "Name" = $_.Name "Path" = $_.Path "LastRunTime" = $_.LastRunTime "NextRunTime" = $_.NextRunTime "Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n" } } # Get tasks from subfolders $schedule.GetFolder($path).GetFolders(0) | % { $out += getTasks($_.Path) } #Output $out } $tasks = @() $schedule = New-Object -ComObject "Schedule.Service" $schedule.Connect() # Start inventory $tasks += getTasks($schedPath) # Close com [System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null Remove-Variable schedule # Output all tasks $tasks
To execute the script, from the Powershell command prompt, I use the following string:
.\GetScheduledTasks.ps1 \Folder\TasksForMe | Format-Table -Wrap -Property Name,NextRunTime,Actions > FolderTasksForMe.txt
Powershell is not one of my strong points. So, there is probably a lot more to do with this.
Thanks to these two posts: