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

Powershell 3.0 然后在文本文件中搜索电子邮件字符串,powershell-3.0,Powershell 3.0,每天我都需要在“销售总额”后搜索数字,并通过电子邮件发送。我希望能够自动完成这项繁琐的任务(最好使用PowerShell) 有人能帮我举个例子吗?该脚本将在使用PowerShell 3.0的Windows Server 2012R2上运行 用于搜索字符串的Powershell脚本: ########################################################### $Path = "C:\temp" $Text = "SALES TOTAL" $PathArr

每天我都需要在“销售总额”后搜索数字,并通过电子邮件发送。我希望能够自动完成这项繁琐的任务(最好使用PowerShell)

有人能帮我举个例子吗?该脚本将在使用PowerShell 3.0的Windows Server 2012R2上运行


用于搜索字符串的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 {$_}


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

然后参考此图来确定邮件的发送(因为我不确定您将使用哪种邮件客户端)。

以下代码对我来说非常适合:

$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对象{$\u.Line}
使用ps命令删除文件名和行号

:选择字符串-路径c:\day.log-模式“SALES TOTAL:”我可以选择包含“SALES TOTAL:FIGURE”的所有行。我不知道如何选择最后一行并通过电子邮件发送此信息。(我知道“sendmailmessage”命令)我认为这个脚本将搜索包含字符串的文件,然后将文件名输出到不同的目录。有用但不是我想要的
$Text=“SALES TOTAL”选择字符串-路径c:\day.log-模式“$Text”-列表
是目前为止我能做的最好的选择,它将显示第一行,需要最后一行。