<p>使用<a href="https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/import-csv" rel="nofollow noreferrer">^{<cd1>}</a>和<a href="https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/group-object" rel="nofollow noreferrer">^{<cd2>}</a>和<a href="https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/send-mailmessage" rel="nofollow noreferrer">^{<cd3>}</a>的PowerShell解决方案:</p>
<p>注:</p>
<ul>
<li><p><code>Send-MailMessage</code>是为了简单起见而使用的,因为它与PowerShell一起提供,但它被认为是过时的,因为它不能保证到SMTP服务器的安全连接</p>
</li>
<li><p>一个<a href="https://github.com/dotnet/platform-compat/blob/master/docs/DE0005.md" rel="nofollow noreferrer">official suggestion</a>是使用第三方<a href="https://github.com/jstedfast/MailKit" rel="nofollow noreferrer">MailKit</a>库<a href="https://www.infoq.com/news/2017/04/MailKit-MimeKit-Official/" rel="nofollow noreferrer">this article</a>有背景信息。<br/>
另一个第三方已为此库创建了PowerShell包装:<a href="https://www.powershellgallery.com/packages/Send-MailKitMessage" rel="nofollow noreferrer">^{<cd5>}</a></p>
</li>
</ul>
<pre><code># Fill in the appropriate values for your environment here.
$csvFile = 'file.csv'
$smtpServer = 'smtp.example.org'
$senderEmail = 'admin@example.org'
Import-Csv $csvFile | Group-Object SupervisorEmail | ForEach-Object {
# Compose the email body.
$body = @"
Dear $($_.Group[0].SupervisorName)
The below accounts have had the Action 2 performed on them. Please contact us with any concerns.
$($_.Group | Select-Object ID, Name, Action | Out-String)
Regards,
Corporate Events
"@
# Uncomment the following line to print the value of $body for diagnostic purposes:
# Write-Verbose -Verbose $body
# Define the parameters for the command that sends the email...
$params = @{
SmtpServer = smtpServer
From = $senderEmail
To = $_.Group[0].SupervisorEmail
Subject = 'Action 2 Notification'
Body = $body
}
# ... and send it.
Send-MailMessage @params
}
</code></pre>