2017-04-11 114 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做一個測試消息,它工作正常.. –

相關問題