【问题标题】:PowerShell send email using CSV Column Provided emailPowerShell 使用 CSV 列发送电子邮件 提供的电子邮件
【发布时间】:2018-03-28 03:11:32
【问题描述】:

CSV 文件格式标题:

Request ID  Requester Name  Binary  Template    Serial Number   Certificate Effective Date  Certificate Expiration Date Issued Country.Region   Issued Organization Issued Organization Unit    Issued Common Name  Issued City Issued State    Issued Email Address

我有以下代码,我尝试使用 CSV“已发行电子邮件地址”中提供的电子邮件发送电子邮件,其中已发行电子邮件和序列号位于同一行。我的代码将电子邮件发送到每个已发布的电子邮件地址,但在邮件正文中,它包含所有已发布电子邮件用户的所有信息。

   Param(
    [Int]$Months = $null,
    [Int]$Days = $null,
    [switch]$noMail = $false
)
# --------------------------------------------------
#functions

Function Send-CertificateList
{
    $FromAddress = 'myemail@email.com'
    $ToAddress = $ToAddress
    $MessageSubject = "Certificate expiration reminder from $env:COMPUTERNAME.$env:USERDNSDOMAIN"
    $SendingServer = 'smtp.SendingServer.com'
    $port = '587'

    $SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $mailbody -ErrorAction SilentlyContinue
    $SMTPMessage.IsBodyHTML = $true
    $SMTPMessage.Priority = [System.Net.Mail.MailPriority]::High
    $SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer -ErrorAction SilentlyContinue

    if(Test-Connection -Cn $SendingServer -BufferSize 16 -Count 1 -ea 0 -quiet){
        $SMTPClient.Send($SMTPMessage)
    }else{
        Write-Host 'No connection to SMTP server. Failed to send email!'
        Write-Output 'No connection to SMTP server. Failed to send email!' | Out-File $mailstatus -Append
    }
}

# --------------------------------------------------

#HTML Style
$style = "<style>body{font-family:`"Calibri`",`"sans-serif`"; font-size: 14px;}"
$style = $style + "@font-face
    {font-family:`"Cambria Math`";
    panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
    {font-family:Tahoma;
    panose-1:2 11 6 4 3 5 4 4 2 4;}"
$style = $style + "table{border: 1px solid black; border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt;}"
$style = $style + "th{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "td{border: 1px solid black; padding: 5px; }"
$style = $style + ".crtsn{font-weight: bold; color: blue; }"
$style = $style + ".crtexp{font-weight: bold; color: red; }"
$style = $style + ".crtcn{font-weight: bold; color: orange; }"
$style = $style + "</style>"

# --------------------------------------------------
#variables
$strDate = get-date -format yyyyMMdd-HHmmss
#$exportFileName = "certs_" + $strDate + ".csv"
$now = Get-Date
$nowm = $now.Month
$nowy = $now.Year
$mailbody = @()
$expirymy = @()
$table = @()
$ToAddress = @()

# --------------------------------------------------
#variables

#export certificates to CSV
#certutil.exe -view csv > $exportFileName
$a = Import-Csv .\csv_file.csv 
#Import certificates from CSV
$importexp = $a | Select-Object 'Certificate Expiration Date'
$importall = $a | Where-Object {$_.'Serial Number' -notcontains 'EMPTY'} | Where-Object {$_.'Certificate Expiration Date' -like "*.$arg0*"} | Where-Object {$_.'Issued Email Address' -notcontains 'EMPTY'} | Select-Object -Property 'Request ID','Serial Number','Requester Name','Certificate Expiration Date','Request Common Name','Request Disposition', 'Issued Email Address'

#build email body in HTML
$mailbody += '<html><head><meta http-equiv=Content-Type content="text/html; charset=utf-8">' + $style + '</head><body>'
$mailbody += "These certificates will expire soon:<br />"

#convert dates and extract month and year
foreach($i in $importall){
   $expiry = Get-Date $i.'Certificate Expiration Date' -Format 'M.d.yyyy HH:mm'
   $expirymy += Get-Date $expiry | Select-Object Month, Year
}

#cycle through array and search for matching cetificates
for($i=0;$i -lt $expirymy.Count;$i++){
    if(($expirymy[$i].Month -gt $nowm)){
        if((($expirymy[$i].Month - $nowm) -le $Months) -and (($expirymy[$i].Year - $nowy) -eq '0')){
            Write-Host 'Certificate ID:' $importall[$i].'Request ID' 'with Serial Number:' $importall[$i].'Serial Number' 'will expire in ' -NoNewline; Write-Host ($expirymy[$i].Month - $nowm) 'months!'-ForegroundColor Red            
            Write-Host 'Email:' $importall[$i].'Issued Email Address'
            Write-Host 'Please don`t forget to renew this certificate before expiration date: ' -NoNewline; Write-Host $importall[$i].'Certificate Expiration Date' -ForegroundColor Red "`n"

            $ToAddress = $importall[$i].'Issued Email Address'
            $mailbody += '<p>'
            $mailbody += 'Certificate ID: ' + $importall[$i].'Request ID' + ' with Serial Number: <span class="crtsn"">' + $importall[$i].'Serial Number' + '</span> will expire in <span class="crtexp">' + ($expirymy[$i].Month - $nowm) + ' months!</span>'+"<br />"
            $mailbody += 'This certificate has Common Name: <span class="crtcn">' + $importall[$i].'Issued Common Name' + "</span><br />"
            $mailbody += $importall[$i].'Issued Email Address'
            $mailbody += 'Please don`t forget to renew this certificate before expiration date: <span class="crtexp">' + $importall[$i].'Certificate Expiration Date'+"</span>"
            $mailbody += '</p>'
            $table += $importall[$i] | Sort-Object 'Certificate Expiration Date' | Select-Object -Property 'Request ID','Serial Number','Requester Name','Certificate Expiration Date','Issued Common Name', 'Issued Email Address'
            Write-Host $table
        }
    }
}

$mailbody += '<p><table>'
$mailbody += '<th>Request ID</th><th>Serial Number</th><th>Requester Name</th><th>Issued CN</th><th>Expiration date</th><th>Email</th>'

foreach($row in $table){
    $mailbody += "<tr><td>" + $row.'Request ID' + "</td><td>" + $row.'Serial Number' + "</td><td>" + $row.'Requester Name' + "</td><td>" + $row.'Issued Common Name' + "</td><td>" + $row.'Certificate Expiration Date' + "</td> <td>" + $row.'Issued Email Address' + "</td></tr>"
    $ToAddress = $row.'Issued Email Address'
}

$mailbody += '</table></p>'
$mailbody += '</body>'
$mailbody += '</html>'
#$ToAddress = $ToAddress
foreach($row in $table){
    $ToAddress = $row.'Issued Email Address'
    Write-Host $ToAddress
    if(($table.Count -gt '0') -and (!$noMail)){
        Send-CertificateList
}
}

【问题讨论】:

  • 你想解决什么问题?

标签: powershell csv


【解决方案1】:

我无法轻松测试这一点,但在我看来,没有必要拥有foreach ($i in $ImportAll)for ($i=0;$i -lt $expirymy.Count;$i++)。据我所知,一切都可以在您的foreach 循环中完成。还可以将月份和年份作为属性添加到每个对象中。

foreach($Entry in $Importall)
{
   $expiry = Get-Date $i.'Certificate Expiration Date' 
   $Entry | Add-Member -MemberType NoteProperty -Name Month -Value $expiry.Month
   $Entry | Add-Member -MemberType NoteProperty -Name year -Value $expiry.Year


    #cycle through array and search for matching cetificates
    if(($Entry.Month -gt $nowm)){
        if((($Entry.Month - $nowm) -le $Months) -and (($Entry.Year - $nowy) -eq '0')){
            Write-Host 'Certificate ID:' $Entry.'Request ID' 'with Serial Number:' $Entry.'Serial Number' 'will expire in ' -NoNewline; Write-Host ($Entry.Month - $nowm) 'months!'-ForegroundColor Red            
            Write-Host 'Email:' $Entry.'Issued Email Address'
            Write-Host 'Please don`t forget to renew this certificate before expiration date: ' -NoNewline; Write-Host $Entry.'Certificate Expiration Date' -ForegroundColor Red "`n"

            $ToAddress = $Entry.'Issued Email Address'
            $mailbody += '<p>'
            $mailbody += 'Certificate ID: ' + $Entry.'Request ID' + ' with Serial Number: <span class="crtsn"">' + $Entry.'Serial Number' + '</span> will expire in <span class="crtexp">' + ($Entry.Month - $nowm) + ' months!</span>'+"<br />"
            $mailbody += 'This certificate has Common Name: <span class="crtcn">' + $Entry.'Issued Common Name' + "</span><br />"
            $mailbody += $Entry.'Issued Email Address'
            $mailbody += 'Please don`t forget to renew this certificate before expiration date: <span class="crtexp">' + $Entry.'Certificate Expiration Date'+"</span>"
            $mailbody += '</p>'
            $table += $Entry | Sort-Object 'Certificate Expiration Date' | Select-Object -Property 'Request ID','Serial Number','Requester Name','Certificate Expiration Date','Issued Common Name', 'Issued Email Address'
            Write-Host $table
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-12-15
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2018-08-06
    • 2016-04-17
    • 2017-12-07
    • 1970-01-01
    相关资源
    最近更新 更多