Using powershell to email password expiring notices

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #6256

    by par4pat at 2013-01-11 11:51:19

    Hi All,

    I am using PS to mail out notices of password expiring my issue is in the formatting of the email body. I used the body = @" "@ around the text but it does not hold the line breaks. So then I decided to try HTML and it does not work at all. Can anyone offer any help? I did have the HTML line break tags in the script when I ran it. When I copied it here I grab the wrong 1

    With the code below this was the email message that was received

    *
    1555

    Code below

    #Loads the active directory module to enable AD cmdlets
    #import-module activedirectory

    #Queries all accounts in AD domain and stores them in username variable
    $username = get-aduser -Filter * -Searchbase "OU=Sales,OU=Medical Scientist,OU=Users,OU=Ironwood,DC=irwd,DC=corp"

    #Foreach loop is run against each account stored in the username variable
    foreach($user in $username){

    #gets current date and stores in now variable
    $now = get-date

    #gets date of when password was last set for a user
    $passlastset = get-aduser $user -properties passwordlastset | select -ExpandProperty passwordlastset

    $emailAddress = get-aduser $user -properties mail | select -ExpandProperty mail

    #calculates password expirationdate by adding 90 days to the password last set date
    $passexpirationdate = $passlastset.adddays(90)

    #calculates the number of days until a user's password will expire
    $daystilexpire = $passexpirationdate – $now | select -ExpandProperty days

    #if statement to select only accounts with expiration greater than 0 days
    if($daystilexpire -gt "0"){

    #if statment to further filter accounts from above if statement. This selects accounts with less than 14 days until expiration.
    if($daystilexpire -le "14"){

    #generates email to user using .net smtpclient to notify them of how many days until their password expires.
    $emailFrom = "pmcnamara@ironwoodpharma.com"
    $emailTo = "$user@ironwoodpharma.com"
    if($daystilexpire -eq "1"){$subject = "Password Expiration Notice – $daystilexpire day"}
    else{$subject = "Password Expiration Notice – $daystilexpire days"}
    #$subject = "Password Expiration Notice – $daystilexpire days"

    $body =@"
    As part of the User Identification and Password Policy, your Windows password will have to be changed at least once every 90 days. Your Windows password is going to expire in the amount of days listed in the subject of this email.

    Please follow the steps listed below to change your password.

    NOTE: Standard corporate security policies mandate that we change the password requirements of all clients. The requirements are that:

    1. The password must be at least 8 characters long.
    2. The password does not contain your username.
    3. The password has not been used in the previous 24 passwords.
    3. The password must contain characters from 3 of the following 4 categories:
    a. English upper case characters (A, B, …Z)
    b. English lower case characters (a, b, …z)
    c. Numbers (0, 1, …9)
    d. Non-alphanumeric characters (For example, !, $, #, %)

    To change your password please follow the instructions below:

    1. You need to be connected to the Ironwood network when changing your password. If you are a remote user, you need to be connected
    to the Ironwood network using the VPN connection.
    2. Press Ctrl+Alt+Del (Hold Ctrl and Alt together and while holding, press Del).
    3. Click the button "Change Password".
    4. You will be asked to enter your old (current) password and new password.
    5. Click OK and wait for the system to say “Your password has been changed.”
    6. Click OK, and then click Cancel to close the security window.

    Please do not reply to the above email address. Ironwood will not respond to emails sent to that address.

    "@

    $smtpServer = "irwdmail.irwd.corp"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg = new-object Net.Mail.MailMessage
    $msg.From = $emailFrom
    $msg.To.Add("pmcnamara@ironwoodpharma.com")
    $msg.subject = $subject
    $msg.IsBodyHTML = $true
    $msg.body = $body | ConvertTo-Html
    $smtp.Send($msg)
    #Echo From: $emailFrom To: $emailAddress Subject: $subject Body: $body

    }
    }
    }

    by DonJ at 2013-01-15 14:52:59

    HTML ignores white space and line breaks. If you want to send a formatted message, you will need to use HTML tags such as the p tag. Simply including line break tags is not sufficient. Converting the body to HTML does not do this – ConvertTo-HTML is simply wrapping whatever you give it in an HTML page. That doesn't put HTML formatting into your message.

    You can also consider using Send-MailMessage instead of manually creating the SmtpClient class.

You must be logged in to reply to this topic.