每天我都需要在“销售总量:”之后搜索这个数字,然后发邮件给它。我很乐意自动化这个繁琐的任务(最好使用PowerShell)。
有谁能帮我拿个示例脚本吗?该脚本将在WindowsServer2012R2上使用PowerShell 3.0运行。
发布于 2016-01-27 09:22:52
下面的代码非常适合我:
$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}移除文件名和行号
发布于 2016-01-26 19:19:56
用于搜索字符串的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 {$_}
##########################################################然后参考这个文章来映射邮件的发送(因为我不确定您将使用哪个邮件客户端)。
https://stackoverflow.com/questions/35021104
复制相似问题