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

[玩转系统] 从所有邮箱中删除 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)。单击角色 > 管理员角色。单击添加角色组

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

为新角色组命名 - 例如,应用程序模拟

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

分配角色ApplicationImpersonation

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

添加您自己、全局管理员或您作为成员创建的服务帐户。

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

查看角色组并单击添加角色组

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

角色组应用程序模拟已成功创建。

注意:应用更改最多可能需要一个小时。

下一步是安装 Microsoft Exchange Web Services Managed API 2.2。

Microsoft Exchange Web 服务托管 API 2.2

下载 Microsoft Exchange Web 服务托管 API 2.2。将文件保存到系统中。

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

以管理员身份运行安装程序并安装 Microsoft Exchange Web Services Managed API 2.2。

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

安装后,验证在安装路径中是否可以看到这些文件。

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

下一步是创建文本文件。

为脚本创建文本文件

C:\Temp 文件夹中创建三个文本文件。

  1. 文件夹.txt
  2. 日志.txt
  3. 用户.txt

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

打开 Folders.txt 并在顶部添加 FolderName。在每一行添加文件夹名称。在我们的示例中,我们要删除邮箱中的四个文件夹。

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

该脚本将生成日志并将其放置在 Log.txt 文件中。保持其为空。

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

打开 Users.txt 并在顶部添加 EmailAddress。在每一行添加电子邮件地址。该脚本将从这些邮箱中删除文件夹。

了解更多:将邮箱列表导出到 CSV 文件 »

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

下一步是在 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.点击注册

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

8.复制应用程序(客户端)ID目录(租户)ID并将其粘贴到记事本中(稍后您将需要它)。

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

下一步是下载 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 获取了他邮箱中的所有文件夹。

运行脚本之前

  • 子文件夹音频文件夹
  • 子文件夹文件夹图片
  • 文件夹文件夹音乐
  • 文件夹文件夹视频

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

将目录更改为脚本文件夹并运行脚本。

PS C:\> cd C:\scripts
PS C:\scripts> .\Delete-Folders-EXO.ps1

将显示一个登录窗口。使用您作为成员添加到模拟角色的帐户登录,然后选中代表您的组织同意复选框。接下来,点击接受

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

它开始搜索邮箱中的文件夹。输出将写入 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 邮箱中删除。

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

如果您想知道从哪些邮箱中删除了哪些文件夹,请检查 Log.txt 文件。

[玩转系统] 从所有邮箱中删除 Exchange Online 中的文件夹

本文是否帮助您使用 PowerShell 删除 Exchange Online 中的文件夹?

继续阅读:使用 PowerShell 将邮箱移动到 Exchange Online »

结论

您了解了如何从 Exchange Online 中的所有邮箱中删除文件夹。您不仅可以删除一个文件夹,还可以在文本文件中放弃特定的文件夹。在立即运行脚本之前,请按照文章中显示的步骤进行操作。 Delete-Folders-EXO.ps1 PowerShell 脚本非常适合实现此结果。

您喜欢这篇文章吗?您可能还喜欢如何使邮箱大小大于 Microsoft 365 中的邮箱大小。不要忘记关注我们并分享这篇文章。

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

取消回复欢迎 发表评论:

关灯