每天我都需要在“SALES TOTAL:”后搜索数字并发送电子邮件。我很想将这个单调乏味的任务自动化(最好使用PowerShell)。然后在文本文件中搜索电子邮件字符串
有人可以帮我用这个请示例脚本吗?该脚本将使用PowerShell 3.0在Windows Server 2012R2上运行。
每天我都需要在“SALES TOTAL:”后搜索数字并发送电子邮件。我很想将这个单调乏味的任务自动化(最好使用PowerShell)。然后在文本文件中搜索电子邮件字符串
有人可以帮我用这个请示例脚本吗?该脚本将使用PowerShell 3.0在Windows Server 2012R2上运行。
下面这段代码完全适用于我:
$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}
删除的文件名和行号
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绘制出邮件的发送(因为我不确定你会使用什么邮件客户端) 。
我觉得这个脚本会搜索文件中包含一串文本的文件,然后将文件名移出到不同的目录。有用但不是我正在寻找的。 '$ Text =“SALES TOTAL” select-string -path c:\ day.log -pattern“$ text”-list'是目前为止我能做的最好的,它将显示第一行,需要最后一行。 – user3355677
使用ps命令:选择 - string -path c:\ day.log -pattern“SALES TOTAL:”我可以选择所有具有“SALES TOTAL:FIGURE”的行我不知道如何选择最后一行并通过电子邮件发送。 (我知道“Send-MailMessage”命令) – user3355677