Exchange script question RRS feed

  • Question

  • Hello all. We have a script that we run on one of our servers, it links into our active directory and checks to see when a users password is due to expire, it then sends an email to them asking them to change there password if required. The system also integrates externally with Google mail using Google sync, recently this mechanism has stopped working, below I have added in the script we are using to achieve this, if someone could look over the code and perhaps see an error we are missing?


    # Update 2.1.0 - Removed filter for "Password never expires".
    # Update 2.1.1 - Added email template filter, can now send different email if there staff/student.
    # Update 2.1.2 - Added user filter to remove unwated accounts from the search.
    # Update 2.1.3 - changed email tempates to just a single email to all users.

    # Requires ActiveRoles CmdLets from Quest Software.

    # Load Quest Snapin if not already loaded.

    if(!(Get-PSSnapin | 
        Where-Object {$_.name -eq "Quest.Activeroles.ADManagement"})) {
          ADD-PSSnapin Quest.Activeroles.ADManagement

    # Connect to AD and return all users.

    Get-QADUser -Enabled -SizeLimit 0 | Select-Object samAccountName,mail,DisplayName,PasswordStatus | 

    # Filter out users we do not need.

    Where-Object {$_.samAccountName -match "^p6|^s7|^u[1-9]" -and $_.PasswordStatus -ne "Expired" -and $_.PasswordStatus -ne "User must change password at next logon." -and $_.mail -ne $null} | 

    #Build user array.

    ForEach-Object {
      $today = Get-Date   
      $logdate = Get-Date -format ddMMyyyy   
      $samaccountname = $_.samAccountName
    $displayname = $_.DisplayName
      $mail = $_.mail 
      #$mail  = "operations@ne-worcs.ac.uk"
      $passwordstatus = $_.PasswordStatus
      $passwordexpiry = $passwordstatus.Replace("Expires at: ","")
      $passwordexpirydate = Get-Date $passwordexpiry
      $daystoexpiry = ($passwordexpirydate - $today).Days


    # Build email.
    if ($daystoexpiry -lt 14) {

    $emailSmtpServer = "xxxxxxxx.ad.ne-worcs.ac.uk"
    $emailSmtpServerPort = "587"
    $emailSmtpUser = "xxxxxxxx"
    $emailSmtpPass = "xxxxxxxx"
    $emailFrom = "no reply <no-reply@ne-worcs.ac.uk>"
    $emailTo = $mail
    $emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
    $emailMessage.Subject = "Your password will expire in $daystoexpiry days"
    $emailMessage.IsBodyHtml = $true

    $emailMessage.Body = @"
    <img alt="" src="http://www.ne-worcs.ac.uk/web05/images/logo.jpg" style="width: 381px; #height: 107px;" /></p>
    <strong>Password expiry warning</strong></p>
    Your College password will expire in <strong>$daystoexpiry #days</strong>.</p>
    You must change your password within <strong>$daystoexpiry #days</strong>, otherwise you will not be able to login.</p>
    <strong>To change your password now:</strong></p>
    Use the &quot;Password Reset&quot; button on the announcements page #or <a href="http://www.ne-worcs.ac.uk/useraccount">click here</a></p>

    <p> Please note you can only reset your password within college.</p>
    IT Services<br>
    North East Worcestershire College<br>
    01527 572754</p>

    <p><strong>This is an automatically generated email. Please do not reply to this #message.</strong></P>

    $SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
    $SMTPClient.Send( $emailMessage )

    Write-Host "Email was sent to $mail on $today"
        Add-Content C:\Scripts\Logs\PasswordExpire\$logdate.txt  "Email was sent to #$displayname - $mail on $today"    

    #Send-MailMessage -To "operations@ne-worcs.ac.uk" -From "operations@ne-worcs.ac.uk" -Subject #"Password Reminder log for $today" -Body "This is the log from $today" –BodyasHtml #-Attachments "C:\Scripts\Logs\PasswordExpire\$logdate.txt" -SmtpServer $emailSmtpServer
    Tuesday, May 13, 2014 8:52 AM

All replies

  • Hi,

    Since this problem is related to script, I recommand you ask a question in Script Center forum for more suggestions:


    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.


    Winnie Liang
    TechNet Community Support

    Wednesday, May 14, 2014 7:38 AM