Advertisement
bdill

GetSchedTasks.ps1

Jul 4th, 2021
1,268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Desc: Lists all scheduled tasks and important properties
  2. # Auth: Brian Dill 2021-07-03
  3. # URL:  https://github.com/wbdill/PowerShell-sandbox01/blob/master/GetSchedTasks.ps1
  4. # Inspiration:  https://stackoverflow.com/questions/32731366/how-to-get-trigger-details-associated-with-a-task-in-task-scheduler-from-powersh
  5. # Dependencies: https://dbatools.io/   # Install-Module -Name dbatools  # (must run Powershell as admin to install module)
  6.  
  7. Clear-Host
  8. #------------------------------------------------------------------------------------------
  9. # Config vars
  10. #------------------------------------------------------------------------------------------
  11. $TaskNameLike   = "*"              # "*" to get all tasks.     "Foo*" to get tasks that start with "Foo"
  12. $TaskFolders    = "\", "\Bar\"   # String array of folders.  "\" = tasks in root folder.  "\Bar\" = all tasks in Bar folder
  13. $SaveToCsv      = 1                # 1 to save, 0 to n\ot save
  14. $CsvSaveFolder  = "C:\Tasks\"      # Where you want to save the CSV output
  15.  
  16. $TasksResult = @()                 # var to hold results
  17. #------------------------------------------------------------------------------------------
  18. Function BinaryWeekdaysToString {
  19.     param([int]$DaysOfWeek)
  20.    
  21.     [string]$output = ""
  22.     If ($DaysOfWeek -eq -1) {
  23.         $output = "No Trigger"
  24.         Return $output;    
  25.     }
  26.     $WeekdaysBits7 = ([convert]::ToString([int32]$DaysOfWeek,2)).PadLeft(7, "0")
  27.     If ($WeekdaysBits7.Substring(0,1) -eq "1") { $output += 'Su,' }
  28.     If ($WeekdaysBits7.Substring(1,1) -eq "1") { $output += 'M,' }
  29.     If ($WeekdaysBits7.Substring(2,1) -eq "1") { $output += 'Tu,' }
  30.     If ($WeekdaysBits7.Substring(3,1) -eq "1") { $output += 'W,' }
  31.     If ($WeekdaysBits7.Substring(4,1) -eq "1") { $output += 'Th,' }
  32.     If ($WeekdaysBits7.Substring(5,1) -eq "1") { $output += 'F,' }
  33.     If ($WeekdaysBits7.Substring(6,1) -eq "1") { $output += 'Sa,' }
  34.     Return $output.TrimEnd(",")
  35. }
  36. #------------------------------------------------------------------------------------------
  37. $TaskFolders | ForEach-Object {
  38.     $TaskFolder= $_
  39.     Get-ScheduledTask -TaskPath $TaskFolder |
  40.         Where-Object { $_.TaskName-like $TaskNameLike } | ForEach-Object {
  41.  
  42.         $Task = $_
  43.         Write-Host $Task.TaskName
  44.         Try {
  45.             [string]$Name          = $Task.TaskName
  46.             [string]$Author        = $Task.Author
  47.             [string]$Description   = $Task.Description
  48.             [bool]$Enabled         = $Task.Settings.Enabled
  49.             [string]$Action        = $Task.Actions | Select -ExpandProperty Execute
  50.             [string]$Arguments     = $Task.Actions | Select -ExpandProperty Arguments
  51.             [string]$TaskPath      = $Task.TaskPath
  52.  
  53.             # Init vars in case no trigger
  54.             [datetime]$Start       = (Get-Date -Month 1 -Day 1 -Year 1900 -Hour 0 -Minute 0 -Second 0) # bogus 1900-01-01 date
  55.             [string]$StartTime     = ""
  56.             [string]$Repetition    = "No Trigger"
  57.             [string]$Duration      = "No Trigger"
  58.             [string]$TriggerDays   = "No Trigger"
  59.             [int]$DaysOfWeek       = -1  # -1 to indicate no trigger
  60.  
  61.             Try {
  62.                 # If the task has a triger, overwrite the trigger vars with valid values
  63.                 $Start         = $Task.Triggers | Select -ExpandProperty StartBoundary
  64.                 $StartTime     = $Start.ToString("hh:mm")
  65.                 $DaysOfWeek    = $Task.Triggers | Select -Expandproperty DaysOfWeek       #$Task.Triggers[0].DaysOfWeek
  66.                 $Repetition    = $Task.Triggers.Repetition | Select -ExpandProperty Interval
  67.                 $Duration      = $Task.triggers.Repetition | Select -ExpandProperty Duration
  68.                 $TriggerDays   = $Task.Triggers | Select -ExpandProperty DaysInterval  #$Task.Triggers[0].DaysInterval
  69.             } Catch {
  70.                 Write-Host "--- INFO: No Trigger found for $Name"
  71.             }
  72.            
  73.             $splat = @{
  74.                 'TaskPath'    = $TaskPath
  75.                 'Name'        = $Name
  76.                 'Author'      = $Author
  77.                 'Description' = $Description
  78.                 'Action'      = $Action
  79.                 'Arguments'   = $Arguments
  80.                 'Start'       = $Start
  81.                 'StartTime'   = $StartTime
  82.                 'Duration'    = $Duration
  83.                 'Repetition'  = $Repetition
  84.                 'DaysOfWeek'  = BinaryWeekdaysToString $DaysOfWeek
  85.                 'TriggerDays' = If ($TriggerDays -eq 1) { "Daily" } else { $TriggerDays }
  86.                 'Enabled'     = If ($Enabled) { "Enabled" } Else { "-" }
  87.             }
  88.  
  89.             $obj = New-Object -TypeName PSObject -property $splat
  90.             $TasksResult += $obj
  91.         }
  92.         Catch {
  93.             Write-Host "ERROR: $Error"
  94.         }
  95.     }
  96. }
  97. #------------------------------------------------------------------------------------------
  98. # Output
  99.  
  100. # select specific columns
  101. $TasksResult = $TasksResult | Select-Object TaskPath, Name, Enabled, DaysOfWeek, TriggerDays, StartTime, Repetition, Duration, Action, Arguments, Description, Author, Start |
  102.     Sort-Object TaskPath, Name
  103. $TasksResult | Out-GridView
  104.  
  105. If ($SaveToCsv) {
  106.     $SaveFullPath = [System.IO.Path]::Combine($CsvSaveFolder, "SchedTasks.csv")
  107.     $TasksResult | Export-Csv $SaveFullPath -NoTypeInformation
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement