[玩转系统] 如何使用 PowerShell 获取邮箱权限 - inc.完整报告
作者:精品下载站 日期:2024-12-14 03:41:28 浏览:12 分类:玩电脑
如何使用 PowerShell 获取邮箱权限 - inc.完整报告
如何跟踪 Exchange Online 中的邮箱权限?添加新权限既简单又快捷,但删除过时的权限却常常被忘记。为了进行跟踪,我们希望获得包含所有邮箱文件夹权限的报告。
邮箱权限可以由 Exchange 管理员或用户自己设置。这使得管理和跟踪权限成为一个真正的挑战。我们需要收集来自不同地方的权限,并将它们组合成一个清晰的概览。
借助PowerShell,我们可以轻松获取每个用户的邮箱文件夹权限,并从中生成漂亮的CSV报告。
下面的 PowerShell 脚本将以下数据从用户和共享邮箱导出到 CSV 文件:
- 显示名称
- 主要电子邮件地址
- 邮箱类型
- 读取和管理权限
- 作为权限发送
- 代发
- 收件箱文件夹权限(显示名称和权限级别)
- 日历权限(显示名称和权限级别)
与往常一样,我将首先向您解释脚本和不同的选项。这样您就了解了脚本的工作原理。在本文末尾,您将在我的 GitHub 存储库中找到该脚本的链接。
邮箱权限
在我们开始报告脚本之前,我首先想向您解释所有不同的权限从何而来。因为要获得所有邮箱权限的完整概述,我们需要从 4 个不同的位置获取权限。
我们想要列出以下权限:
- 读取和管理权限
- 作为权限发送
- 代表权限发送
- 收件箱文件夹权限
- 日历文件夹权限
前 3 个权限可以通过 Microsoft 365 管理中心轻松设置和查看,但对于文件夹权限,您需要使用 PowerShell 中的获取邮箱文件夹权限 cmdlet 。
读取和管理权限
读取和管理基本上是完全访问权限。要查看有权访问邮箱的所有用户,我们需要使用 cmdlet Get-EXOMailboxPermissions
。我们希望确保不列出默认系统权限,因此我们将其过滤掉。
Get-EXOMailboxPermission -Identity <userprincipalname>| where { -not ($_.User -match "NT AUTHORITY") -and ($_.IsInherited -eq $false)} | ft
作为权限发送
要在 PowerShell 中查看发送权限,我们需要使用 Get-EXORecipientPermissions cmdlet。同样在这里我们要过滤掉默认权限。
Get-EXORecipientPermission -Identity <userprincipalname> | where { -not ($_.Trustee -match "NT AUTHORITY") -and ($_.IsInherited -eq $false)} | ft
代表发送权限
代表发送权限是 Get-EXOMailbox
cmdlet 的一部分。您必须指定属性 GrantSendOnBehalfTo
才能查看权限
Get-EXOMailbox -Identity <userprincipalname> -Properties GrantSendOnBehalfTo | select UserPrincipalName, DisplayName, PrimarySMTPAddress, GrantSendOnBehalfTo, ForwardingSMTPAddress | ft
获取邮箱文件夹权限
我们要在报告中列出的最后一个权限是文件夹权限。这些有点棘手,因为我们需要放弃正确的文件夹名称。对于大多数人来说,这些将是默认的英文文件夹名称,例如收件箱和日历。
但在荷兰语中,例如“Postvak in”和“Agenda”。如果您不确定租户中的正确文件夹名称是什么,请列出单个邮箱的所有文件夹:
Get-EXOMailboxFolderStatistics -identity <userprincipalname> | ft
如果您找到了租户的正确文件夹名称,我们可以使用以下 PowerShell cmdlet 来获取邮箱文件夹权限
$identity = <userprincipalname>
$folder = Inbox # Or Calendar for example
Get-EXOMailboxFolderPermission -Identity "$($identity):$($folder)" | where { -not ($_.User -match "Default") -and -not ($_.AccessRights -match "None")}
正如您所看到的,要获得所有邮箱权限,我们需要查看不同的位置并合并所有数据。
如何使用邮箱权限脚本
获取较大租户中的所有邮箱权限可能需要一些时间,因为我们需要从 4 个不同的位置获取权限。因此,我在脚本中添加了几个参数,以便您可以根据需要微调报告。
要运行该脚本,您始终需要使用 -adminUPN
参数输入您的电子邮件地址进行身份验证。如果没有任何其他参数,该脚本将生成一个报告:
- 所有邮箱(共享和用户)
- 包括收件箱和日历文件夹权限
- 获取权限的显示名称
- 将报告存储在脚本根位置 (.\mailboxsizereport-nov-30-2021.csv)
.\MailboxPermissionReport.ps1 -adminUPN [email protected]
正如您所看到的,脚本将根据其处理的邮箱数量显示一个进度条。
获取共享邮箱权限
对于共享邮箱,有多种选择。默认情况下,它们包含在报告中。但您也可以仅获取共享邮箱权限,或使用参数 -sharedMailboxes
将其排除在 PowerShell 报告之外:
# Get only the shared mailboxes
.\MailboxPermissionReport.ps1 -adminUPN [email protected] -sharedMailboxes only
# Get only the user mailboxes
.\MailboxPermissionReport.ps1 -adminUPN [email protected] -sharedMailboxes no
# (Default) Get user and shared mailboxes
.\MailboxPermissionReport.ps1 -adminUPN [email protected] -sharedMailboxes include
邮箱文件夹权限
默认情况下,该脚本将列出所有邮箱文件夹权限。例如,这些权限仅适用于日历或收件箱文件夹。尤其是日历是一个邮箱文件夹,应用了很多权限。
但如前所述,每个请求都需要额外的时间。因此,如果对邮箱文件夹权限不感兴趣,您可以使用参数 -folderPermissions
忽略它们:
.\MailboxPermissionReport.ps1 -adminUPN [email protected] -folderPermissions:$false
虽然这些列仍然在 Excel 中列出,但它们都是空的。
获取单个或选择的用户的权限
您还可以仅请求单个邮箱或选定邮箱的邮箱权限。这允许您仅检查管理邮箱的邮箱权限,例如:
.\MailboxPermissionReport.ps1 -adminUPN [email protected] -UserPrincipalName [email protected],[email protected],[email protected]
您可以对 UserPrincipalName 参数使用以下任意选项,只要该值唯一标识邮箱即可:
- 用户 ID 或用户主体名称 (UPN)
- 姓名
- 别名
- 专有名称 (DN)
- 域名\用户名
- 电子邮件
与用户一起使用 CSV 文件
我添加了与您想要获取邮箱权限的用户一起使用 CSV 文件的选项。通过这种方式,您可以轻松地仅从选定的用户处获取权限,而无需在命令行中键入所有用户的权限。
.\MailboxPermissionReport.ps1 -adminUPN [email protected] -csvFile "c:\temp\csvfile.csv"
确保 CSV 文件以逗号分隔、有标题并包含字段 UserPrincipalName。例如:
UserPrincipalName,Display Name
[email protected],Adele Vance
[email protected],Grady Archie
[email protected],Joni Sherman
是否显示显示名称
我们在脚本中使用的不同邮箱权限 cmdlet 都会返回具有权限的用户的用户主体名称。例如,如果我们查找“发送为”权限,则必须发送为权限的用户将列在“受托者”列中。
在报告中将所有用户列为 [email protected] 并不能真正使其具有可读性。因此,默认情况下,我们将查找每个用户的显示名称。但这需要一些额外的时间。
如果您想要针对大量用户运行报告,您可能需要禁用此功能。然后,脚本执行的操作是从用户名中删除该域部分,因此您在报告中仅获得 GradyA。
.\MailboxPermissionReport.ps1 -adminUPN [email protected] -displayNames:$false
CSV 导出路径
您还可以使用 -CSVpath
参数指定输出文件的位置和文件名。默认情况下,脚本会将结果导出到控制台。但如果您指定 CSV 路径,则脚本会创建 CSV 导出。
.\MailboxPermissionReport.ps1 -adminUPN [email protected] -CSVpath c:\temp\permission.csv
PowerShell 获取邮箱权限脚本
要使用 PowerShell 获取邮箱权限,我们需要使用 Exchange Online 模块。我们将使用 Exchange Online v2 模块,因为它支持 MFA 和 SSON。这样,您只需输入您的电子邮件地址即可连接到 Exchange Online。
该脚本将检查该模块是否已安装,并在您的计算机上不可用时为您提供安装该模块的选项。我们还会检查任何现有连接,以防止来自同一台计算机的多个连接。
获取邮箱
所以脚本的第一部分是获取邮箱。如果设置了参数-userPrincipalName
,那么我们将获取列出的每个邮箱并将它们添加到数组中。否则,我们将获取所有邮箱,包括共享或非共享邮箱,具体取决于 -sharedMailboxes
参数。
# Check which mailboxes to get
if ($UserPrincipalName) {
Write-Host "Collecting mailboxes" -ForegroundColor Cyan
$mailboxes = @()
# Get the requested mailboxes
foreach ($user in $UserPrincipalName) {
Write-Host "- Get mailbox $user" -ForegroundColor Cyan
$mailboxes += Get-SingleUser -identity $user
}
}else{
Write-Host "Collecting mailboxes" -ForegroundColor Cyan
$mailboxes = Get-Mailboxes
}
# Function Get-SingleUser gets mailbox based on identity
Get-EXOMailbox -Identity $identity -Properties GrantSendOnBehalfTo, ForwardingSMTPAddress |
select UserPrincipalName, DisplayName, PrimarySMTPAddress, RecipientType, RecipientTypeDetails, GrantSendOnBehalfTo, ForwardingSMTPAddress
# Function Get-Mailboxes gets all mailboxes based on shared mailbox parameter
switch ($sharedMailboxes)
{
"include" {$mailboxTypes = "UserMailbox,SharedMailbox"}
"only" {$mailboxTypes = "SharedMailbox"}
"no" {$mailboxTypes = "UserMailbox"}
}
Get-EXOMailbox -ResultSize unlimited -RecipientTypeDetails $mailboxTypes -Properties GrantSendOnBehalfTo, ForwardingSMTPAddress|
select UserPrincipalName, DisplayName, PrimarySMTPAddress, RecipientType, RecipientTypeDetails, GrantSendOnBehalfTo, ForwardingSMTPAddress
收集权限
下一步是获取每个邮箱的权限。每个函数都会使用开头描述的函数查找权限,并返回一个包含用户的数组
# Collect permissions for each mailbox
$mailboxes | ForEach {
# Get Send on Behalf Permissions
$sendOnbehalfUsers = Get-SendOnBehalf -mailbox $_
# Get Fullaccess Permissions
$fullAccessUsers = Get-FullAccessPermissions -identity $_.UserPrincipalName
# Get Send as Permissions
$sendAsUsers = Get-SendAsPermissions -identity $_.UserPrincipalName
}
根据参数folderPermissions,我们还将获得邮箱文件夹权限:
if ($folderPermissions.IsPresent) {
# Get Inbox folder permission
$inboxFolder = Get-FolderPermissions -identity $_.UserPrincipalName -folder $inboxFolderName
$ib = $inboxFolder.users.Count
# Get Calendar permissions
$calendarFolder = Get-FolderPermissions -identity $_.UserPrincipalName -folder $calendarFolderName
$ca = $calendarFolder.users.Count
}
对于文件夹权限,我们需要知道正确的文件夹名称。在脚本的开头,我们设置了 2 个具有正确名称的变量:
$inboxFolderName = "inbox" # Default "inbox"
$calendarFolderName = "calendar" # Default "calendar"
创建 CSV 导出
对于 CSV 导出,我想在自己的行中列出有权访问邮箱的每个用户,并使用正确的缩进。因此,要做到这一点,我们需要知道要添加多少个子行以及哪个具有权限的数组最长。
因此,我们对每个数组进行计数,然后使用简单的 if-else 函数来找出哪个数组更大。
# Count number or records
$sob = $sendOnbehalfUsers.Count
$fa = $fullAccessUsers.Count
$sa = $sendAsUsers.Count
# $ib and $ca are set in the folder permissions
$mostRecords = Find-LargestValue -sob $sob -fa $fa -sa $sa -ib $ib -ca $ca
# Find Largest Values function:
Function Find-LargestValue {
<#
.SYNOPSIS
Find the value with the most records
#>
param(
[Parameter(Mandatory = $true)]$sob,
[Parameter(Mandatory = $true)]$fa,
[Parameter(Mandatory = $true)]$sa,
[Parameter(Mandatory = $true)]$ib,
[Parameter(Mandatory = $true)]$ca
)
if ($sob -gt $fa -and $sob -gt $sa -and $sob -gt $ib -and $sob -gt $ca) {return $sob}
elseif ($fa -gt $sa -and $fa -gt $ib -and $fa -gt $ca) {return $fa}
elseif ($sa -gt $ib -and $sa -gt $ca) {return $sa}
elseif ($ib -gt $ca) {return $ib}
else {return $ca}
}
如果我们知道需要创建多少个子记录,我们可以使用 do-while 循环遍历它们,并创建一个包含所有数据的 PSCustomObject。
Do{
if ($x -eq 0) {
[pscustomobject]@{
"Display Name" = $_.DisplayName
"Emailaddress" = $_.PrimarySMTPAddress
"Mailbox type" = $_.RecipientTypeDetails
"Read and manage" = @($fullAccessUsers)[$x]
"Send as" = @($sendAsUsers)[$x]
"Send on behalf" = @($sendOnbehalfUsers)[$x]
"Inbox folder" = @($inboxFolder.users)[$x]
"Inbox folder Permission" = @($inboxFolder.permission)[$x]
"Inbox folder Delegated" = @($inboxFolder.delegated)[$x]
"Calendar" = @($calendarFolder.users)[$x]
"Calendar Permission" = @($calendarFolder.permission)[$x]
"Calendar Delegated" = @($calendarFolder.delegated)[$x]
}
$x++;
}else{
[pscustomobject]@{
"Display Name" = ''
"Emailaddress" = ''
"Mailbox type" = ''
"Read and manage" = @($fullAccessUsers)[$x]
"Send as" = @($sendAsUsers)[$x]
"Send on behalf" = @($sendOnbehalfUsers)[$x]
"Inbox folder" = @($inboxFolder.users)[$x]
"Inbox folder Permission" = @($inboxFolder.permission)[$x]
"Inbox folder Delegated" = @($inboxFolder.delegated)[$x]
"Calendar" = @($calendarFolder.users)[$x]
"Calendar Permission" = @($calendarFolder.permission)[$x]
"Calendar Delegated" = @($calendarFolder.delegated)[$x]
}
$x++;
}
$currentUser = $_.DisplayName
if ($mailboxes.Count -gt 1) {
Write-Progress -Activity "Collecting mailbox permissions" -Status "Current Count: $i" -PercentComplete (($i / $mailboxes.Count) * 100) -CurrentOperation "Processing mailbox: $currentUser"
}
}
while($x -ne $mostRecords)
下载完整的邮箱权限脚本
您可以从我的 GitHub 页面下载完整的脚本。这样您将始终拥有最新版本的脚本。始终首先使用 -UserPrincipalName
参数对一小组用户测试脚本。
如果您想了解如何运行 PowerShell 脚本,请务必阅读本文。
总结
要从命令行轻松运行脚本,您还可以将脚本(或文件夹)添加到 PowerShell 配置文件中。请阅读本文中的所有内容。
我希望您发现这个脚本有用,如果您有任何疑问,请在下面发表评论。
您可能还喜欢以下 PowerShell 报告脚本之一:
- OneDrive 大小报告
- 邮箱大小报告
- 外交部现状报告
猜你还喜欢
- 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