当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

作者:精品下载站 日期:2024-12-14 09:07:29 浏览:14 分类:玩电脑

使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3


PowerShell Get-MailDetailSpamReport cmdlet 是作为一种工具创建的,可简化查看和“转储”存储在 Exchange Online 垃圾邮件日志文件中的信息的过程。 Exchange Online 垃圾邮件日志文件充当存储库,保存与传入和传出邮件流相关的每个“垃圾邮件事件”的文档。

在本文中,我们将回顾如何扩展和增强 Exchange Online PowerShell cmdlet - Get-MailDetailSpamReport 的功能。

系列文章|使用 Get-MailDetailSpamReport |查看和导出垃圾邮件报告

  1. 使用 PowerShell 报告 Office 365 垃圾邮件 |介绍
  2. 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告(本文)
  3. 如何使用垃圾邮件报告PowerShell脚本

在第一部分中,我们将回顾可与 Get-MailDetailSpamReport cmdlet 一起使用的基本 PowerShell 语法示例。
在第二部分中,我们将回顾 et-MailDetailSpamReport cmdlet 的更高级用法。 >Get-MailDetailSpamReport cmdlet,通过使用 PowerShell 脚本来帮助我们执行一系列任务并生成各种类型的垃圾邮件报告(通过使用搜索查询)。

第 1 部分:Get-MailDetailSpamReport cmdlet 的基本使用

显示 - 垃圾邮件报告信息

要获取垃圾邮件报告,我们只需键入 Get-MailDetailSpamReport cmdlet 的名称,无需任何其他强制参数。

例如:

Get-MailDetailSpamReport | FT Date,Subject,Direction,SenderAddress,RecipientAddress

PowerShell Get-MailDetailSpamReport 输出:

PS C:\> Get-MailDetailSpamReport | FT Date,Domain,Subject,Direction,SenderAddress,RecipientAddress,EventType,MessageSize,MessageId,MessageTraceId

Date                   Domain                    Subject                                                                                                           Direction SenderAddress                               RecipientAddress
----                   ------                    -------                                                                                                           --------- -------------                               ----------------
2017-09-28 1:42:16 PM  o365info.com              INBOX SMTP,RDP,UNLIMITED WEBMAIL,FRESH LEADS,FUND TRANSFER TO ALL BANKS IN USA..all available now.                Inbound   [email protected]                        [email protected]
2017-09-28 1:42:25 PM  o365info.com              Undeliverable: INBOX SMTP,RDP,UNLIMITED WEBMAIL,FRESH LEADS,FUND TRANSFER TO ALL BANKS IN USA..all available now. Outbound  [email protected]                     [email protected]
2017-09-28 1:42:25 PM  o365info.com              Undeliverable: INBOX SMTP,RDP,UNLIMITED WEBMAIL,FRESH LEADS,FUND TRANSFER TO ALL BANKS IN USA..all available now. Outbound  [email protected]                     [email protected]
2017-09-28 7:50:37 PM  o365info.com              Scan Data                                                                                                         Outbound  [email protected]                      [email protected]
2017-09-28 8:24:52 PM  o365info2.onmicrosoft.com Cloud App Security: suspicious administrative activity detected                                                   Inbound   [email protected]               [email protected]
2017-09-29 12:41:46 AM o365info.com              RE: ANITA ROBERTS ( DEVCOIN CRYPTOCURRENCY )                                                                      Inbound   [email protected]                   [email protected]
2017-09-29 2:39:59 AM  o365info.com              Scan Data                                                                                                         Outbound  [email protected]                        [email protected]
2017-09-29 7:02:53 PM  o365info.com              Become a Target Content Curator Today!                                                                            Inbound   [email protected] [email protected]
2017-09-29 11:31:00 PM mottie.com                Invoice                                                                                                           Outbound  [email protected]                            [email protected]
2017-09-30 5:16:18 AM  alwasmiwater.com          BENEFICIARY PAYMENT ADVICE                                                                                        Outbound  [email protected]                   [email protected]
2017-09-30 6:38:37 AM  o365info.com              Top On Web                                                                                                        Inbound   [email protected]                            [email protected]
2017-09-30 7:07:23 AM  eenvoudigonline.be        Invoice                                                                                                           Outbound  [email protected]                     [email protected]
2017-09-30 8:04:06 AM  timberlineroof.net        Invoice                                                                                                           Outbound  [email protected]                  [email protected]
2017-09-30 9:24:07 AM  o365info.com              Invitation: UNITED BANK OF AFRICA @ Friday, 29 September 2017                                                     Inbound   [email protected]                       [email protected]
2017-10-01 7:43:34 PM  interia.com               QUOTATION  doc20171002                                                                                            Outbound  [email protected]                      [email protected]
2017-10-02 4:09:56 AM  o365info.com              Hi                                                                                                                Inbound   [email protected]                     [email protected]
2017-10-02 8:24:58 AM  o365info.com              What's up                                                                                                         Inbound   [email protected]            [email protected]
2017-10-02 10:36:54 PM o365info.com              Hey, Sweety                                                                                                       Inbound   [email protected]                   [email protected]
2017-10-03 1:15:58 AM  o365info.com              Emailed Invoice - 822287                                                                                          Outbound  [email protected]                         [email protected]
2017-10-03 2:19:17 AM  o365info.com              Hey, Sweety                                                                                                       Inbound   [email protected]                  [email protected]

将垃圾邮件报告信息导出到 CSV 文件。

要将垃圾邮件报告的结果导出到 CSV 文件,我们可以使用以下语法。

PowerShell命令语法:

Get-MailDetailSpamReport | Export-CSV <Path> -NoTypeInformation -Encoding utf8

PowerShell 命令示例:

Get-MailDetailSpamReport | Export-CSV c:\temp\"spam mail report.CSV" -NoTypeInformation -Encoding UTF8

将垃圾邮件报告信息导出到 CSV 文件 |具体日期范围

在这种情况下,我们希望将垃圾邮件报告的结果导出到 CSV 文件,但这一次。我们想要为垃圾邮件报告定义一个特定的日期范围。

为了定义所需的日期范围,我们使用参数-StartDate -EndDate

PowerShell命令语法:

Get-MailDetailSpamReport -StartDate <Date> -EndDate <Date>

PowerShell 命令示例:

Get-MailDetailSpamReport -StartDate 09/01/2017 -EndDate 10/01/2017

简短前缀 - 垃圾邮件发件人与垃圾邮件收件人

垃圾邮件日志文件包含每个垃圾邮件事件的许多“属性”。

我们需要熟悉的最重要的区别之一是某些实体(表示为电子邮件地址)发送垃圾邮件的事件。其含义是——掌握“攻击者”规则的一方,与接收垃圾邮件的用户或收件人的实体,即垃圾邮件攻击的受害者。

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

这种区别的实现是通过使用以下 PowerShell 参数来实现的:

  • PowerShell参数-SenderAddress,定义发送垃圾邮件的实体
  • PowerShell 参数-RecipientAddress,定义接收垃圾邮件的实体。

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

获取垃圾邮件报告|过滤结果 - 特定垃圾邮件接收者

在这种情况下,我们希望获取有关所有垃圾邮件事件的信息,其中垃圾邮件被发送到(接收)特定的“目标电子邮件地址”。

为了定义“收件人”的身份,我们使用 PowerShell 参数-RecipientAddress,并提供目标收件人的电子邮件地址。

PowerShell命令语法:

Get-MailDetailSpamReport -RecipientAddress <Sender E-mail address>

PowerShell 命令示例:

Get-MailDetailSpamReport -RecipientAddress "[email protected]"

获取垃圾邮件报告|筛选结果 |特定垃圾邮件发件人

在这种情况下,我们希望获取有关所有垃圾邮件事件的信息,其中垃圾邮件是从特定电子邮件地址发送的(发送者)。

为了定义“发件人”的身份,我们使用 PowerShell 参数-SenderAddress,并提供发件人的电子邮件地址。

PowerShell命令语法:

Get-MailDetailSpamReport -SenderAddress <Sender E-mail address>

PowerShell 命令示例:

Get-MailDetailSpamReport -SenderAddress "[email protected]"

获取垃圾邮件报告|筛选结果 |垃圾邮件接收者的电子邮件地址包含特定域名后缀

在这种情况下,我们希望获取有关发送给特定收件人(目标电子邮件地址)的垃圾邮件的信息。请注意,这次我们感兴趣的是“多个收件人”与与收件人的单个实体相关的默认 PowerShell 参数 -RecipientAddress

我们的愿望是获取有关发送给收件人的所有垃圾邮件的信息,这些收件人的电子邮件地址包含特定的域名后缀。

例如,获取有关发送给我们组织收件人(即电子邮件地址包含我们组织域名后缀的收件人)的所有垃圾邮件的信息。

在这种情况下,我们使用 PowerShell 参数“Where-Object”,它根据我们定义的搜索查询来过滤信息。在我们的场景中,搜索查询语法为:

{$_.RecipientAddress -like "*@<Domain name suffix>*"}

PowerShell命令语法:

Get-MailDetailSpamReport -StartDate <Date> -EndDate <Date> | Where-Object {$_.RecipientAddress -like "*@<Domain name suffix>*"} | Select SenderAddress,RecipientAddress ,Date

PowerShell 命令示例:

Get-MailDetailSpamReport -StartDate $StartDate -EndDate $EndDate | Where-Object {$_.RecipientAddress -like "*@o365info.com*"} | Select SenderAddress,RecipientAddress ,Date

获取垃圾邮件报告|筛选结果 |垃圾邮件发件人的电子邮件地址包含特定域名后缀

这个场景与之前的场景类似,但是这一次;我们想要获取有关所有垃圾邮件事件的信息,这些事件与使用带有特定域名后缀的电子邮件的“垃圾邮件发件人”(发送垃圾邮件的实体)有关。

PowerShell命令语法:

Get-MailDetailSpamReport -StartDate <Date> -EndDate <Date> | Where-Object {$_.SenderAddress -like "*@<Domain name suffix>*"} | Select SenderAddress,RecipientAddress,Date

PowerShell 命令示例:

Get-MailDetailSpamReport -StartDate $StartDate -EndDate $EndDate | Where-Object {$_.SenderAddress -like "*@o365info.com*"} | Select SenderAddress,RecipientAddress,Date

第 2 部分:Get-MailDetailSpamReport cmdlet 的更高级使用

Get-MailDetailSpamReport cmdlet 使用的一般概念

术语“垃圾邮件事件”

在本文中,我们将多次提到“垃圾邮件事件”一词。这个术语不是正式术语,而是我使用的术语。

术语“垃圾邮件事件”定义了 Exchange Online 垃圾邮件日志文件中记录的事件,其中包括以下两个选项之一的文档:

  • 从特定实体(电子邮件地址)发送的垃圾邮件。
  • 由特定实体(电子邮件地址)接收(发送至)的垃圾邮件。

术语“垃圾邮件事件记录”包括事件的属性,例如日期、邮件ID、邮件主题等。

优化我们使用日期范围的方式

据我所知,目前还没有关于 Get-MailDetailSpamReport PowerShell cmdlet 使用的默认时间范围的正式信息。

注意 - 我不知道 Exchange Online 垃圾邮件日志文件中保存的垃圾邮件事件的最大日期范围是多少。根据我的经验,我们可以“获取”30 天或更长时间内有关垃圾邮件事件的信息。

为了能够定义确切的日期范围,我们可以向基本的 Get-MailDetailSpamReport PowerShell cmdlet 添加参数 -StartDate-EndDate,这使我们能够设置确切的日期范围。

例如:

Get-MailDetailSpamReport -StartDate 09/01/2017 -EndDate 10/01/2017

在本文中,我将使用“日期范围参数”的自定义。我们将使用一个简单的 PowerShell 公式,该公式定义将以“天为单位”测量的日期范围。

在我们的示例中,我们定义 30 天的日期范围。

注意:使用这个简单的 PowerShell 公式,您可以轻松创建满足您的特定需求的所需日期范围自定义。

结束日期值

我们没有手动提供“结束日期”的值,而是使用一个变量来存储从 PowerShell cmdlet Get-Date 获取的结果。
PowerShell cmdlet Get-Date 配置为“获取”当前日期。

开始日期值

我们没有手动提供“开始日期”的值,而是使用一个变量来存储简单公式的结果。我们使用的公式采用当前日期并从此日期减去我们指定的天数。

该公式的结果将是“开始日期”。

我们将在当前文章中使用的两个变量的示例是:

$EndDate = Get-Date

$StartDate = $EndDate.Adddays(-30)

在此示例中,我们定义“30 天”的日期范围。

在下图中,我们可以看到“日期范围”概念的示例。

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

转储 Exchange Online 垃圾邮件日志文件的内容

在本节中,我们回顾如何“转储”Exchange Online 垃圾邮件日志文件的内容,但重点是我们使用的一个技巧,它将帮助我们绕过 Get-MailDetailSpamReport PowerShell cmdlet 的默认限制,因此我们将能够在我们定义的日期范围内导出(转储)Exchange Online 垃圾邮件日志文件的全部内容。

导出垃圾邮件报告 |所有垃圾邮件事件 |过去 30 天

我们的使命:导出过去 30 天内所有现有的垃圾邮件事件。

请注意,在这种情况下,我们不想使用任何类型的过滤器或定义特定的搜索查询。相反,我们只想将 Exchange Online 垃圾邮件日志文件中的所有信息“转储”到本地文件。

Exchange Online 和秘密数据限制默认设置

当我们要求 Exchange Online“获取”存储在数据中心日志文件中的信息时,我们大多数人不知道的小秘密是,默认情况下,Exchange Online 不会“志愿者”为我们提供100%的信息。

即使我们提供了定义的“日期范围”,Exchange Online 也会配置为提供“有限数量的数据”。

这种内置限制的原因可能是某种“服务器保护机制”,该机制是为了防止 Exchange Online 服务器的过载或通过限制从“云”流出的数据量来防止通信线路的过载而创建的。

数据衡量单位 - 页面

我们用来衡量 Exchange Online 服务器提供的“数据量”的方式,使用名为“页面”的衡量单位进行定义。

每个“页面”可以包含最大数量的“行”。

如果我们需要获取的信息多于单个“页面”中可以存储的信息,我们需要“指示”Exchange Online 为我们提供额外的“页面”。

页面内容默认和最大限制

  • 单个页面上显示的“事件”(日志行)的默认数量为 - 1,000。
  • 单个页面上可以出现的“事件”(日志行)的最大数量为 - 5,000。

页面默认和最大限制

  • 默认页数为 - 1。
  • Exchange Online 可以提供的最大页数为 - 1,000。

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

如果您想进行匹配,Exchange Online 可以为我们提供的最大结果是 - 500 万个事件 (5,000,00000)。

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

Exchange Online 和 Get-MailDetailSpamReport cmdlet 默认设置

关于我们从 Get-MailDetailSpamReport PowerShell cmdlet 获得的输出,默认设置配置为生成最多包含一千行 (1 X 1,000) 的“1 页”。

换句话说,默认情况下,Get-MailDetailSpamReport PowerShell cmdlet 配置为仅向我们提供存储在 Exchange Online 垃圾邮件日志文件中的现有信息的“第一页”。

为了演示这个概念,我们使用以下场景:Exchange Online 垃圾邮件日志,包括 15,000 个垃圾邮件事件的文档。

(Exchange Online 垃圾邮件日志文件包含 - 15,000 行)。

如果我们使用不带任何筛选器或参数的 Get-MailDetailSpamReport PowerShell cmdlet,Exchange Online 将向我们“发送”有关 1,000 个事件的信息。

快速提醒 - 默认情况下,Exchange Online 配置为仅提供一个“页面”,并且每个页面默认配置为最多 1,000 个“行”。

这种“默认配置”可能会导致“数据完整性”问题,因为我们无法看到全貌,而只能看到整个信息的一部分。

好消息是 Get-MailDetailSpamReport PowerShell cmdlet 允许我们定义两个重要参数:

  • 页面大小 - 每页包含的“事件”(行)的最大数量。一个页面可以包含的最大行数为 - 5,000。
  • 页数——我们“要求获得”的页数。

在我们的场景中,如果我们想要获得包含 100% 可用信息的“完整垃圾邮件报告”,我们需要“扩展”默认的 Get-MailDetailSpamReport PowerShell 命令语法通过以下方式:

Get-MailDetailSpamReport -PageSize 5000 -Page 3

使用 PowerShell 脚本转储垃圾邮件日志文件中的所有信息

虽然这个“解决方案”看起来像是对我们问题的满意答案,但我们还有其他问题需要解决!

在当前场景中,我提到我们“知道”Exchange Online 垃圾邮件日志文件包含 15,000 行,但实际上,我们如何才能真正知道日志文件中存储的事件数量,以便我们可以对 PowerShell 命令语法进行必要的调整?

答案是,大多数时候,我们不知道 Exchange Online Server 日志文件中出现的行(事件)的确切“数量”是多少。

为了能够应对这一挑战,我们可以使用一些 PowerShell“调整”来提供所需的解决方案。

该解决方案是通过使用“循环”过程来实现的,该过程将指示 Exchange Online 使用 5,000 行的页面大小为我们提供所需的结果。

如果 Exchange Online 垃圾邮件日志文件中存在其他信息,PowerShell“循环命令”会要求 Exchange Online 创建一个“新页面”,其中将包含接下来的 5,000 个结果,依此类推。

此循环过程将一直运行,直到我们获得存储在 Exchange Online 垃圾邮件日志文件中的所有可用信息

在下一节中,我们可以看到此类解决方案的示例。 PowerShell 脚本将执行以下操作序列:

将日期范围定义为 30 天的日期范围(从当前日期向后推 30 天)。

  1. 在驱动器 C: NEW 文件夹结构中自动创建,该文件夹结构将用作将导出的信息的容器。
  2. 从 PowerShell“循环进程”(从 Exchange Online 垃圾邮件日志文件获取所有可用信息)收集的“数据”将保存在名为 $SpamMailLogFileContent 的变量中。
  3. 名为 $SpamMailLogFileContent 的变量的内容将导出到 CSV 文件。

从 Exchange Online 垃圾邮件日志文件中导出所有信息 |过去 30 天

$EndDate = Get-Date

$StartDate = $EndDate.Adddays(-30)

# Define the Date format variable

$Datef = Get-Date -Format "\Da\te dd-MM-yyyy \Ti\me H-mm"

# Define variables that contain the folder names

$A20 = "C:\INFO\Spam mail Reports"

$A21 = "$A20\ALL Spam mail events - Last 30 days - $Datef"

# C:\INFO\Spam mail Reports

IF (!(Test-Path -path $A20))

{ New-Item $A20 -type directory | Out-Null }

# ALL Spam mail events - Last 30 days - <Date>

IF (!(Test-Path -path $A21))

{ New-Item $A21 -type directory | Out-Null }

# Define the variable for the exported Filename

$File1 = "Full spam mail report - All information - last 30 Days"

# Using PowerShell command that will verify that ALL the available information will be exported.

# Get the complete content of the Exchange Online spam mail Log file (all existing spam mail events in the last 30 days)

$SpamMailLogFileContent =

for ($c = 1; $c -lt 1001; $c++)
{

    if ((Get-MailDetailSpamReport -StartDate $StartDate -EndDate $EndDate -PageSize 5000 -Page $c).count -gt 0)
    {

        Get-MailDetailSpamReport -StartDate $StartDate -EndDate $EndDate -PageSize 5000 -Page $c

    }

    else

    { break; }

}

# Define a variable that store the content of the Exchange Online spam log file

$ExportSpamLog = $SpamMailLogFileContent | Sort-Object -Property Date -descending

# Export information to the CSV file

$ExportSpamLog | Export-CSV $A21\"$File1.CSV" -NoTypeInformation -Encoding utf8

统计已发送和已接收的垃圾邮件 |垃圾邮件报告。

在本节中,我想演示如何使用 Get-MailDetailSpamReport PowerShell cmdlet 生成特定类型的垃圾邮件报告,该报告将计算特定垃圾邮件事件与标准垃圾邮件事件的数量Get-MailDetailSpamReport PowerShell cmdlet 的输出,显示有关特定垃圾邮件事件的信息(有关特定垃圾邮件事件的详细信息)。

默认情况下,Get-MailDetailSpamReport PowerShell cmdlet 不“知道”如何对垃圾邮件事件进行计数。在本节中,我们将演示如何使用 PowerShell 脚本来“添加”所需的容量。

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

我们寻求实现的目标是统计以下类型的垃圾邮件事件:

  • 对接收(发送到)特定实体的垃圾邮件数量进行求和(计数)。
  • 对从特定实体发送(发送)的垃圾邮件数量进行求和(计数)。

在我们的场景中,术语“实体”被翻译为“电子邮件地址”。
“电子邮件地址身份”可以是外部收件人(非组织收件人)的电子邮件地址或收件人组织的电子邮件地址。

导出垃圾邮件摘要|统计(总和)所有收到垃圾邮件 |所有电子邮件地址

在本节中,我们回顾如何使用 PowerShell 脚本,该脚本将生成垃圾邮件“摘要报告”,该报告对发送到(接收)出现在 Exchange Online 垃圾邮件日志中的每个电子邮件地址的所有垃圾邮件进行计数文件。

我们寻求实现的目标是统计以下类型的垃圾邮件事件:

  • 特定实体接收(发送至)特定实体的垃圾邮件数量的总和(计数)。

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

在开始描述包含许多不同部分的 PowerShell 脚本之前,我想先介绍一下基本的 PowerShell 命令结构,我们使用该结构来计算接收(发送到)特定电子邮件地址的垃圾邮件事件的数量。

基本的PowerShell命令语法

在提供“复杂”的 PowerShell 语法之前,我们先看一下该场景中需要使用的基本 PowerShell 语法结构:

$ReceiveEvents = Get-MailDetailSpamReport -RecipientAddress <E-mail address> -StartDate <StartDate> -EndDate <EndDate>

$ReceiveEventsCount = @($ReceiveEvents).count

write-host "The Number of Received spam emails is: $ReceiveEventsCount"

在这种情况下,PowerShell 脚本将执行以下任务序列:

任务 1 - 从 Exchange Online 垃圾邮件日志文件中获取有关过去 30 天内发生的所有垃圾邮件事件的信息。

垃圾邮件日志文件的内容是通过我们在 XXX 部分中恢复的 PowerShell 命令获取的。

我们使用变量 - $SpamMailLogFileContent 作为逻辑容器,它将存储垃圾邮件日志文件的内容。

任务 2 - 创建 Exchange Online 垃圾邮件日志文件中出现的所有电子邮件地址的列表。

Exchange Online 垃圾邮件日志文件中显示在“垃圾邮件接收器”列“下方”的所有电子邮件地址列表是使用以下 PowerShell 命令创建的:

$ALLSpamMailRecipients = $SpamMailLogFileContent | Group-Object -Property RecipientAddress | Sort-Object name -descending | Select-Object name,count

我们使用变量 - $ALLSpamMailRecipients 作为逻辑容器,它将存储电子邮件地址列表。

任务 3 - 创建一个文件夹层次结构,用于存储导出的垃圾邮件摘要报告。

任务 4 - 运行循环进程,该进程将获取列表中的“第一个电子邮件地址”,并计算与该电子邮件地址相关的所有垃圾邮件事件。然后循环过程将“继续”到列表中的下一个电子邮件地址,直到到达列表中的最后一个电子邮件地址。

有关每个电子邮件地址的信息以及接收垃圾邮件事件的“总和”将被写入 PowerShell 控制台,并并行导出到报告文件。

循环过程的输出发生在一个专用空间中,该空间被描述为哈希数组。

在我们的场景中,哈希数组名为 $Results=@()

我们在垃圾邮件日志文件中查询每个电子邮件地址的循环过程是通过以下 PowerShell 命令实现的:

$ReceiveEvents = Get-MailDetailSpamReport -RecipientAddress $ID1 -StartDate $StartDate -EndDate $EndDate

我们使用变量 - $ReceiveEvents 作为逻辑容器,它将存储与指定电子邮件地址相关的所有垃圾邮件事件的信息(表示为 $ID1)。

统计垃圾邮件事件

鉴于我们有与特定电子邮件地址(特定收件人)相关的垃圾邮件事件列表,我们可以使用 PowerShell 形式来统计现有垃圾邮件事件的数量。

计数过程是通过以下PowerShell命令实现的:

$ReceiveEventsCount = @($ReceiveEvents).count

我们使用变量 - $ReceiveEventsCount 作为逻辑容器,它将存储特定电子邮件地址的“计数结果”。

任务 5 - 将哈希数组中保存的信息写入 - CSV 文件。

我们用于将垃圾邮件报告导出到 CSV 文件的 PowerShell 命令是:

$ResultsExport | Export-CSV $A22\"$File1.CSV" -NoTypeInformation -Encoding utf8

我们使用变量$ResultsExport作为逻辑容器,它存储循环过程中收集的所有结果。含义是 - “垃圾邮件接收者”的每个电子邮件地址以及发送到该特定电子邮件地址的垃圾邮件(垃圾邮件事件)的总和。

PowerShell 脚本对所有收到的垃圾邮件进行计数(求和)

# Define variables for a start date, and End date

$EndDate = Get-Date

$StartDate = $EndDate.Adddays(-30)

# Define variables that contain the folder names

$A20 = "C:\INFO\Spam mail Reports"

$A21 = "$A20\Summary Spam Report"

$A22 = "$A21. Count RECEIVED spam mail report - Each Unique E-mail Address"

# Create folders Structure that contains the exported information

# C:\INFO\Spam mail Reports

IF (!(Test-Path -path $A20))

{ New-Item $A20 -type directory | Out-Null }

# Summary Spam Report

IF (!(Test-Path -path $A21))

{ New-Item $A21 -type directory | Out-Null }

# 2. Count SENT + RECEIVED spam mail report - each Unique E-mail Address - <Date>

IF (!(Test-Path -path $A22))

{ New-Item $A22 -type directory | Out-Null }

# Get the complete content of the Exchange Online spam mail Log file (all existing spam mail events in the last 30 days)

$SpamMailLogFileContent =

for ($c = 1; $c -lt 1001; $c++)
{

    if ((Get-MailDetailSpamReport -StartDate $StartDate -EndDate $EndDate -PageSize 5000 -Page $c).count -gt 0)
    {

        Get-MailDetailSpamReport -StartDate $StartDate -EndDate $EndDate -PageSize 5000 -Page $c

    }

    else

    { break; }

}

# Create a list of unique E-mail addresses that appear in the spam mail report | Spam Mail receiver (recipients that spam mail sent to them)

$ALLSpamMailRecipients = $SpamMailLogFileContent | Group-Object -Property RecipientAddress | Sort-Object name -descending | Select-Object name, count

# Define an array (hash) that will store information about spam mail events that fetched from the mail spam mail report

$Results = @()

ForEach ($SpamEvent in $ALLSpamMailRecipients)
{

    # Specific Unique identity (E-mail address)

    $ID1 = $SpamEvent.name

    # Display progress bar information on the PowerShell console

    Write-Progress -Activity "Export to Log Files - SPAM mail Report for $ID1 E-mail address - last 30 days"

    # Define a variable that store ALL spam mail events of emails that were received by (sent to) specified recipient

    $ReceiveEvents = Get-MailDetailSpamReport -RecipientAddress $ID1 -StartDate $StartDate -EndDate $EndDate

    # Count the number of - all spam emails events of E-mail that sent to the specified recipient

    $ReceiveEventsCount = @($ReceiveEvents).count

    write-host -ForegroundColor white ----------------------------------------------------------------------------

    write-host -ForegroundColor white "Spam mail information about - Exchange Online User " -NoNewline; Write-Host $ID1 -ForegroundColor white -BackgroundColor DarkGreen

    write-host -ForegroundColor white "(The last 30 days)"

    write-host

    write-host -ForegroundColor white "* Number of RECEIVED spam emails:" $ReceiveEventsCount

    write-host -ForegroundColor white ----------------------------------------------------------------------------

    # Define that array of properties, that appears in the spam mail report

    $Properties = @{

        "E-mail address"     = $ID1

        "Spam mail Received" = $ReceiveEventsCount

    }

    # Define a variable that store the information about each identity

    $Results += New-Object psobject -Property $properties

    $ResultsExport = $Results | Select-Object "E-mail address", "Spam mail Received"

}

# Define the variable for the exported Filename

$File1 = "Received Spam emails per unique E-mail Address"

# Export information about - Sent SPAM mail Report

$ResultsExport | Export-CSV $A22\"$File1.CSV" -NoTypeInformation -Encoding utf8

导出垃圾邮件摘要|对所有已发送垃圾邮件进行计数(总和)|所有电子邮件地址

当前的场景与我们在上一节中回顾的场景类似。

与之前场景的主要区别在于,现在我们的主要关注点是发送垃圾邮件的实体。

我们寻求实现的目标是统计以下类型的垃圾邮件事件:

  • 对从特定实体发送(发送)的垃圾邮件数量进行求和(计数)。

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

基本的PowerShell命令语法

在这种情况下我们需要使用的基本PowerShell语法结构是:

$ReceiveEvents = Get-MailDetailSpamReport -SenderAddress <E-mail address> -

$SENTEventsCount = @($SENTEvents).count

write-host "The Number of Sent spam emails is: $SENTEventsCount"

PowerShell 脚本 - 对所有发送的垃圾邮件进行计数(求和)

# Define variables for a start date, and End date

$EndDate = Get-Date

$StartDate = $EndDate.Adddays(-30)

# Define variables that contain the folder names

$A20 = "C:\INFO\Spam mail Reports"

$A21 = "$A20\Summary Spam Report"

$A22 = "$A21. Count SENT spam mail report - Each Unique E-mail Address"

# Create folders Structure that contains the exported information

# C:\INFO\Spam mail Reports

IF (!(Test-Path -path $A20))

{ New-Item $A20 -type directory | Out-Null }

# Summary Spam Report

IF (!(Test-Path -path $A21))

{ New-Item $A21 -type directory | Out-Null }

# 2. Count SENT + RECEIVED spam mail report - each Unique E-mail Address - <Date>

IF (!(Test-Path -path $A22))

{ New-Item $A22 -type directory | Out-Null }

# Get the complete content of the Exchange Online spam mail Log file (all existing spam mail events in the last 30 days)

$SpamMailLogFileContent =

for ($c = 1; $c -lt 1001; $c++)
{

    if ((Get-MailDetailSpamReport -StartDate $StartDate -EndDate $EndDate -PageSize 5000 -Page $c).count -gt 0)
    {

        Get-MailDetailSpamReport -StartDate $StartDate -EndDate $EndDate -PageSize 5000 -Page $c

    }

    else

    { break; }

}

# Create a list of unique E-mail addresses that appear in the spam mail report | Spam Mail Sender (E-mail address that sent spam mail)

$ALLSpamMailSenders = $SpamMailLogFileContent | Group-Object -Property SenderAddress | Sort-Object name -descending | Select-Object name, count

# efine an array (hash) that will store information about spam mail events that fetched from the mail spam mail report

$Results = @()

ForEach ($SpamEvent in $ALLSpamMailSenders)
{

    # Specific Unique identity (E-mail address)

    $ID1 = $SpamEvent.name

    # Display progress bar information on the PowerShell console

    Write-Progress -Activity "Export to Log Files - SPAM mail Report for $ID1 E-mail address - last 30 days"

    # Define a variable that store ALL spam mail events of emails that where SENT by specified recipient

    $SENTEvents = Get-MailDetailSpamReport -SenderAddress $ID1 -StartDate $StartDate -EndDate $EndDate

    # Count the number of - Spam emails that was sent by (Sent from) a specific E-mail Address

    $SENTEventsCount = @($SENTEvents).count

    write-host -ForegroundColor white ----------------------------------------------------------------------------

    write-host -ForegroundColor white "Spam mail information about - Exchange Online User " -NoNewline; Write-Host $ID1 -ForegroundColor white  -BackgroundColor DarkGreen

    write-host -ForegroundColor white "(The last 30 days)"

    write-host

    write-host -ForegroundColor white "* Number of SENT spam emails is: " -NoNewline; Write-Host $SENTEventsCount -ForegroundColor white  -BackgroundColor Darkred

    write-host -ForegroundColor white ----------------------------------------------------------------------------

    # Define that array of -properties, that appears in the spam mail report

    $Properties = @{

        "E-mail address" = $ID1

        "Spam mail SENT" = $SENTEventsCount

    }

    # Define a variable that store the information about each identity

    $Results += New-Object psobject -Property $properties

    $ResultsExport = $Results | Select-Object "E-mail address", "Spam mail SENT"

}

# Define the variable for the exported Filename

$File1 = "SENT Spam emails per unique E-mail Address"

# Export information about - Sent SPAM mail Report

$ResultsExport | Export-CSV $A22\"$File1.CSV" -NoTypeInformation -Encoding utf8

为每个 Exchange 邮箱用户生成专用垃圾邮件报告(批量模式)

在本节中,我们使用一种方法为每个 Exchange Online 用户电子邮件地址生成“专用垃圾邮件报告”,该报告显示在 Exchange Online 垃圾邮件日志文件中。

我使用术语“专用”来描述 PowerShell 为每个 Exchange Online 用户电子邮件地址自动创建专用文件夹的过程。

该文件夹将用作存储“专用垃圾邮件报告”的容器,其中包含与指定电子邮件地址相关的所有垃圾邮件事件的信息。

[玩转系统] 使用 Get-MailDetailSpamReport PowerShell cmdlet |查看和导出垃圾邮件报告 |第 2 部分#3

我们将回顾此场景的两种变体。

场景 1 - 在此场景中,我们为每个 Exchange Online 用户电子邮件地址创建专用的垃圾邮件报告,该报告显示为“接收”垃圾邮件的电子邮件地址。

在这种情况下,我们的目的是分析敌对分子攻击我们组织收件人的垃圾邮件事件。

方案 2 - 在此方案中,我们为每个 Exchange Online 用户电子邮件地址创建专用的垃圾邮件报告,该报告显示为 - “发送”垃圾邮件的电子邮件地址。

在本例中,我们的目的是分析垃圾邮件事件,其中我们的组织用户之一正在分发(有意或无意)垃圾邮件。

导出专用垃圾邮件事件报告| 已收到垃圾邮件 |对于每个 Exchange 邮箱用户收件人

在当前情况下,我们希望获得有关垃圾邮件事件的详细信息,在这些事件中,我们有机锡用户正受到向他们发送垃圾邮件的敌对分子的攻击。

PowerShell 脚本将执行以下任务序列:

任务 1 - 从 Exchange Online 垃圾邮件日志文件中获取有关过去 30 天内发生的所有垃圾邮件事件的信息。

任务 2 - 创建描述为“Exchange Online 用户”(拥有邮箱的用户)的 Exchange Online 收件人的所有电子邮件地址的列表。

将使用以下 PowerShell 命令创建 Exchange Online 用户收件人的“列表”:

$GetMBXUser = Get-MailBox -Filter '(RecipientTypeDetails -eq "UserMailbox")' | Where-Object {$_.name -notlike '*DiscoverySearchMailbox*'} | Sort-Object -Property Displayname

我们使用变量 $GetMBXUser 作为逻辑容器,它将存储 Exchange Online 用户电子邮件地址列表。

任务 3 - 扫描从 Exchange Online 垃圾邮件日志文件中提取的信息,并执行循环过程。

PowerShell 脚本将获取列表中的“第一个电子邮件地址”,并检查垃圾邮件日志中是否有任何有关垃圾邮件事件的信息,其中垃圾邮件被发送到特定的电子邮件地址。

如果没有与特定电子邮件地址相关的信息,PowerShell脚本将继续对列表中的“下一个电子邮件地址”执行相同的过程。

如果 PowerShell 脚本找到有关发送到特定电子邮件地址的垃圾邮件的信息,PowerShell 脚本将执行以下操作:

3.1 - 创建专用文件夹,并使用 Exchange Online 用户收件人的显示名称命名该文件夹。

3.2 - 生成垃圾邮件事件日志,其中包括将垃圾邮件发送到指定电子邮件地址的所有垃圾邮件事件(描述为“收到的垃圾邮件”)。

PowerShell 脚本 - 导出专用垃圾邮件事件报告 |收到垃圾邮件

# Define variables for a start date and End date

$EndDate = Get-Date

$StartDate = $EndDate.Adddays(-30)

# All Exchange Online USER Mailboxes

$GetMBXUser = Get-MailBox -Filter '(RecipientTypeDetails -eq "UserMailbox")' | Where-Object { $_.name -notlike '*DiscoverySearchMailbox*' } | Sort-Object -Property Displayname

# Define variables that contain the folder names

$A30 = "$A10\C. Dedicated Spam Mail Report For Each E-mail Address"

$A31 = "$A30. Exchange Online users"

$A32 = "$A31.1 - RECEIVED Spam mail report"

# Create folders Structure that contains the exported information

# C:\INFO\Spam mail Reports

# C. Dedicated Spam Mail Report For Each E-mail Address

IF (!(Test-Path -path $A30))

{ New-Item $A30 -type directory | Out-Null }

# 1. Exchange Online users

IF (!(Test-Path -path $A31))

{ New-Item $A31 -type directory | Out-Null }

# 1.1 - RECEIVED Spam mail report

IF (!(Test-Path -path $A32))

{ New-Item $A32 -type directory | Out-Null }

# Loop via the list of existing Exchange Online mailbox users | locate information about the spam mail event for each E-mail address

ForEach ($Mailbox in $GetMBXUser)
{

    # Specific Exchange Online mailbox identity | Specific member from mailboxes Array

    $ID1 = $Mailbox.Displayname

    $ID2 = $Mailbox.PrimarySmtpAddress

    #  Define variable - Spam report for a specified Exchange Online user

    # Define a variable that store all spam mail event of E-mail that sent to the specified recipient

    $ReceiveEvents = Get-MailDetailSpamReport -RecipientAddress $ID2 -StartDate $StartDate -EndDate $EndDate

    # Count the number of - all spam mail event of E-mail that sent to the specified recipient

    $ReceiveEventsCount = @($ReceiveEvents).count

    # Verify if the specified Exchange Online user RECEIVE any spam mail

    IF (!$ReceiveEventsCount)
    {

    }

    Else
    {

        # Define variables that contain the folders name for the dedicated Exchange Online user spam report folder

        $A40 = "$A32$ID1"

        # Dynamically create a dedicated folder that will store spam mail report

        # <User name>

        IF (!(Test-Path -path $A40))
        {

            New-Item $A40 -type directory | Out-Null

        }

        # Define the variable for the exported Filename

        $File1 = "Spam emails RECEIVED"

        # Export information about - Received Spam emails by specific Exchange Online recipient

        $ReceiveEvents | Export-CSV $A40\"$File1.CSV" -NoTypeInformation -Encoding utf8

    }

}

导出垃圾邮件事件邮件报告| 已发送垃圾邮件 |对于每个 Exchange 邮箱用户收件人

当前的场景与我们在上一节中回顾的场景类似。

与之前场景的主要区别在于,现在我们的主要关注点是发送垃圾邮件的“实体”。

在这种情况下,我们希望为每个 Exchange Online 用户电子邮件地址创建专用的垃圾邮件报告,该地址在 Exchange Online 垃圾邮件日志文件中显示为 - 发送垃圾邮件的电子邮件地址。

PowerShell 脚本 - 导出专用垃圾邮件事件报告 |已发送垃圾邮件

# Define variables for a start date and End date

$EndDate = Get-Date

$StartDate = $EndDate.Adddays(-30)

# All Exchange Online USER Mailboxes

$GetMBXUser = Get-MailBox -Filter '(RecipientTypeDetails -eq "UserMailbox")' | Where-Object { $_.name -notlike '*DiscoverySearchMailbox*' } | Sort-Object -Property Displayname

# Define variables that contain the folders names

$A30 = "$A10\C. Dedicated Spam mail Report For Each E-mail Address"

$A31 = "$A30. Exchange Online users"

$A33 = "$A31.2 - SENT Spam mail report"

# Create folders Structure that contains the exported information

# C:\INFO\Spam mail Reports

# C. Dedicated Spam Mail Report For Each E-mail Address

IF (!(Test-Path -path $A30))

{ New-Item $A30 -type directory | Out-Null }

# 1. Exchange Online users

IF (!(Test-Path -path $A31))

{ New-Item $A31 -type directory | Out-Null }

# 1.2 - SENT Spam mail report

IF (!(Test-Path -path $A33))

{ New-Item $A33 -type directory | Out-Null }

# Loop via the list of existing Exchange Online mailbox users | locate information about spam mail event for each E-mail address

ForEach ($Mailbox in $GetMBXUser)
{

    # Specific Exchange Online mailbox identity | Specific member from mailboxes Array

    $ID1 = $Mailbox.Displayname

    $ID2 = $Mailbox.PrimarySmtpAddress

    #----------------------------------------------------------------------------------

    #  Define variable - Spam report for a specified Exchange Online user

    #----------------------------------------------------------------------------------

    # Display progress bar information on the PowerShell console

    Write-Progress -Activity "Export to Log Files - SPAM mail Report for $ID1 E-mail address - last 30 days"

    # Define a variable that store ALL spam mail events of emails that where SENT by specified recipient

    $SENTEvents = Get-MailDetailSpamReport -SenderAddress $ID2 -StartDate $StartDate -EndDate $EndDate

    # Count the number of - Spam emails that was sent by (Sent from) a specific E-mail Address

    $SENTEventsCount = @($SENTEvents).count

    # Verify if the specified Exchange Online user SENT any spam mail

    IF (!$SENTEventsCount)
    {

    }

    Else
    {

        # Define variables that contain the folders name for the dedicated Exchange Online user spam report folder

        $A40 = "$A33$ID1"


        # Dynamically create a dedicated folder that will store spam mail report


        # <User name>

        IF (!(Test-Path -path $A40))
        {

            New-Item $A40 -type directory | Out-Null

        }

        # Define the variable for the exported Filename

        $File1 = "Spam emails SENT"

        # Export information about - SENT Spam emails by specific Exchange Online recipient

        $SENTEvents | Export-CSV $A40\"$File1.CSV" -NoTypeInformation -Encoding utf8

    }

}

导出垃圾邮件摘要|计数(总和)已发送垃圾邮件 |所有电子邮件地址 |仅当发送的项目值大于 X 时

在当前场景中,我们希望统计“垃圾邮件发件人”(从特定电子邮件地址发送的垃圾邮件)的垃圾邮件事件。

与之前统计特定E-mail地址发送的垃圾邮件的场景不同的是,这次我们只想在发送的垃圾邮件数量超过一定阈值时查看信息。

在我们的具体示例中,我们将数字“10”定义为阈值。

基本的PowerShell命令语法

在这种情况下我们需要使用的基本PowerShell语法结构是:

$ReceiveEvents = Get-MailDetailSpamReport -SenderAddress <E-mail address> -

$SENTEvents = Get-MailDetailSpamReport -SenderAddress [email protected] -StartDate $StartDate -EndDate $EndDate

$SENTEventsCount = @($SENTEvents).count

IF ($SENTEventsCount -lt 10)
{

    write-host "The Number of Sent spam emails is: $SENTEventsCount"

    write-host “This is less than required number ? “

}

Else
{

    write-host "The Number of Sent spam emails is: $SENTEventsCount"

    write-host “This is a dangerous sender !!!!”

}

PowerShell 脚本如下所示:

# Define variables for start date and End date

$EndDate = Get-Date

$StartDate = $EndDate.Adddays(-30)

$ThresHold = Read-Host "Type the Number of threshold "

$A20 = "C:\INFO\Spam mail Reports - $Datef"

$A21 = "$A20\C. E-mail addreses that Pass a certain spam mail threshold"

$A22 = "$A21.1 RECEIVED spam mail - ThresHold"

$A23 = "$A21.2 SENT spam mail - ThresHold"

#  Create folders Structure that contains the exported information to TXT, CSV and HTML files

#---------------------------------------------------------------------------------------------------

# C:\INFO\Spam mail Reports - <Date>

IF (!(Test-Path -path $A20))

{ New-Item $A20 -type directory | Out-Null }

# C. E-mail addresses that Pass a certain spam mail threshold

IF (!(Test-Path -path $A21))

{ New-Item $A21 -type directory | Out-Null }

# 1.1 RECEIVED spam mail - ThresHold

IF (!(Test-Path -path $A22))

{ New-Item $A22 -type directory | Out-Null }

# 1.2 SENT spam mail - ThresHold

IF (!(Test-Path -path $A23))

{ New-Item $A23 -type directory | Out-Null }

$SpamMailLogFileContent =

for ($c = 1; $c -lt 1001; $c++)
{

    if ((Get-MailDetailSpamReport -StartDate $global:StartDate -EndDate $global:EndDate -PageSize 5000 -Page $c).count -gt 0)
    {

        Get-MailDetailSpamReport -StartDate $global:StartDate -EndDate $global:EndDate -PageSize 5000 -Page $c

    }

    else

    { break; }

}

# Define variables that will store the list of Exchange Online users (mailbox users) + all E-mail addresses that appear in the spam mail log file

# Create a list of unique E-mail address that appear in the spam mail report | Spam Mail Sender (E-mail address that sent spam mail)

$ALLSpamMailSenders = $SpamMailLogFileContent | Group-Object -Property SenderAddress | Sort-Object name -descending | Select-Object name, count

# Define an array (hash) that will store information about spam mail events that fetched from the mail spam mail report

$Results = @()

# Loop via the list of All existing E-mail addresses | locate information about spam mail event for each E-mail address

ForEach ($SpamEvent in $ALLSpamMailSenders)
{

    # Specific Unique identity (E-mail address)

    $ID1 = $SpamEvent.name

    #  Define variable - Spam report for a specified Exchange Online user

    # Define a variable that store all spam mail event of E-mail that sent to the specified recipient

    $SENTEvents = $LogFileContent | Where-Object { $_.SenderAddress -eq "$ID1" }

    # Count the number of - Spam emails that was sent by (Sent from) a specific E-mail Address

    $SENTEventsCount = @($SENTEvents).count

    # Verify if the specific E-mail address passes the specified spam mail threshold

    IF ($SENTEventsCount -lt $ThresHold)
    {

    }

    Else
    {

        write-host "The number of spam emails that was sent by : " -nonewline; write-host $ID1 -ForegroundColor Yellow -BackgroundColor blue -nonewline; write-host

        write-host "is - " -ForegroundColor white -nonewline; write-host $SENTEventsCount -ForegroundColor white -BackgroundColor Darkred -nonewline; write-host " spam emails" -ForegroundColor white

        # Define that array of -properties, that appears in the spam mail report

        $Properties = @{

            "E-mail address" = $ID1

            "Spam mail Sent" = $SENTEventsCount

        }

        # Define a variable that store the information about each identity

        $Results += New-Object psobject -Property $properties

        $ResultsExport = $Results | Select-Object "E-mail address", "Spam mail Sent"

    }

    # Define the variable for the exported Filename

    $File1 = "Recipients that Send more then $ThresHold spam emails"

    # Export information to the following file formats:  TXT + CSV + HTML files

    $ResultsExport | Export-CSV $A23\"$File1.CSV" -NoTypeInformation -Encoding utf8

}

在下一篇文章中,我们将研究如何使用垃圾邮件报告PowerShell脚本。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯