Created
August 19, 2021 11:52
-
-
Save aev-mambro2/eb32b6aa69151693df2f597cb943a502 to your computer and use it in GitHub Desktop.
check-scheduled-tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# Check API Tasks | |
# This script checks scheduled tasks of all known Windows-operated servers, | |
# determines whether any has missed its scheduled execution, | |
# and emails those tasks with server name and amount of missed executions. | |
# | |
# Run this after list-api-tasks, which creates the input files that this script | |
# expects. | |
# | |
# Author: A.E.Veltstra | |
# Version: 2.21.818.1246 | |
#> | |
<# | |
# Supply the host names of the Windows-operated servers to query. | |
#> | |
$hosts = @( | |
"netbui-hostname" | |
"other-host" | |
) | |
$zero = 0; | |
$errors = @() | |
$now = Get-Date -Format s; | |
foreach ($h in $hosts) { | |
if (Test-Connection -ComputerName $h -Quiet) { | |
$c = New-CimSession -ComputerName $h; | |
$inFile = "drve:\folder\scheduled-tasks-" + $h + ".txt"; | |
if (Test-Path -Path $inFile) { | |
$content = Get-Content -Path $inFile; | |
foreach ($line in $content) { | |
$line = $line.Trim() | |
if ($line.Length -gt 10) { | |
$path = Split-Path -Path $line -Parent; | |
$name = Split-Path -Path $line -Leaf; | |
try { | |
$info = Get-ScheduledTaskInfo -CimSession $c -TaskPath $path -TaskName $name -ErrorAction Stop | select NumberOfMissedRuns; | |
$missed = $info.NumberOfMissedRuns; | |
if ($zero -lt $missed) { | |
$errors += "On server '$h', $missed executions were missed by task '$line'." | |
} | |
} catch { | |
$errors += "On server '$h', found no task in path '$path', named '$name'." | |
} | |
} | |
} | |
} else { | |
$errors += "Found no file named '$inFile'." | |
} | |
} else { | |
$errors += "Failed to connect to host: '$h'." | |
} | |
} | |
if ($zero -lt $errors.Length) { | |
Write-Output $([System.String]::Join("`r`n", $errors)); | |
try { | |
$bm = New-Object System.Management.Automation.PSCredential( | |
"[email protected]", | |
(ConvertTo-SecureString "Password" -AsPlainText -Force) | |
) -ErrorAction Stop; | |
#NOTE: this version of Send-MailMessage does NOT have a -ReplyTo parameter. | |
Send-MailMessage ` | |
-From [email protected] ` | |
-Subject $("{0} scheduled task problems, dd. {1}" -f $errors.Length, $now) ` | |
-To [email protected] ` | |
-Body $([System.String]::Concat( | |
($errors -Join "`r`n"), | |
" `r`n `r`n", | |
"-- `r`n", | |
"For support and feedback, contact [email protected]. `r`n" | |
)) ` | |
-Port 587 ` | |
-SmtpServer smtp.office365.com ` | |
-UseSsl ` | |
-ErrorAction Stop ` | |
-Credential $bm; | |
} catch { | |
$errmsg = $_; | |
Write-Output $( | |
"{0}: ERROR 1: {1}" -f $now, | |
$errmsg | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment