2016-01-26 32 views
0

每天我都需要在「SALES TOTAL:」後搜索數字併發送電子郵件。我很想將這個單調乏味的任務自動化(最好使用PowerShell)。然後在文本文件中搜索電子郵件字符串

有人可以幫我用這個請示例腳本嗎?該腳本將使用PowerShell 3.0在Windows Server 2012R2上運行。

Example text file

+0

使用ps命令:選擇 - string -path c:\ day.log -pattern「SALES TOTAL:」我可以選擇所有具有「SALES TOTAL:FIGURE」的行我不知道如何選擇最後一行並通過電子郵件發送。 (我知道「Send-MailMessage」命令) – user3355677

回答

0

下面這段代碼完全適用於我:

$finds = Select-String -Path "file path" -Pattern "text to search" | ForEach-Object {$_.Line} 

Send-MailMessage -To "email.address", -From "email.address" -Subject "subject" -body $finds[-1] -SmtpServer "smtp address" here 

注: [-1](只顯示最後一行) ForEach-Object {$_.Line}刪除的文件名和行號

0

PowerShell腳本來搜索字符串:

########################################################### 
$Path = "C:\temp" 
$Text = "SALES TOTAL" 
$PathArray = @() 
$Results = "C:\temp\test.txt" 

# This code snippet gets all the files in $Path that end in ".txt". 
Get-ChildItem $Path -Filter "*.txt"」 | 
Where-Object { $_.Attributes -ne "「Directory"} | 
ForEach-Object { 
If (Get-Content $_.FullName | Select-String -Pattern $Text) { 
$PathArray += $_.FullName 
$PathArray += $_.FullName 
} 
} 
Write-Host "Contents of ArrayPath:" 
$PathArray | ForEach-Object {$_} 


########################################################## 

,然後參考該article繪製出郵件的發送(因爲我不確定你會使用什麼郵件客戶端) 。

+0

我覺得這個腳本會搜索文件中包含一串文本的文件,然後將文件名移出到不同的目錄。有用但不是我正在尋找的。 '$ Text =「SALES TOTAL」 select-string -path c:\ day.log -pattern「$ text」-list'是目前爲止我能做的最好的,它將顯示第一行,需要最後一行。 – user3355677