Powershell向Manager的所有即将过期的用户发送一封电子邮件

时间:2018-06-21 14:40:50

标签: powershell active-directory html-email

我正尝试在包含将要与Manager匹配的所有即将过期用户的正文上发送一封邮件,我遇到的问题是该脚本正在向具有以下条件的每个用户发送一封电子邮件是同一位经理,我的老板对我说,不可能那样做。 您能否在这个脚本上帮我解决如何发送这样的正文html电子邮件;

Dear $ Manager,
you have the following soon to expire users;
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
$Body

这是我在网上找到并根据需要进行编辑的实际脚本,

import-module ActiveDirectory;

Get-ADUser -Filter * -Properties directReports,EmailAddress,Displayname | ForEach {

    $ManagerName=$_.Displayname


    If ( $_.directReports ) {

        $managerEmailAddress = $_.EmailAddress

        $_.directReports | ForEach {

            $userDetails = Get-ADUser $_ -Properties AccountExpirationDate,accountExpires

            $userName=$userdetails.Name

            If( $userDetails.accountExpires -eq 0 -or $userDetails.accountExpires -eq 9223372036854775807 ) {

                $sendEmail = $false

            }

            If ( $userDetails.AccountExpirationDate ) {

                $ExpiryDate=$userDetails.AccountExpirationDate

                $ExpiryDate1=$ExpiryDate.ToShortDateString()

                $today=(Get-Date)

                $DaysLeft=($ExpiryDate-$today).days
                $Date = Get-Date -format "yyyy-MM-dd_HH-mm-ss"          
                $UserList = Get-ADUser -Filter {sAMAccountName -Like "*_ext" -and enabled -eq $true} -SearchBase 'DC=example,DC=local' -properties AccountExpirationDate,manager | `Where-Object{$_.AccountExpirationDate -lt (Get-Date).AddDays(7) -and  $_.AccountExpirationDate -gt (Get-Date).AddDays(1) -and $_.AccountExpirationDate -ne $null} |Select Name,UserPrincipalName,AccountExpirationDate,@{Name="ManagerEmail";Expression={(get-aduser -property emailaddress $_.manager).emailaddress}}
                $ReportName = "C:\Users\myusername\Desktop\ExpiringUserReports\Expiring_Users_$Date.xlsx" 
                $UserList | Export-Excel $ReportName -FreezeTopRow -AutoFilter -AutoSize -BoldTopRow

                $Body = "
                        <html>  
                        <body> 
                        <p>Dear $ManagerName,<br>
                        The user account <strong>$userName</strong>, will expire in <strong>$DaysLeft days</strong>.<br>
                        You are being notified because our records show that you are the primary contact Manager for this user.<br>
                        Please take action to extend the validity before expiration.<br />
                        Download the following <a href='https://examplespa-my.mysite.com/:x:/g/personal/myyser_example_com/file.xslx'> Consultants Template </a> file and send back to us with updated expiration date PO.<br>

                        Having any issues, don't hesitate to  <font color=104160 size=5> &#9993;</font> us.</p>
                            <p>Regards<br />
                            <img src='https://www.example.it/example/logo.jpg' ><br>

                            <b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Help&nbsp;Desk
                            Team</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'><o:p></o:p></span><br />
                            <b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>example
                            </span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span><br />
                            <p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span>
                            <span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Via V.
                        &nbsp;</span><span style='font-size:6.0pt;font-family:
                            'Lucida Grande';color:#00578C'></span><br />
                            <span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>T.
                            +</span><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span><br />
                            <span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>F.
                            +39 06 </span><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span>
                            <p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span>
                            <span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
                            href='mailto:helpdesk@example.com'><span style='font-size:9.0pt'>helpdesk@example.com</span></a></span><span
                            style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>&nbsp;</span><span
                            style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br />
                            <span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
                            href='http://www.example.com/'><span style='font-size:9.0pt'>www.example.com</span></a></span><span
                            style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br>
                            &#169; &#174;
                            </body>  
                            </html>"

                If ($DaysLeft -le 2 -and $DaysLeft -ge 0) {

                    $sendEmail = $true

                }

            }

            If ( $sendEmail ) {

                # i am not using this right now as i don't want to spam managers while i am testing script.
                send-mailmessage -To $managerEmailAddress -From helpdesk@example.com -Subject "Users Expiry notification. Action is required" -body "$Body $body1" -smtpserver "example-smtp.com" -BodyAsHtml -Attachments $ReportName 

                # This is i am using just to see the output

                "{0,-28} {1,-20} {2,-18} {3,-8}" -f $managerEmailAddress,$userdetails.SamAccountName,$ExpiryDate1,$DaysLeft

            }

        $sendEmail = $false

        }

    }

}

1 个答案:

答案 0 :(得分:0)

找到了解决方案, 这是最终代码。

import-module ActiveDirectory;

Get-ADUser -Filter * -SearchBase 'OU=TestExpire,DC=AdlabTest,DC=local' -Properties directReports,EmailAddress,Displayname | ForEach {

    $ManagerName=$_.Displayname
    $Body = "
                        <html>  
                        <body> 
                        <p>Dear $ManagerName<br/>
                        You are being notified because our records show that you are the primary contact Manager for the below listed users.<br>
                        <style>
                        TABLE {border-width: 1px; border-style: solid; border-color: black; border-=collapse: collapse;}
                        TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
                        TD {border-width: 1px; padding: 1px; border-style: solid; border-color: black;}
                        </style>
                        <table>
                        <tbody>
                        <tr><th>Name</th><th>UserName</th><th>AccountExpires</th></tr>";
    $AddBody = "";

    If ( $_.directReports ) {

    #Write-Output("Processing : " + $ManagerName);
        $ToEmail = $_.EmailAddress

        $_.directReports | ForEach {

            $userDetails = Get-ADUser $_ -Properties AccountExpirationDate,accountExpires,EmailAddress

            $userName=$userdetails.Name
            $userEmail=$userdetails.userPrincipalName

            If( $userDetails.accountExpires -eq 0 -or $userDetails.accountExpires -eq 9223372036854775807 ) {

                $sendEmail = $false
            }

            If ( $userDetails.AccountExpirationDate ) {

                $ExpiryDate=$userDetails.AccountExpirationDate

                $ExpiryDate1=$ExpiryDate.ToShortDateString()

                $today=(Get-Date)

                $DaysLeft=($ExpiryDate-$today).days

                If ($DaysLeft -le 40 -and $DaysLeft -ge 0) {
                    $AddBody += "<tr><td>$userName</td> <td><a style='text-decoration:none;color: rgb(0, 0, 0);'>$userEmail</a></td> <td>$ExpiryDate1</td> </tr>";
                    $sendEmail = $true
                }

            }
        }

        If ( $sendEmail ) {
            $Body +=$AddBody;
            $Body = $Body + "</tbody>
                            </table><br>

                            Please take action as soon as possible to extend the validity before expiration.<br />
                            Download the following <a href='https://link_to_file.xslx'>Template</a> file and send back to us with updated expiration date PO.<br>
                            Having any issues, don't hesitate to  <font color=104160 size=5> &#9993;</font> us.</p>
                            <p>Regards<br />
                            <img src='https://www.link_to_your_site/logo.jpg' ><br>

                            <b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Help&nbsp;Desk
                            Team</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'><o:p></o:p></span><br />
                            <b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Your Business Name</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span><br />
                            <p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span>
                            <span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Your Business address&nbsp;</span><span style='font-size:6.0pt;font-family:
                            'Lucida Grande';color:#00578C'></span><br />
                            <span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>T.
                            +tout phone</span><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span><br />
                            <span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>F.
                            +your fax</span><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span>
                            <p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
                            color:#00578C'></span>
                            <span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
                            href='mailto:helpdesk@link_your_site.com'><span style='font-size:9.0pt'>helpdesk@link_your_site.com</span></a></span><span
                            style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>&nbsp;</span><span
                            style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br />
                            <span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
                            href='http://www.link_your_site.com/'><span style='font-size:9.0pt'>http://www.link_your_site.com</span></a></span><span
                            style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br>
                            &#169; &#174;
                            </body>  
                            </html>";
                send-mailmessage -To $ToEmail -From expiry@link_your_site.comm -Subject "Users Expiry notification. Action is required" -body $Body -smtpserver "link_your_site.smtp" -BodyAsHtml -Priority High

            }
    }
}