[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹
作者:精品下载站 日期:2024-12-14 03:17:10 浏览:15 分类:玩电脑
从所有邮箱中删除 Exchange Online 中的文件夹
您想使用 PowerShell 从 Exchange Online 中的所有邮箱中删除文件夹吗?更有趣的是,如果您想删除特定文件夹并定位特定邮箱该怎么办?如何实现这一点令人印象深刻,对吧?本文将教您如何从所有邮箱中删除 Exchange Online 中的文件夹。
开始删除 Exchange Online 中的文件夹之前
仔细按照文章中所示的步骤操作:
- 从 Exchange Online 中的特定邮箱删除特定文件夹
- 从 Exchange Online 中的所有邮箱中删除特定文件夹
在 Exchange Online 中添加模拟权限
当您的服务应用程序需要访问多个邮箱并“充当”邮箱所有者时,请使用模拟。详细了解 Exchange 中的模拟和 EWS。
创建并将模拟权限添加到将运行脚本的帐户。它可以是全局管理员帐户或服务帐户。在我们的示例中,添加了全局管理员帐户。
登录 Exchange 管理中心 (Exchange Online)。单击角色 > 管理员角色。单击添加角色组。
为新角色组命名 - 例如,应用程序模拟。
分配角色ApplicationImpersonation。
添加您自己、全局管理员或您作为成员创建的服务帐户。
查看角色组并单击添加角色组。
角色组应用程序模拟已成功创建。
注意:应用更改最多可能需要一个小时。
下一步是安装 Microsoft Exchange Web Services Managed API 2.2。
Microsoft Exchange Web 服务托管 API 2.2
下载 Microsoft Exchange Web 服务托管 API 2.2。将文件保存到系统中。
以管理员身份运行安装程序并安装 Microsoft Exchange Web Services Managed API 2.2。
安装后,验证在安装路径中是否可以看到这些文件。
下一步是创建文本文件。
为脚本创建文本文件
在 C:\Temp 文件夹中创建三个文本文件。
- 文件夹.txt
- 日志.txt
- 用户.txt
打开 Folders.txt 并在顶部添加 FolderName。在每一行添加文件夹名称。在我们的示例中,我们要删除邮箱中的四个文件夹。
该脚本将生成日志并将其放置在 Log.txt 文件中。保持其为空。
打开 Users.txt 并在顶部添加 EmailAddress。在每一行添加电子邮件地址。该脚本将从这些邮箱中删除文件夹。
了解更多:将邮箱列表导出到 CSV 文件 »
下一步是在 Azure AD 中注册应用程序。
在 Azure AD 中注册应用程序
要在 Azure AD 中注册应用程序,请执行以下步骤:
1. 登录 Microsoft Azure 门户。
2. 导航到 Azure Active Directory > 应用注册。
3.点击新注册。
4.填写名称DeleteFolders。
5. 选择仅此组织目录中的帐户。
6. 选择公共客户端/本机(移动和桌面)并填写 URL:
https://login.microsoftonline.com/common/oauth2/nativeclient
7.点击注册。
8.复制应用程序(客户端)ID和目录(租户)ID并将其粘贴到记事本中(稍后您将需要它)。
下一步是下载 PowerShell 脚本。
准备删除文件夹 Exchange Online PowerShell 脚本
下载Delete-Folders-EXO.ps1 PowerShell 脚本并将其保存到C:\Scripts 文件夹。
<#
.SYNOPSIS
Delete-Folders-EXO.ps1
.DESCRIPTION
Delete specific folder/folders from specific mailboxes in Exchange Online.
.LINK
www.a-d.site/delete-folder-in-exchange-online-from-all-mailboxes
.NOTES
Written by: Catalin Streang
Edited by: ALI TAJRAN
Website: www.a-d.site
LinkedIn: linkedin.com/in/a-d
.LICENSE
The MIT License (MIT)
Copyright (c) 2020 ALI TAJRAN
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
.CHANGELOG
V1.00, 10/09/2020 - Initial version
V2.00, 01/31/2023 - Support Modern Authentication
.REQUIRED
The script requires EWS Managed API 2.2.
Make sure the Import-Module command matches the Microsoft.Exchange.WebServices.dll location of EWS Managed API, chosen during the installation.
#>
# Provide Directory (tenant) ID or Tenant domain name
$TenantId = "2d845888-c017-4121-abe5-02f619863965"
# Provide Application (client) ID
$AppClientId = "fccc7ed0-6218-41f9-a89b-d43b29bf7681"
$MsalParams = @{
ClientId = $AppClientId
TenantId = $TenantId
Scopes = "https://outlook.office.com/EWS.AccessAsUser.All"
}
$MsalResponse = Get-MsalToken @MsalParams
$EWSAccessToken = $MsalResponse.AccessToken
# Variables
[string]$info = "White" # Color for informational messages
[string]$warning = "Yellow" # Color for warning messages
[string]$error = "Red" # Color for error messages
[string]$success = "Green" # Color for success messages
[string]$LogFile = "C:\Temp\Log.txt" # Path of the Log File
[string]$FoldersCSV = "C:\Temp\Folders.txt" # Path of the Folders File
[string]$UsersCSV = "C:\Temp\Users.txt" # Path of the Users File
function DeleteFolder($MailboxName) {
Write-Host "Searching for folder in Mailbox Name:" $MailboxName -ForegroundColor $info
Add-Content $LogFile ("Searching for folder in Mailbox Name:" + $MailboxName)
# Change the user to impersonate
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
do {
$oFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1)
$oFolderView.Traversal = [Microsoft.Exchange.Webservices.Data.FolderTraversal]::Deep
$oSearchFilter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName, $FolderName)
$oFindFolderResults = $service.FindFolders([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot, $oSearchFilter, $oFolderView)
if ($oFindFolderResults.TotalCount -eq 0) {
Write-Host "Folder $FolderName does not exist in Mailbox:" $MailboxName -ForegroundColor $warning
Add-Content $LogFile ("Folder does not exist in Mailbox:" + $MailboxName)
}
else {
Write-Host "Folder $FolderName EXISTS in Mailbox:" $MailboxName -ForegroundColor $success
Add-Content $LogFile ("Folder EXISTS in Mailbox:" + $MailboxName)
$oFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $oFindFolderResults.Folders[0].Id)
Write-Host "Deleting Folder:" $FolderName -ForegroundColor $success
Add-Content $LogFile ("Deleting Folder:" + $FolderName)
# You can choose from a few delete types, just choose one:
$oFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
#$oFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete)
}
} while ($oFindFolderResults.TotalCount -ne 0)
$service.ImpersonatedUserId = $null
}
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services.2\Microsoft.Exchange.WebServices.dll"
$service = [Microsoft.Exchange.WebServices.Data.ExchangeService]::new()
# Connect with Modern Authentication
$service.Credentials = [Microsoft.Exchange.WebServices.Data.OAuthCredentials]$EWSAccessToken
# Exchange Online URL
$service.Url = New-Object Uri("https://outlook.office365.com/EWS/Exchange.asmx")
# Read the data
Import-Csv $FoldersCSV -Encoding UTF8 | Foreach-Object {
$FolderName = $_.FolderName.ToString()
Import-Csv $UsersCSV -Encoding UTF8 | Foreach-Object {
$EmailAddress = $_.EmailAddress.ToString()
# Catch the errors
trap [System.Exception] {
Write-Host ("Error: " + $_.Exception.Message) -ForegroundColor $error
Add-Content $LogFile ("Error: " + $_.Exception.Message)
continue
}
DeleteFolder($EmailAddress)
}
}
下一步是编辑脚本。
编辑删除文件夹 Exchange Online PowerShell 脚本
使用从 Azure AD 中注册的应用程序复制的值更改 TenantId(第 46 行)和 AppClientId(第 49 行)。
该脚本将完全删除文件夹以及文件夹中的邮件,并且您无法从“可恢复项目”文件夹中恢复它们。
是否要删除文件夹并将邮件移至邮箱的“可恢复项目”文件夹?将其更改为SoftDelete。
检查第 100 行和101可用选项。
$oFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
#$oFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete)
下一步是安装 MSAL.PS PowerShell 模块。
安装 MSAL.PS PowerShell 模块
我们将使用 MSAL.PS PowerShell 模块从 Microsoft 获取令牌。
以管理员身份启动 Windows PowerShell 或 Windows PowerShell ISE。安装 MSAL.PS PowerShell 模块。
Install-Module -Name MSAL.PS
下一步也是最后一步是运行脚本。
运行删除文件夹 Exchange Online PowerShell 脚本
在我们的示例中,我们知道用户 James 获取了他邮箱中的所有文件夹。
运行脚本之前
- 子文件夹音频文件夹
- 子文件夹文件夹图片
- 文件夹文件夹音乐
- 文件夹文件夹视频
将目录更改为脚本文件夹并运行脚本。
PS C:\> cd C:\scripts
PS C:\scripts> .\Delete-Folders-EXO.ps1
将显示一个登录窗口。使用您作为成员添加到模拟角色的帐户登录,然后选中代表您的组织同意复选框。接下来,点击接受。
它开始搜索邮箱中的文件夹。输出将写入 Log.txt 文件。
Searching for folder in Mailbox Name: [email protected]
Folder EXISTS in Mailbox: [email protected]
Deleting Folder: Folder Audio
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder EXISTS in Mailbox: [email protected]
Deleting Folder: Folder Pictures
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder EXISTS in Mailbox: [email protected]
Deleting Folder: Folder Music
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder EXISTS in Mailbox: [email protected]
Deleting Folder: Folder Videos
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
重新运行脚本。它将显示没有任何内容可删除。
PS C:\scripts> .\Delete-Folders-EXO.ps1
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
Searching for folder in Mailbox Name: [email protected]
Folder does not exist in Mailbox: [email protected]
运行脚本后
这些文件夹将从 Office 365 邮箱中删除。
如果您想知道从哪些邮箱中删除了哪些文件夹,请检查 Log.txt 文件。
本文是否帮助您使用 PowerShell 删除 Exchange Online 中的文件夹?
继续阅读:使用 PowerShell 将邮箱移动到 Exchange Online »
结论
您了解了如何从 Exchange Online 中的所有邮箱中删除文件夹。您不仅可以删除一个文件夹,还可以在文本文件中放弃特定的文件夹。在立即运行脚本之前,请按照文章中显示的步骤进行操作。 Delete-Folders-EXO.ps1 PowerShell 脚本非常适合实现此结果。
您喜欢这篇文章吗?您可能还喜欢如何使邮箱大小大于 Microsoft 365 中的邮箱大小。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 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