然后在文本文件中搜索电子邮件字符

时间:2016-01-26 18:26:49

标签: powershell-v3.0

每天我需要在" SALES TOTAL:"之后搜索这个数字。通过电子邮件发送我希望能够自动执行这项繁琐的任务(理想情况下使用PowerShell)。

有人可以帮我提供一个示例脚本吗?该脚本将在带有PowerShell 3.0的Windows Server 2012R2上运行。

Example text file

2 个答案:

答案 0 :(得分: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来确定邮件的发送方式(因为我不确定您将使用哪种邮件客户端)。

答案 1 :(得分: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}删除文件名和行号

相关问题