簡體   English   中英

Powershell向Manager的所有即將過期的用戶發送一封電子郵件

[英]Powershell Send single Email to Manager for all his soon to expire users

我正在嘗試在包含將與Manager匹配的所有即將過期的所有用戶的正文上發送一封郵件,我遇到的問題是該腳本正在向具有相同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

        }

    }

}

找到了解決方案,這是最終代碼。

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

            }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM