代码之家  ›  专栏  ›  技术社区  ›  Bonneau21

在一封邮件中发送所有结果的邮件

  •  0
  • Bonneau21  · 技术社区  · 6 年前

    我想把所有的结果都放在一封邮件里,而不是每次都发邮件。

    这就是我现在拥有的,它会为每个用户发送一封带有过期或即将过期密码的邮件。

    非常感谢您的帮助

    谢谢

    $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 "xxx@xxx.com"` 
            -from x@x.com`
            -Subject "Mot de passe Windows"`
            -body $WarnMsg1 -smtpserver x.x.x.x `
            -BodyAsHtml -Encoding $enc
        }
    
        ElseIf ($DaysLeft -eq 1) {
            Send-mailmessage -to "x@x.com"`
            -from x@x.com `
            -Subject "Mot de passe Windows"`
            -body $WarnMsg2 -smtpserver x.x.x.x `
            -BodyAsHtml -Encoding $enc
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   henrycarteruk    6 年前

    您可以创建一个PSObject,其中包含 foreach 循环,然后使用该列表发送已填充PSObjects的单个电子邮件。

    注意:我还没有测试过这个,因为我现在没有访问广告环境的权限,但是应该非常接近…

    $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 "xxx@xxx.com" -from "x@x.com" -Subject "Mot de passe Windows" -body $PasswordExpired_Body -smtpserver $SMTPServer -BodyAsHtml -Encoding UTF8
    Send-MailMessage -to "x@x.com" -from "x@x.com" -Subject "Mot de passe Windows va expirer demain" -body $OneDayLeft_Body -smtpserver $SMTPServer -BodyAsHtml -Encoding UTF8
    
        2
  •  0
  •   John Seerden    6 年前

    这个怎么样?我没能测试它,但我修正了你第一个代码中的一些错误

    • get aduser上的过滤器错误(不要在那里使用大括号)
    • 没有passwordExpired属性,您必须计算日期
    • 删除了一些不必要的属性
    • 显示sendmailmessage以提高可读性

    $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