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

时间:2018-08-28 13:17:03

标签: powershell

我正在尝试通过一封电子邮件获得所有结果,而不是每次都发送邮件

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

我们将非常感谢您的帮助

谢谢

$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 个答案:

答案 0 :(得分:1)

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

注意:由于我目前无法访问AD环境,因此我尚未对其进行测试,但是它应该非常接近...

$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

答案 1 :(得分:0)

那呢?我无法对其进行测试,但是我修复了您的第一个代码中的一些错误

  • Get-ADUser上的过滤器错误(请勿在其中使用花括号)
  • 没有PasswordExpired属性,您必须计算日期
  • 删除了一些不必要的属性
  • 使用sendMailMessage进行可读性

function readExcelFile(workbook, row, cell, newData) {
    workbook.xlsx.readFile('H://filename.xlsm')
        .then(function () {
            cell.value = newData;
            row.commit();
            return workbook.xlsx.writeFile('H://newFile.xlsx');
        });
}