Advertisement
bdill

GetScheduledTasks.ps1

Jul 4th, 2021 (edited)
1,395
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. # borrowed from: https://stackoverflow.com/questions/32731366/how-to-get-trigger-details-associated-with-a-task-in-task-scheduler-from-powersh
  4.  
  5. Clear-Host
  6. #------------------------------------------------------------------------------------------
  7. # Config vars
  8. #------------------------------------------------------------------------------------------
  9. $TaskNameLike   = "*"          # "*" to get all tasks.        "Foo*" to get tasks that start with "Foo"
  10. $TaskFolder     = "\"          # "\" = tasks in root folder.  "\TOWER\" = all tasks in TOWER folder
  11. $SaveToCsv      = 1            # 1 to save, 0 to n\ot save
  12. $CsvSaveFolder  = "C:\Tasks\"  # Where you want to save the CSV output
  13.  
  14. #------------------------------------------------------------------------------------------
  15. Function BinaryWeekdaysToString {
  16.     param([int]$DaysOfWeek)
  17.    
  18.     [string]$output = ""
  19.     If ($DaysOfWeek -eq -1) {
  20.         $output = "No Trigger"
  21.         Return $output;    
  22.     }
  23.     $WeekdaysBits7 = ([convert]::ToString([int32]$DaysOfWeek,2)).PadLeft(7, "0")
  24.     If ($WeekdaysBits7.Substring(0,1) -eq "1") { $output += 'Su,' }
  25.     If ($WeekdaysBits7.Substring(1,1) -eq "1") { $output += 'M,' }
  26.     If ($WeekdaysBits7.Substring(2,1) -eq "1") { $output += 'Tu,' }
  27.     If ($WeekdaysBits7.Substring(3,1) -eq "1") { $output += 'W,' }
  28.     If ($WeekdaysBits7.Substring(4,1) -eq "1") { $output += 'Th,' }
  29.     If ($WeekdaysBits7.Substring(5,1) -eq "1") { $output += 'F,' }
  30.     If ($WeekdaysBits7.Substring(6,1) -eq "1") { $output += 'Sa,' }
  31.     Return $output.TrimEnd(",")
  32. }
  33. #------------------------------------------------------------------------------------------
  34. $Result = @()
  35. Get-ScheduledTask -TaskPath $TaskFolder |
  36.     Where-Object { $_.TaskName-like $TaskNameLike } | ForEach-Object {
  37.  
  38.     $Task = $_
  39.     Write-Host $Task.TaskName
  40.     Try {
  41.         [string]$Name          = $Task.TaskName
  42.         [string]$Author        = $Task.Author
  43.         [string]$Description   = $Task.Description
  44.         [bool]$Enabled         = $Task.Settings.Enabled
  45.         [string]$Action        = $Task.Actions | Select -ExpandProperty Execute
  46.         [string]$Arguments     = $Task.Actions | Select -ExpandProperty Arguments
  47.  
  48.         # Init vars in case no trigger
  49.         [datetime]$Start       = (Get-Date -Month 1 -Day 1 -Year 1900 -Hour 0 -Minute 0 -Second 0) # bogus 1900-01-01 date
  50.         [string]$StartTime     = ""
  51.         [string]$Repetition    = "No Trigger"
  52.         [string]$Duration      = "No Trigger"
  53.         [string]$TriggerDays   = "No Trigger"
  54.         [int]$DaysOfWeek       = -1  # -1 to indicate no trigger
  55.  
  56.         Try {
  57.             # If the task has a triger, overwrite the trigger vars with valid values
  58.             $Start         = $Task.Triggers | Select -ExpandProperty StartBoundary
  59.             $StartTime     = $Start.ToString("hh:mm")
  60.             $DaysOfWeek    = $Task.Triggers | Select -Expandproperty DaysOfWeek       #$Task.Triggers[0].DaysOfWeek
  61.             $Repetition    = $Task.Triggers.Repetition | Select -ExpandProperty Interval
  62.             $Duration      = $Task.triggers.Repetition | Select -ExpandProperty Duration
  63.             $TriggerDays   = $Task.Triggers | Select -ExpandProperty DaysInterval  #$Task.Triggers[0].DaysInterval
  64.         } Catch {
  65.             Write-Host "--- INFO: No Trigger found for $Name"
  66.         }
  67.            
  68.         $splat = @{
  69.             'Name'        = $Name
  70.             'Author'      = $Author
  71.             'Description' = $Description
  72.             'Action'      = $Action
  73.             'Arguments'   = $Arguments
  74.             'Start'       = $Start
  75.             'StartTime'   = $StartTime
  76.             'Duration'    = $Duration
  77.             'Repetition'  = $Repetition
  78.             'DaysOfWeek'  = BinaryWeekdaysToString $DaysOfWeek
  79.             'TriggerDays' = If ($TriggerDays -eq 1) { "Daily" } else { $TriggerDays }
  80.             'Enabled'     = If ($Enabled) { "Enabled" } Else { "-" }
  81.         }
  82.  
  83.         $obj = New-Object -TypeName PSObject -property $splat
  84.         $Result += $obj
  85.     }
  86.     Catch {
  87.         Write-Host "ERROR: $Error"
  88.     }
  89. }
  90.  
  91. $Result = $Result | Select-Object Name, Enabled, DaysOfWeek, TriggerDays, StartTime, Repetition, Duration, Action, Arguments, Description, Author, Start  # select in a specific order
  92. $Result | Out-GridView
  93. If ($SaveToCsv) {
  94.     $DateIso = Get-Date -Format "yyyy-MM-dd_hhmmss"
  95.     $SaveFullPath = [System.IO.Path]::Combine($CsvSaveFolder, "SchedTasks_$DateIso.csv")
  96.     $Result | Export-Csv $SaveFullPath -NoTypeInformation
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement