根据日期

时间:2017-04-11 09:14:40

标签: powershell csv

我希望每周五通过计划任务自动发送电子邮件,告诉各个网站更改周末备份的磁带。

旋转和磁带编号与日期一起位于csv文件中。

所以我想我可以把这个日期的数据和电子邮件发送给相关部门。

到目前为止我的内容如下。我可以从csv中读取,但是获取所有输出而不仅仅是该日期的信息。我设置adddays -1并认为可以捕获截至昨天的任何日期?

$Data =import-csv "C:\New folder\csv-1.csv" | select "Item-Date", "Tape_1", "Tape_3", "Tape_2", "Rotation"
$CheckDate = (Get-Date).AddDays(-1)
    Where-Object {
        ($_."Item-Date" -as [DateTime]) -lt $CheckDate
    }
    $PSEmailServer = "SomeSMTPserver.com"
Send-MailMessage -From "User3@email.co.uk" -To "User2@email.co.uk" -cc "User@email.co.uk" -Subject "Tape Changes2" -Body "Hi User, Could you please change the tapes, inserting the tapes "$_.Tape_1", "$_.Tape_2", "$_.Tape_3", for rotation week "$_.Rotation". Thanks"

1 个答案:

答案 0 :(得分:1)

您的where-object目前没有做任何事情,因为它没有从管道接收任何输入或将结果返回给任何变量。

在不知道您的CSV数据的情况下很难确定,但您可能希望这样做(包括foreach-object循环,以防where-object返回多个结果):

$PSEmailServer = "SomeSMTPserver.com"

$Data =import-csv "C:\New folder\csv-1.csv" | select "Item-Date", "Tape_1", "Tape_3", "Tape_2", "Rotation"

$CheckDate = (Get-Date).AddDays(-1)
$TapeData = $Data | Where-Object { ($_."Item-Date" -as [DateTime]) -lt $CheckDate }

$TapeData | ForEach-Object {    
    Send-MailMessage -From "User3@email.co.uk" -To "User2@email.co.uk" -cc "User@email.co.uk" -Subject "Tape Changes2" -Body "Hi User, Could you please change the tapes, inserting the tapes $($_.Tape_1), $($_.Tape_2), $($_.Tape_3), for rotation week $($_.Rotation). Thanks"
}

请注意,如上所示,当您访问其中的.property时,还需要将字符串包装在子表达式$()中。