2017-04-11 70 views
1

我想每周五通过计划任务自动执行电子邮件,以告知各个站点更改周末备份的磁带。发送电子邮件根据日期从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 "[email protected]" -To "[email protected]" -cc "[email protected]" -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" 
+0

嗨,只是有一个想法,有磁带的变化,如每隔一天添加和删除,我可以做一个if语句,比如如果今天的日期在csv然后做到上述? –

回答

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 "[email protected]" -To "[email protected]" -cc "[email protected]" -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属性时,还需要在子表达式$()中包装字符串。

+0

谢谢马克。我收到有关位置参数的错误? –

+0

Send-MailMessage <<<< -From“[email protected]”+ CategoryInfo:InvalidArgument:(:) [Send-MailMessage],ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound,Microsoft.PowerShell.Commands.SendMailMessage –

+0

当我使用send-mailmessage做一个测试消息,它工作正常.. –