通过PowerShell从CSV发送一封电子邮件给同一管理员

时间:2019-08-25 23:42:57

标签: powershell csv email

我有一个CSV文件,其中包含用户列表及其相应的AD到期日期和相应的管理员。列表中有多个用户,只有一名经理。我希望只发送一种电子邮件和表格格式的用户列表。我在下面使用的代码每行发送一封电子邮件,一位经理接收多封电子邮件,这不好。

$users = Import-Csv C:\AboutToExpire\15days.csv

# send email for each row
foreach ($item in $users) {
    $name = $item.("Employee")
    $expiration = $item.("Expiration")
    $email = $item.("Email")

    # HTML Email format
    $to      =  "$email"
    $from    =  "$from"
    $subject =  "Account expiration of $name"
    $body    =  "Hello <b>Manager</b>,

<p>The login account of the employee named <b>$name</b> will expire on <b>$expiration</b>.</p>

<p>Please reply to this email should the account needs to be extended.</p>
Thank you.
<p>Best regards,
<br>ISD Team</p>"

    # Create the message
    $mail = New-Object System.Net.Mail.Mailmessage $from, $to, $subject, $body
    $mail.IsBodyHTML = $true
    $mail.Cc.Add("itops@dummy.com")

    # Create an SMTP Server Object
    $server = "$smtp"
    $Smtp = New-Object System.Net.Mail.SMTPClient $server
    $Smtp.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

    # Send the Mail
    $smtp.Send($mail)
}

0 个答案:

没有答案