Tuesday, 19 February 2008

Send Welcome Email

Wouldn't it be nice to send your new users an email welcoming them to the company and providing them with some info about how things work. Anyway, I had a request to do this and since I don't think you can do it natively within Exchange 2003 I turned straight away to Powershell.

So, all you need to do is run a daily sceduled task which calls the below Powershell script. It finds the current and next days's dates, searches AD for users created between those dates (i.e. on the current day) and sends an email to the users it finds with the relevant info attached.

(Note: You need the Quest AD cmdlets installed unless you want to use ADSI for the AD search part.)

Doing the search by specifiying the exact date didn't seem to work very successfully. Digging around the Internet revealed the correct format to get the dates into and also searching between the two adjacent dates.


#Find today's and tomorrow's dates and store them in the 'yyyyMMdd}000000.0Z' format
$dayStart = "{0:yyyyMMdd}000000.0Z" -f (Get-Date)
$dayEnd = "{0:yyyyMMdd}000000.0Z" -f ((Get-Date).AddDays(1))

#Find AD users created between the two dates
$newuser = Get-QADUser -searchroot 'springfield.local/userou' -LdapFilter "(&(whenCreated>=$dayStart)(whenCreated<=$dayEnd))" -size 0

#Find the email address for each of the users and send a mail to them with the WelcomeTo.doc attachment
foreach ($address in $newuser)
{
$recipient = $address.mail
$sender = "sender@domain.com"
$server = "smtpservername"
$file = "path to attachment"
$subject = "subject"
$body = "body text"
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
$attachment = new-object System.Net.Mail.Attachment $file
$msg.Attachments.Add($attachment)
$client = new-object System.Net.Mail.SmtpClient $server
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$client.Send($msg)
}

9 comments:

  1. Hello, How Do we use this in an Exchange 2003 Environment.
    Basically am looking out for a Script to Send an automated Welcome email to New users.
    Thanks in Advance.
    Jxon

    ReplyDelete
  2. Then you have come to the right place because this script is for an Exchange 2003 environment.

    Go try it!

    ReplyDelete
  3. Hi Jonathan

    Thanks for the script. Just a question, is it possible to request a read receipt so I have proof that the user received the email.

    ReplyDelete
  4. Looking at MSDN if you add the below it should add a read receipt.

    $msg.Headers.Add("Disposition-Notification-To", "returnreceipt@return.com")

    http://msdn.microsoft.com/en-us/vbasic/bb630227.aspx

    ReplyDelete
  5. what would be the changes if we use ADSI for the AD search part.

    ReplyDelete
  6. Hi Jonathan,
    what changes do i make to the script if im gonna use ADSI for the AD search part.
    Thanks

    ReplyDelete
  7. Hi,

    You would need to do something like the following.

    $searcher = New-Object DirectoryServices.DirectorySearcher
    $searcher.filter="(&(whenCreated>=$dayStart)(whenCreated<=$dayEnd))"
    $searcher.findall()

    ReplyDelete
  8. Hi Jonathan,

    it works like a charm,
    Thanks for sharing it with us.

    Tomer

    ReplyDelete
  9. Hello, I was curious as to whether the line:
    'springfield.local/userou'

    Does this limit the search for new users to just one OU? Obviously I would change the springfield.local to my domain name. We have different GPO's pushed to different people and if I created a user in a OU that I created, would this script grab it? Sorry, I'm a script noob.

    ReplyDelete