i need Send-mailmessage delay with about an hour or so howto?

Peter Huiskens 47 Reputation points
2021-04-28T10:12:33.717+00:00

So I've automated the user creation wit a powershell script and everything worked great ( userd, dfslinks, user rights per organisation, mailbox creation, and even numberassigning and enabling Skype for business) effectively the entire user creation in Active directory with everything needed for a user to logon. and start his work.
Everything went fine until we switched to outlook 365 / outlook in the cloud.

situation: at the end of the script i send an email to the newly created user. with some helpfull "welcome text"

Problem: the mailbox is created on our exchange server with the enable-remote mailbox cmdlet.
After the sync from or local directory to o365 the mailbox is created. However, this welcome mail is sent before the mailbox has been created.

I'm trying to figure out what the best way would be to make sure this welcome message is being delivered , so the sync is every hour, so delaying the sending of the mail message would be a good option.

I was thinking of using a scheduled task for that, but i have no clue on how to do that. ( so pointers in the right direction would help a lot)

an alternative would be to create the mailbox instantly in office 365, but mail still has to go through our exchange server. so if you guys know a way to instantly create the mailbox on o365 , that could help as well. ( for instance a power shell connect to instantly sync the newly created user)

any help in solving this would be appreciated
Thnx in advance

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2021-05-04T14:49:36.793+00:00

    suggestions are welcome

    Don't schedule the task in your script. Instead have it create a file that contains the email address of the user and any other user specific data. Save this file in a folder maybe named something like C:\WelcomeQueue. Format the filename using a timestamp, like 2021-05-04-10-37.msg.

    Manually create a scheduled task that executes every hour or every half hour. Have this task execute a SendWelcomeMail.ps1 script.

    When the script runs it does a Get-Date and subtracts 60 minutes to build a variable that can be compared to the file names in C:\WelcomeQueue. Actually, you could compare against the file name or the date created attribute. The intent is to identify files that were created more than 1 hour before the current time.

    Read those files one by one, send the welcome email, and then delete the .msg file. If there are no files found, the script just terminates.

    Be sure to add some logging functionality into SendWelcomeMail.ps1 so that you know what it's doing.


9 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-04-28T10:45:02.997+00:00

    Hi @Anonymous ,

    using the Task Scheduler to run a PowerShell script is described here. Maybe it's helpful to get it started:

    https://blog.netwrix.com/2018/07/03/how-to-automate-powershell-scripts-with-task-scheduler/

    I would create a script that checks the mailbox is created and available and if so sending the email to the user.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Peter Huiskens 47 Reputation points
    2021-04-28T15:22:15.707+00:00

    Thnx for the answer.
    I think the task scheduler could do what i want, but it looks like i need quite a bit of scripting to get it to work like how i want it to,

    I was hoping i could make a small mod to my current script so i could reuse all the collected variables,

    0 comments No comments

  3. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-04-28T15:42:52.757+00:00

    Hi @Anonymous ,

    I wouldn't idle the current script for an hour or so ... if the computer is turning of the current job is gone.

    But maybe creating a Task Scheduler job in your current script is an option. This way you could put the variables in the scheduled scripts.
    https://devblogs.microsoft.com/scripting/use-powershell-to-create-scheduled-tasks/

    And with the scheduled script you might delete the Task Scheduler job.

    Just an idea.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  4. Peter Huiskens 47 Reputation points
    2021-04-29T10:49:10.833+00:00

    So i figured it out, so thnx again @Andreas Baumgarten

    i thought, lets place the code here for future reference.

    the below process is fetching the mail body from a text file, adds an attachement, en then creates a scheduled task to send that email in 90 minutes
    only problem with it so far is that the scheduled task isn't deleted after it has ran, but i don't really see a problem in that. since i only create an average of 5 user accounts per week.

    # Mail aan de gebruiker.   
    $body3tekst= get-content "\\xxxxxxxx\apps\Sources\02_Application_Sources\05_scripts\Accountmail\$org\mailbody.txt" -raw   
    $body3 = $body3tekst.Replace("<voornaam>","$vnaam")  
    $attachement = "\\xxxxx\apps\Sources\02_Application_Sources\05_scripts\Accountmail\Handleiding Thuiswerken.docx"  
      
      
    $sendwelkom = Send-MailMessage -To $emailadres -From ict@xxxxx -Subject "Welkom" -body $body3 -Attachments $attachement -SmtpServer xxxxxx  
    $starttime = (get-date).AddMinutes(90)  
      
    # scheduled task aanmaken voor het versturen van welkomstmail:   
    # Create a new task action  
    $taskAction = New-ScheduledTaskAction `  
        -Execute 'powershell.exe'`  
        -Argument '$sendwelkom'  
      
    # Register the new PowerShell scheduled task  
    $taskName = "welkomstmail-$vnaam-$anaam"  
    $description = "welkomstmail sturen naar $vnaam $anaam"  
    $taskTrigger = New-ScheduledTaskTrigger -once -At $starttime  
      
    # Register the scheduled task  
    Register-ScheduledTask `  
        -TaskName $taskName `  
        -Action $taskAction `  
        -Trigger $taskTrigger `  
        -Description $description  
      
    # Einde mail versturen aan gebruiker  
    

    .

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.