I have a Powershell file that's sending emails based on a list of input parameters from a CSV file.
I've calculated a delay between each email based on the total time it needs to run for, divided by the number of lines in the file. Unfortunately this doesn't take into account the length of time it takes to send each individual email, which can vary between 1 second and 60 seconds so can't be estimated.
The intention is to calculate the length of time it took to send the email and subtract this from the intended delay, giving a value to pause before sending the next email (where this would produce a negative value, the next email would be sent immediately.)
I can't get the time calculation working properly though... the current looping code is as below:
$StartTime = [datetime](get-date).ToShortTimeString()
Start-Countdown -Seconds $iDelay -Message ("Waiting to send email "+$i+" of "+$LinesinFile)
sendMail
$iTimeToRun = NEW-TIMESPAN –Start $StartTime -End [datetime(get-date).ToShortTimeString()
$iDelay = $Delay - $iTimeToRun
Delay is the intended delay required, so subtracting the time to send previous email should give me the required delay for the next one. Start-Countdown and sendMail are functions defined within the script... the sendMail function is the one that needs to be calculated specifically as it will have the most impact on the time to run.