Skip to content

Instantly share code, notes, and snippets.

@luisdelatorre012
Last active November 20, 2024 17:30
Show Gist options
  • Save luisdelatorre012/42791d30a47ad69754bf81865b93bfb2 to your computer and use it in GitHub Desktop.
Save luisdelatorre012/42791d30a47ad69754bf81865b93bfb2 to your computer and use it in GitHub Desktop.
Show scheduled tasks
# Ensure the ScheduledTasks module is available
Import-Module ScheduledTasks
# Define the search string
$searchString = '\scheduled_reports\'
# Retrieve all scheduled tasks that have actions defined
$allTasks = Get-ScheduledTask | Where-Object { $_.Actions -ne $null }
# Initialize an array to store matching tasks
$matchingTasks = @()
# Iterate through each task
foreach ($task in $allTasks) {
# Iterate through each action in the task
foreach ($action in $task.Actions) {
# Initialize a flag to indicate a match
$isMatch = $false
# Check if the Execute path contains the search string
if ($action.Execute -like "*$searchString*") {
$isMatch = $true
}
# Check if the Arguments contain the search string
if ($action.Arguments -like "*$searchString*") {
$isMatch = $true
}
# If either Execute or Arguments contain the search string, add to matchingTasks
if ($isMatch) {
# Get task info
$taskInfo = [PSCustomObject]@{
TaskName = $task.TaskName
TaskPath = $task.TaskPath
ActionExecute = $action.Execute
ActionArguments = $action.Arguments
State = (Get-ScheduledTaskInfo -TaskName $task.TaskName -TaskPath $task.TaskPath).State
}
# Add the task info to the matching tasks array
$matchingTasks += $taskInfo
# If you only want to list each task once even if multiple actions match, uncomment the next line
# break
}
}
}
# Check if any matching tasks were found
if ($matchingTasks.Count -gt 0) {
# Display the matching tasks in a table format
$matchingTasks | Format-Table -AutoSize
} else {
Write-Output "No scheduled tasks found with Program/script or Arguments containing '$searchString'."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment