Skip to content

Instantly share code, notes, and snippets.

@aev-mambro2
Created August 18, 2021 20:07
Show Gist options
  • Save aev-mambro2/3ae2df286b28ee7263c2fbd9c5636096 to your computer and use it in GitHub Desktop.
Save aev-mambro2/3ae2df286b28ee7263c2fbd9c5636096 to your computer and use it in GitHub Desktop.
list-scheduled-tasks
<# List Scheduled Tasks
# This script lists scheduled tasks of all known Windows-operated servers,
# and places each task's path into a file specific to the server.
#
# Run this prior to check-api-tasks, which uses the output files.
#
# Author: A.E.Veltstra
# Version: 2.21.818.1243
#>
<#
# Specify host names of Windows-operated servers,
# and the paths for each of those scheduled task folders of theirs,
# that you want listed.
#>
$tasks = @{
"netbui-hostname" = @("\Microsoft\Windows\Authentication\*")
"other-host" = @(
"\Microsoft\Windows\Authentication\*"
, "\Microsoft\Windows\Authentication User Interface\*"
)
}
foreach ($h in $tasks.Keys) {
if (Test-Connection -ComputerName $h -Quiet) {
$c = New-CimSession -ComputerName $h;
$out = "local-drive\some-local-path\tasks-" + $h + ".txt";
if (Test-Path -Path $out) {
Remove-Item -Path $out;
}
foreach ($p in $tasks[$h]) {
Get-ScheduledTask -CimSession $c -TaskPath $p `
| select TaskPath, TaskName `
| sort TaskPath, TaskName `
| Select-Object @{L='Task';E={join-path -path $_.TaskPath -ChildPath $_.TaskName}} `
| ft -AutoSize -HideTableHeaders `
| Out-File -FilePath $out -Append;
}
} else {
Write-Output "Failed to connect to host: $h"
}
}
@aev-mambro2
Copy link
Author

If you need to check whether known scheduled tasks ran at all, you need to have a listing of them first. That is what this script provides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment