将今天的日期与CSV进行比较,如果已过期则发送电子邮件

时间:2017-05-05 07:47:28

标签: powershell

有人可以告诉我如何比较CSV文件中的日期并使用PowerShell发送提醒电子邮件吗? 感谢。

此致 LOOI

1 个答案:

答案 0 :(得分:0)

我猜这可能就是答案。

# Just imitating Import-Csv here...
$csv = "Text,Date",
       "Abc,6/10/2017",
       "Def,3/26/2017",
       "Uvw,2/5/2017 ",
       "Xyz,2/13/2017" | ConvertFrom-Csv

# Get the date in a sortable form and filter:
$out = $csv | Select-Object -Property Text,
                                      @{Name="Date"; Expression={Get-Date $_.Date}} |
              Where-Object {$_.Date -lt (Get-Date 3/1/2017)}

# If any are earlier than 3/1/2017, send email:
if ($out)
{
    # I'm outputting strings that look like the Send-MailMessage cmdlet...
    $out | ForEach-Object {
        "Send-MailMessage -To      `"Usr <usr@grandwazzoo.com>`"
                          -From    `"$($_.Text) <$($_.Text)@grandwazzoo.com>`"
                          -Subject `"A Reminder`""
    }
}