IIS:如何使用直接链接下载文件时接收电子邮件通知

当某个目录下的文件被下载时,是否可以configurationIIS发送电子邮件通知? 例如: www.example.com/download/任何文件?

你最好的select可能是在适当的时间间隔内查询IIS日志文件。

下面的PowerShell脚本应该做你想做的。 显然要改变这些variables以适合你的需要。

 # Directory of IIS log files $Path = "C:\inetpub\logs\LogFiles\W3SVC1" #Get the most recent log file from the directory $File = Get-ChildItem $Path | sort LastWriteTime | select -last 1 # Get-Content gets the file, pipe to Where-Object and skip the first 3 lines. $Log = Get-Content $File.FullName | where {$_ -notLike "#[D,SV]*" } # Replace unwanted text in the line containing the columns. $Columns = (($Log[0].TrimEnd()) -replace "#Fields: ", "" -replace "-","" -replace "\(","" -replace "\)","").Split(" ") # Count available Columns, used later $Count = $Columns.Length # Get all Rows that i Want to retrieve $QueryString = "*GET*" $Rows = $Log | where {$_ -like $QueryString} # Create an instance of a System.Data.DataTable $IISLog = New-Object System.Data.DataTable "IISLog" # Loop through each Column, create a new column through Data.DataColumn and add it to the DataTable foreach ($Column in $Columns) { $NewColumn = New-Object System.Data.DataColumn $Column, ([string]) $IISLog.Columns.Add($NewColumn) } # Loop Through each Row and add the Rows. foreach ($Row in $Rows) { $Row = $Row.Split(" ") $AddRow = $IISLog.newrow() for($i=0;$i -lt $Count; $i++) { $ColumnName = $Columns[$i] $AddRow.$ColumnName = $Row[$i] } $IISLog.Rows.Add($AddRow) } #Format Log data into string for sending $BodyString = "" foreach( $Row in $IISLog.Rows ){ $BodyString = $BodyString + $Row.date + " " + $Row.time + " " + $Row.sip + " " + $Row.csuriquery + "`n" } # Variables for sending email $MailServer = "NAME OF YOUR MAIL SERVER" $FromAddress = "" $ToAddress = "" $Subject = "" $SMTP = New-Object Net.Mail.SmtpClient($MailServer) $SMTP.Send($FromAddress,$ToAddress,$Subject,$BodyString) 

我使用了以下页面作为参考。

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/46bc6859-d9e3-47c3-b1a6-5132281df18b/howto-use-powershell-to-parse-iis-logs-files?forum=ITCG

http://exchangeserverpro.com/powershell-how-to-send-email/

有一种使用IIS Url重写模块和自定义重写提供程序的方法。 这意味着你必须写一些代码,但是我只是为你做了这个:

首先,你需要Url Rewrite模块 ,这是很好的反正。 安装它确保它的工作。

接下来按照开发URL重写模块的自定义重写提供程序的文章中的步骤操作

该文章中有一个代码部分,将其replace为以下内容:

  using System; using System.Collections.Generic; using System.Net.Mail; using Microsoft.Web.Iis.Rewrite; public class DownloadAlerter : IRewriteProvider, IProviderDescriptor { private string recipient, sender, server; public void Initialize(IDictionary<string, string> settings, IRewriteContext rewriteContext) { if (!settings.TryGetValue("Recipient", out recipient) || string.IsNullOrEmpty(recipient)) throw new ArgumentException("Recipient provider setting is required and cannot be empty"); if (!settings.TryGetValue("Sender", out sender) || string.IsNullOrEmpty(sender)) throw new ArgumentException("Sender provider setting is required and cannot be empty"); if (!settings.TryGetValue("Server", out server) || string.IsNullOrEmpty(server)) throw new ArgumentException("Server provider setting is required and cannot be empty"); } public string Rewrite(string value) { MailMessage message = new MailMessage(); message.From = new MailAddress(sender); message.To.Add(new MailAddress(recipient)); message.Subject = "Download Alert"; message.Body = string.Format("The following URL was downloaded: '{0}'" + Environment.NewLine + "For details check your IIS logs", value); SmtpClient client = new SmtpClient(); client.Host = server; client.Send(message); return value; } public IEnumerable<SettingDescriptor> GetSettings() { yield return new SettingDescriptor("Recipient", "Email address of the recipient"); yield return new SettingDescriptor("Sender", "Email address of the sender"); yield return new SettingDescriptor("Server", "The SMTP server to be used"); } } 

接下来的文章描述了你的web-config的变化,使用类似这样的东西:

  <rewrite> <providers> <provider name="DownloadAlerter" type="DownloadAlerter, Hahndorf.IIS.DownloadAlertProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx"> <settings> <add key="Recipient" value="peter@******.****" /> <add key="Sender" value="iis@******.****" /> <add key="Server" value="localhost" /> </settings> </provider> </providers> <rules> <rule name="Test"> <match url="specific-page\.html" /> <action type="Rewrite" url="{DownloadAlerter:{URL}}" appendQueryString="false" /> </rule> </rules> </rewrite> 

provider type是您在本文后面创build的提供程序集的强名称。 这些settings允许您指定服务器和收件人。 match url定义了您正在接收电子邮件的url。

这只是一个概念的certificate,但它对我来说工作得很好。 我遇到了一个问题,因为我在一个设置为“No Managed Code”的IIS站点上testing了这个问题,所以你需要有一个.NET CLR Version集合,它与你编写Provider的版本相匹配。

此外,我无法访问有关请求的信息,如远程IP地址,但我没有太多的了解。

我把项目上传到GitHub。

不知道一个在所有的解决scheme,但看看这个:

1-使用一个软件来阅读iis日志,并与某些“代码”电子邮件结果,当某些匹配您的监控文件。 在logstash中查找带有电子邮件输出的elk堆栈。 不要忘记首先启用日志logging。

2-使用你的代码的networking应用程序来生成一个邮件,当有人得到这个文件。