我有一个 powershell 脚本,用于检查证书存储中是否有过期证书。它将在 CLI 中显示这些结果。我想修改此脚本以将输出结果通过电子邮件发送到电子邮件正文中。我知道如何使用 send-mailmessage CmdLet 通过 PowerShell 发送电子邮件,但我不确定如何获取脚本的结果并将其发送到电子邮件中...
脚本如下:
param([int]$InNumberOfDays=180,[switch]$ExcludeAutoEnroll)
function WriteCertInfo($cert)
{
#just a lot of code to get the fields into an object
$certObj = "" | Select RequesterName,RequestType,ExpirationDate,CommonName,EnrollmentFlags
$RequesterName=$cert -match "Requester Name:.*Request Type:"
$startLength="Requester Name:".Length
$lineLength=$matches[0].Length -("Request Type:".Length + $startLength)
$OutRequesterName=$matches[0].SubString($startLength,$lineLength)
$certObj.RequesterName=$OutRequesterName
$RequestType=$cert -match "Request Type:.*Certificate Expiration Date:"
$startLength="Request Type:".Length
$lineLength=$matches[0].Length - ("Certificate Expiration Date:".Length + $startLength)
$OutRequestType=$matches[0].SubString($startLength,$lineLength)
$certObj.RequestType=$OutRequestType
$ExpirationDate = $cert -match "Certificate Expiration Date:.*Issued Common Name:"
$startLength="Certificate Expiration Date:".Length
$lineLength=$matches[0].Length - ("Issued Common Name:".Length + $startLength)
$OutExpirationDate=$matches[0].SubString($startLength,$lineLength)
$certObj.ExpirationDate=$OutExpirationDate
$IssuedCommonName= $cert -match "Issued Common Name:.*Template Enrollment Flags:"
$startLength="Issued Common Name:".Length
$lineLength=$matches[0].Length - ("Template Enrollment Flags:".Length + $startLength)
$OutCommonName=$matches[0].SubString($startLength,$lineLength)
$certObj.CommonName=$OutCommonName
$EnrollmentFlags= $cert -match "Template Enrollment Flags:.*"
$startLength="Template Enrollment Flags:".Length
$lineLength=$matches[0].Length - ($startLength)
$OutEnrollmentFlags=$matches[0].SubString($startLength,$lineLength)
$certObj.EnrollmentFlags=$OutEnrollmentFlags
if($ExcludeAutoEnroll)
{
if(($OutEnrollmentFlags -match "CT_FLAG_AUTO_ENROLLMENT") -eq $false)
{
$script:CertToList+=$certObj
}
}
else
{
$script:CertToList+=$certObj
}
}
$CertToList=@()
$today=Get-Date
$endperiod=$today.AddDays($InNumberOfDays)
$tester=certutil -view -restrict "NotAfter>=$today,NotAfter<=$endperiod" -out
"RequestID,RequesterName,RequestType,NotAfter,CommonName,EnrollmentFlags"
$arr=$tester -match "Row \d*:"
$numberOfCerts=$arr.length
$line=[string]::join(" ",$tester)
for($certNo=0;$certNo -lt $numberOfCerts;$certNo=$certNo+1)
{
$r1=$arr[$certNo]
if($certNo -ne ($numberOfCerts-1))
{
$r2=$arr[$certNo+1]
}
else
{
$r2="Maximum Row Index"
}
$isFound=$line -match "$r1 .* $r2"
$NumberOfChars=$matches[0].Length - $r2.Length
$thisCert=$matches[0].SubString(0,$NumberOfChars)
WriteCertInfo($thisCert)
}
$CertToList
答案1
由于 PowerShell 提示符中的输出格式化发生在您实际输出时$CertToList
,并且您对此不感兴趣,因此您可能需要构建一个 HTML 结构以在邮件中显示输出。
更新
由于您想要以图像的形式嵌入内联附件,我们必须自己创建和发送电子邮件,而不是依赖Send-MailMessage
:
# Smtp details as outlined by Colyn1337
$smtpServer = "server.domain.com"
$smtpFrom = "[email protected]"
$smtpTo = "[email protected]"
$messageSubject = "The subject line of the email"
# Set up an SmtpClient for sending the message
$smtpClient = New-Object Net.Mail.SmtpClient
$smtpClient.Host = $smtpServer
# Create the MailMessage you want to send
$mailMessage = New-Object Net.Mail.MailMessage
$mailMessage.From = $smtpFrom
$mailMessage.To.Add($smtpTo)
$mailMessage.Subject = $messageSubject
# Create the html content for the mail
# Add embedded image resource to HTML
$htmlReport = "<image src=cid:DangeRussLogo>"
# And then the table with your data
$htmlReport += "<table>"
$htmlReport += "`n"
$htmlReport += "<tr>"
$htmlReport += "<th>RequestID</th>"
$htmlReport += "<th>RequesterName</th>"
$htmlReport += "<th>RequestType</th>"
$htmlReport += "<th>NotAfter</th>"
$htmlReport += "<th>CommonName</th>"
$htmlReport += "<th>EnrollmentFlags</th>"
$htmlReport += "</tr>"
$htmlReport += "`n"
foreach($cert in $CertToList)
{
$htmlReport += "<tr>"
$htmlReport += "<td>$($cert.RequestID)</td>"
$htmlReport += "<td>$($cert.RequesterName)</td>"
$htmlReport += "<td>$($cert.RequestType)</td>"
$htmlReport += "<td>$($cert.NotAfter)</td>"
$htmlReport += "<td>$($cert.CommonName)</td>"
$htmlReport += "<td>$($cert.EnrollmentFlags)</td>"
$htmlReport += "</tr>"
$htmlReport += "`n"
}
$htmlReport += "</table>"
# Now create an AlternateView from the HTML contents
$messageBody = [Net.Mail.AlternateView]::CreateAlternateViewFromString($htmlReport, 'text/html')
# Create a Linked Resource from the logo image
$imageMimeType = New-Object System.Net.Mime.ContentType("image/png")
$embeddedImage = New-Object Net.Mail.LinkedResource("C:\Users\DangeRuss\logo.png", $imageMimeType)
$embeddedImage.ContentId = "DangeRussLogo"
# Add the resource to the HTML view
$messageBody.LinkedResources.Add($embeddedImage)
# Add the HTML view to the MailMessage
$mailMessage.AlternateViews.Add($messageBody)
# And finally send the message
$smtpClient.Send($mailMessage)
如果您想使用PNG.jpeg
或.jpg
文件,请将 MimeType 从“image/png”更改为“image/jpeg”
答案2
因此,您已经完成了生成列表的困难部分$CertToList
。您可能过度思考了如何发送电子邮件,而这正是让您感到困惑的地方。请记住,Powershell 可以让您的生活更轻松。如果看起来太复杂,那么可能有更简单的方法来实现它。无论如何,您需要找出以下变量并将它们添加到您的代码中:
$smtpServer = "server.domain.com"
$smtpFrom = "[email protected]"
$smtpTo = "[email protected]"
$messageSubject = "The subject line of the email"
然后在代码的最末尾添加此行:
Send-MailMessage -From "$smtpFrom" -To "$smtpTo" -Subject "$messageSubject" -Body "$($CertToList)" -SMTPServer "$smtpServer"
您会注意到我以特殊方式记录了您的$CertToList
变量。我这样做是为了让 Powershell 确信 $CertToList 是一个变量并将其内容拉入正文中。现在,您可以随意创建正文,您可以执行以下操作:
-Body "This email contains the cert list: `n`n $($CertToList)"