Bonneau21
Bonneau21

Reputation: 586

send mail with all result in one mail

I'm trying to get all the result in one email instead of sending mail each time

this is what i have right now and it send mail for each user with a password expired or about to.

Help will be much appreciated

Thanks

$MaxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge

Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} -properties PasswordLastSet,
                                                                                    PasswordExpired,
                                                                                    PasswordNeverExpires,
                                                                                    EmailAddress,
                                                                                    DisplayName,
                                                                                    GivenName,
                                                                                    SN,
                                                                                    pwdLastSet | 
foreach {
    $UserName = $_.DisplayName
    $SN = $_.SN
    $Email = $_.EmailAddress
    $today = Get-Date
    $enc  = New-Object System.Text.utf8encoding
    $ExpiryDate = $_.PasswordLastSet + $maxPasswordAgeTimeSpan
    $DaysLeft = ($ExpiryDate-$today).days
    $Msg1 = "<p style='font-family:arial'>Hi,</p>
            <p style='font-family:arial'>Password for the $UserName is expired.</p>
            <p style='font-family:arial'>Thanks.</p>"

    $Msg2 = "<p style='font-family:arial'>Hi,</p>
            <p style='font-family:arial'>The password for $UserName will  expire $ExpiryDate.</p>
            <p style='font-family:arial'>Thanks.</p>"

    If ($_.PasswordExpired -like 'True') {
        Send-mailmessage -to "[email protected]"` 
        -from [email protected]`
        -Subject "Mot de passe Windows"`
        -body $WarnMsg1 -smtpserver x.x.x.x `
        -BodyAsHtml -Encoding $enc
    }

    ElseIf ($DaysLeft -eq 1) {
        Send-mailmessage -to "[email protected]"`
        -from [email protected] `
        -Subject "Mot de passe Windows"`
        -body $WarnMsg2 -smtpserver x.x.x.x `
        -BodyAsHtml -Encoding $enc
    }
}

Upvotes: 0

Views: 58

Answers (2)

John Seerden
John Seerden

Reputation: 166

What about this? I haven't been able to test it, but I fixed a few faults in your first code

  • The filter on Get-ADUser was wrong (don't use curly braces there)
  • There's no PasswordExpired attribute, you have to calculate the date
  • Removed some unnecessary attributes
  • Splatting sendMailMessage for readability

$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days

$mailBody = ""
$date = Get-Date

$adUsers = Get-ADUser -Filter 'Enabled -eq $true -and PasswordNeverExpires -eq $false' `
    -Properties PasswordLastSet, DisplayName | 
        Where-Object { $_.PasswordLastSet }

foreach ($adUser in $adUsers) {
    $expirationDate = $adUser.PasswordLastSet.AddDays($maxPasswordAge)
    $daysUntilExpiration = ($expirationDate - $date).Days
    if ($daysUntilExpiration -gt 0 -and $daysUntilExpiration -lt 7) {
        $mailBody += "Password of $($adUser.DisplayName) is about to expire in $daysUntilExpiration"
    }
    elseif ($daysUntilExpiration -le 0) {
        $mailBody += "Password of $($adUser.DisplayName) is expired"
    }
}

$sendMailMessage = @{
    To         = ""
    From       = ""
    Subject    = ""
    Body       = $mailBody
    SmtpServer = "smtp.office365.com"
    Port       = 587
    Encoding   = UTF8
}

Send-MailMessage @sendMailMessage -BodyAsHtml -UseSSL

Upvotes: 0

henrycarteruk
henrycarteruk

Reputation: 13227

You can create a PSObject that contains the users within the foreach loop, then use that list to send a single email one the PSObjects have been populated.

Note: I haven't tested this as I don't have access to an AD Environment at the moment, but it should be pretty close...

$MaxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
$Properties = @("PasswordLastSet","PasswordExpired","DisplayName")

$PasswordExpired = @()
$OneDayLeft = @()

$SMTPServer = "x.x.x.x"
$today = Get-Date

Get-ADUser -Filter "Enabled -eq $True -and PasswordNeverExpires -eq $False" -Properties $Properties | foreach {

    $DisplayName = $_.DisplayName
    $ExpiryDate = $_.PasswordLastSet + $maxPasswordAgeTimeSpan
    $DaysLeft = ($ExpiryDate-$today).days

    If ($_.PasswordExpired -like 'True') {
        #Add user to PasswordExpired PSObject if password expired:
        $PasswordExpired += New-Object -TypeName PSObject -Property @{User="$DisplayName"}
    }
    ElseIf ($DaysLeft -eq 1) {
        #Add user to OneDayLeft PSObject if password expiring soon:
        $OneDayLeft += New-Object -TypeName PSObject -Property @{User="$DisplayName"; ExpiryDate="$ExpiryDate"}
    }
}

#Use PSObject to create a HTML Table:
$PasswordExpired_Table = $PasswordExpired | ConvertTo-Html -Fragment
$OneDayLeft_Table = $OneDayLeft | ConvertTo-Html -Fragment

#Assemble Email Body string using HTML Table of data:
$PasswordExpired_Body = "<p style='font-family:arial'>Hi,</p>
        <p style='font-family:arial'>Expired passwords:</p>
        <p style='font-family:arial'>$PasswordExpired_Table</p>
        <p style='font-family:arial'>Thanks.</p>"

$OneDayLeft_Body = "<p style='font-family:arial'>Hi,</p>
        <p style='font-family:arial'>Passwords expiring soon:</p>
        <p style='font-family:arial'>$OneDayLeftTable</p>
        <p style='font-family:arial'>Thanks.</p>"

Send-MailMessage -to "[email protected]" -from "[email protected]" -Subject "Mot de passe Windows" -body $PasswordExpired_Body -smtpserver $SMTPServer -BodyAsHtml -Encoding UTF8
Send-MailMessage -to "[email protected]" -from "[email protected]" -Subject "Mot de passe Windows va expirer demain" -body $OneDayLeft_Body -smtpserver $SMTPServer -BodyAsHtml -Encoding UTF8

Upvotes: 1

Related Questions