[玩转系统] 如何使用 PowerShell 搜索电子邮件
作者:精品下载站 日期:2024-12-14 03:37:56 浏览:14 分类:玩电脑
如何使用 PowerShell 搜索电子邮件
在 Exchange Online 邮箱中搜索邮件的一个绝佳方法是使用 PowerShell。您可以根据自己的喜好调整命令并获得所需的结果。此外,将结果导出到 CSV 文件也很棒,因此您可以在其中进行过滤。在本文中,您将学习如何使用 PowerShell 搜索电子邮件。
安装 Microsoft Graph PowerShell
以管理员身份运行 Windows PowerShell 并安装 Microsoft Graph PowerShell。
Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force
重要提示:始终安装 Microsoft Graph PowerShell 和 Microsoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。
在 Microsoft Entra 中配置应用程序注册
在运行命令或脚本之前,必须首先使用正确的权限和客户端密钥创建应用注册,以便使用 Microsoft Graph PowerShell 进行身份验证。
1. 注册新应用
在 Microsoft Entra ID 中注册新应用程序:
- 登录 Microsoft Entra 管理中心
- 单击身份>应用程序>应用程序注册
- 点击新注册
- 将应用程序命名为Search-Mail
- 选择仅此组织目录中的帐户(仅限 EXOIP - 单一租户)
- 点击注册
应用程序Search-Mail 已成功创建。复制以下值并将其粘贴到记事本中,因为稍后连接到 Microsoft Graph PowerShell 时需要它们。
- 复制应用程序(客户端)ID
- 复制目录(租户)ID
2.分配API权限
为您在上一步中注册的应用程序分配 API 权限:
- 点击API权限
- 点击添加权限
- 点击Microsoft API
- 单击Microsoft Graph
- 点击应用程序权限
- 搜索User.Read.All
- 选择用户 > User.Read.All
- 搜索Mail.Read
- 选择邮件 > 邮件.阅读
- 点击添加权限
API权限添加成功。下一步是授予管理员同意。
3. 授予管理员同意
您必须向管理员授予您在上一步中选择的权限的同意:
- 点击授予 EXOIP 管理员同意
- 单击是
- 绿色复选标记表明您已成功授予管理员同意
4. 创建客户端密钥
在 Microsoft Entra 中注册新应用程序、分配 API 权限并授予管理员同意后,您需要创建客户端密钥。
要在 Microsoft Entra ID 中为您的应用程序创建客户端密钥,请按照下列步骤操作:
- 点击证书和机密
- 单击客户端密钥 > 新客户端密钥
- 输入描述
- 选择到期日期
- 点击添加
注意:客户端密钥的有效期最长为 24 个月(2 年)。您始终可以使用 PowerShell 更新 Microsoft Entra ID 中的客户端密钥。
- 复制客户端密钥值并将其保存在记事本中
使用客户端密钥连接到 Microsoft Graph PowerShell
您需要更改以下参数值才能使用客户端密钥连接到 MS Graph PowerShell:
- 在第 2 行中键入应用程序客户端 ID 值
- 在第 3 行中键入目录租户 ID 值
- 在第 4 行中输入客户端密钥值值
运行以下 PowerShell 脚本。
# Configuration
$ClientId = "c6787ff5-08ec-4e96-b4d2-1d1782303310"
$TenantId = "eb403171-a4ec-4d98-a08f-1876318c9deb"
$ClientSecret = "6_q8Q~X9e-fjwWhVVP_WWx~ua4JMI.BWkI7Q7b7B"
# Convert the client secret to a secure string
$ClientSecretPass = ConvertTo-SecureString -String $ClientSecret -AsPlainText -Force
# Create a credential object using the client ID and secure string
$ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ClientId, $ClientSecretPass
# Connect to Microsoft Graph with Client Secret
Connect-MgGraph -TenantId $tenantId -ClientSecretCredential $ClientSecretCredential
搜索特定用户的电子邮件
获取来自特定用户的所有消息。
$User = "[email protected]"
Get-MgUserMessage -All -UserId "$User" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridView
获取特定用户已阅读的所有消息。
$User = "[email protected]"
Get-MgUserMessage -All -UserId "$User" -Filter "IsRead eq true" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridView
获取来自特定用户的所有未读消息。
$User = "[email protected]"
Get-MgUserMessage -All -UserId "$User" -Filter "IsRead eq false" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridView
按日期搜索电子邮件
搜索特定日期之后来自特定用户的电子邮件。
$User = "[email protected]"
$Date = "2024-01-01"
Get-MgUserMessage -All -UserId "$User" -Filter "ReceivedDateTime gt $Date" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridView
搜索有关主题的电子邮件
搜索以特定主题开头的电子邮件。
$Subject = "The subject"
$user = "[email protected]"
Get-MgUserMessage -All -UserId "$User" -Filter "startswith(Subject,'$Subject')" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridView
搜索与主题相同的电子邮件。
$Subject = "The specific subject"
$user = "[email protected]"
Get-MgUserMessage -All -UserId "$user" -Filter "Subject eq '$Subject'" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridView
搜索过去 30 天的电子邮件
让我们搜索过去 30 天内组织中收件人和发件人的所有电子邮件。结果将显示在 Out-GridView 中并导出到 CSV 文件。
搜索过去 30 天内组织中的所有电子邮件。
# Get all users
$users = Get-MgUser -All
# Create an empty array to store the data
$Data = @()
# Determine the date 30 days prior to today
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-dd")
# Loop through each user
foreach ($user in $users) {
# Ensure the user has an associated email address
if ($user.Mail) {
$messages = Get-MgUserMessage -UserId $user.Id -All -Filter "ReceivedDateTime ge $startDate" -ErrorAction SilentlyContinue
foreach ($message in $messages) {
$Data += [PSCustomObject]@{
ReceivedDateTime = $message.ReceivedDateTime
Subject = $message.Subject
Sender = $message.Sender.EmailAddress.Address -join ','
Recipient = $message.ToRecipients.EmailAddress.Address -join ','
InternetMessageId = $message.InternetMessageId
}
}
}
}
# Display and export the data
$Data | Out-GridView
$Data | Export-Csv -Path "C:\temp\All_emails.csv" -NoTypeInformation -Encoding utf8
搜索组织中过去 30 天内发送给特定收件人的所有电子邮件。
# Email address of the recipient you want to filter
$recipientEmailAddress = "[email protected]"
# Get all users
$users = Get-MgUser -All
# Create an empty array to store the data
$Data = @()
# Determine the date 30 days prior to today
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-dd")
# Loop through each user
foreach ($user in $users) {
# Ensure the user has an associated email address
if ($user.Mail) {
$messages = Get-MgUserMessage -UserId $user.Id -All -Filter "ReceivedDateTime ge $startDate" -ErrorAction SilentlyContinue
foreach ($message in $messages) {
# Check if the recipient matches the desired email address
if ($message.ToRecipients.EmailAddress.Address -eq $recipientEmailAddress) {
$Data += [PSCustomObject]@{
ReceivedDateTime = $message.ReceivedDateTime
Subject = $message.Subject
Sender = $message.Sender.EmailAddress.Address
Recipient = $message.ToRecipients.EmailAddress.Address
InternetMessageId = $message.InternetMessageId
}
}
}
}
}
# Display and export the data
$Data | Out-GridView
$Data | Export-Csv -Path "C:\temp\Recipient_emails.csv" -NoTypeInformation -Encoding utf8
搜索组织中过去 30 天内由特定发件人发送的所有电子邮件。
# Email address of the sender you want to filter
$senderEmailAddress = "[email protected]"
# Get all users
$users = Get-MgUser -All
# Create an empty array to store the data
$Data = @()
# Determine the date 30 days prior to today
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-dd")
# Loop through each user
foreach ($user in $users) {
# Ensure the user has an associated email address
if ($user.Mail) {
$messages = Get-MgUserMessage -UserId $user.Id -All -Filter "ReceivedDateTime ge $startDate" -ErrorAction SilentlyContinue
foreach ($message in $messages) {
if ($message.Sender.EmailAddress.Address -eq $senderEmailAddress) {
$Data += [PSCustomObject]@{
ReceivedDateTime = $message.ReceivedDateTime
Subject = $message.Subject
Sender = $message.Sender.EmailAddress.Address
Recipient = ($message.ToRecipients.EmailAddress.Address -join ',')
InternetMessageId = $message.InternetMessageId
}
}
}
}
}
# Display and export the data
$Data | Out-GridView
$Data | Export-Csv -Path "C:\temp\Sender_emails.csv" -NoTypeInformation -Encoding utf8
就是这样!
了解更多:导出 Entra ID 应用程序注册证书和机密到期报告 »
结论
您学习了如何使用 PowerShell 搜索电子邮件。首先,在 Microsoft Entra 中设置具有正确权限的应用程序。之后,使用 Microsoft Graph PowerShell 连接到应用程序。最后,运行命令以使用 PowerShell 搜索电子邮件。
您喜欢这篇文章吗?您可能还喜欢如何在 Microsoft 365 中阻止顶级域 (TLD)。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag