Try setting the repeat to stop after one day.
PowerShell Task Schedule running multiple times
I have the following Powershell script that is set to run once every 3 hours in a Task Schedule. It just scans a folder ($startFolder) and if there are more than 200 files ($MaxFiles), it should send an alert email.
$startFolder = "E:\Data\"
$linkInFolder = "file://10.1.44.244/Myfolder/"
[string[]]$EmailTo = "EMail Group 1 <******@blah.com>", "
EMail Group 2 <******@blah.com>"
$EmailFrom = "Email Server <******@blah.com>"
$EmailServer = "SMTPsrv"
$Subject = "File Threshold Limit Reached"
$MaxFiles = 200
$totalFiles = (Get-ChildItem $startFolder -File | Measure-Object).Count
$totalFilesStr = '{0:N0}' -f $totalFiles
if ($totalFiles -gt $MaxFiles)
{
Write-Host 'Total Files: ' $totalFilesStr
$Body = 'There are <b>' + $totalFilesStr + '</b> files inside the <b><a href='''+ $linkInFolder +'''>IN</a></b> folder.<br/> <br/> Please verify!'
try {
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -Body $Body -SmtpServer $EmailServer -Port 25 -BodyAsHtml
"Email has been submitted to server!"
}
catch [System.Exception] {
"Failed to send email: " -f $_.Exception.Message
}
}
I created a Task schedule to run this PS every 3 hours. The following is the Export of the Task Schedule.
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2020-10-26T15:27:55.1413442</Date>
<Author>AME</Author>
<URI>\CheckFiles</URI>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<Repetition>
<Interval>PT3H</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<StartBoundary>2022-09-06T15:23:52</StartBoundary>
<ExecutionTimeLimit>PT30M</ExecutionTimeLimit>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell</Command>
<Arguments>-File E:\CheckFileSize.ps1</Arguments>
</Exec>
</Actions>
</Task>
The problem I am having is that when it detects more than 200 files it triggers, but sends sometimes like 13 emails at once. If I run the PowerShell manually it runs once. Also, if I manually run the task schedule it runs once. Not sure what is wrong here.
1 additional answer
Sort by: Most helpful
-
Rich Matheisen 47,686 Reputation points
2023-02-21T03:15:51.4066667+00:00 Try using this version of your script. It will record a transcript activity. If the same execution of the script sends 13 unique messages the contents of the transcript will show that.
Start-Transcript -Path PATH-TO-TRANSCRIPT-FILE.txt -Append $startFolder = "E:\Data\" $MaxFiles = 200 $totalFiles = (Get-ChildItem $startFolder -File | Measure-Object).Count $totalFilesStr = '{0:N0}' -f $totalFiles Write-Host 'Total Files: ' $totalFilesStr if ($totalFiles -gt $MaxFiles) { $props = @{ From = "Email Server <******@blah.com>" To = "EMail Group 1 <******@blah.com>", "EMail Group 2 <******@blah.com>" Subject = "File Threshold Limit Reached" Body = "There are <b>$totalFilesStr</b> files inside the <b><a href='$linkInFolder'>IN</a></b> folder.<br/><br/>Please verify!" BodyAsHtml = $true SmtpServer = "SMTPsrv" Port = 25 } try { Write-Host "Sending email" Send-MailMessage @props Write-Host "Email has been submitted to server!" } catch { Write-Host "Failed to send email: {0}" -f $_.Exception.Message } Write-Host "End of sending email" } else{ Write-Host "Not enough files present to send email" } Write-Host "End of script" Stop-Transcript