#Build the base pieces for emailing results
$ServerName = gc env:computername
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.Body = ""
$SmtpClient.Host = "mail"
$MailMessage.from = ($ServerName + "@company.com")
$MailMessage.To.add("josh.o@comapany.com")
$MailMessage.Subject = $ServerName + " Certificate Expiration Results"
$threshold = 300 #Number of days to look for expiring certificates
$deadline = (Get-Date).AddDays($threshold) #Set deadline date
Dir Cert:\LocalMachine\TrustedPeople | foreach {
If ($_.NotAfter -le $deadline) { $_ | Select Issuer, Subject, NotAfter,
@{Label="Expires In (Days)";Expression={($_.NotAfter -
(Get-Date)).Days}} }
}
此脚本检查将在指定的指定时间范围内过期的证书,但是我想创建一个计划任务并让它向我发送电子邮件。我目前被困在这里。有没有人知道如何使用SMTP服务器向我发送电子邮件?谢谢
答案 0 :(得分:1)
Function Mail-Output
{
Param(
[String]$subject
)
$From = "EmailFrom@example.com"
$To = "EmailTo@example.com"
$SMTPServer = "smtp.example.com"
$SMTPPort = "587"
$Username = "Username"
$Password = "Password"
$body = "Body of email goes here"
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($From, $To, $subject, $body);
}
这是我用来在Powershell中发送简单SMTP电子邮件的方法。它基本上只是扩展了Send-MailMessage以显示cmdlet实际执行的操作。