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

[玩转系统] 如何导出小于总大小的邮箱

作者:精品下载站 日期:2024-12-14 06:30:43 浏览:14 分类:玩电脑

如何导出小于总大小的邮箱


我们希望在 Exchange Server 中获取总大小小于给定数量的邮箱。在我们将邮箱迁移到另一个数据库之前,这是一个很好的方法。在本文中,您将了解如何导出小于总大小的邮箱。

导出小于总大小的邮箱 PowerShell 脚本

Export-MailboxesTotalSize.ps1 将获取小于您在脚本中定义的总大小的所有邮箱。对于每个邮箱,它都会收集以下信息:

  1. 显示名称

  2. 总大小GB

  3. 用户主体名称

  4. 主SMTP地址

  5. SMTP别名地址

注意:如果您在脚本中定义总大小为 100 GB,并且两个邮箱大小为 50 GB,则只会显示这两个邮箱。这是因为两个邮箱的总容量为 100 GB。

准备 Export-MailboxesTotalSize PowerShell 脚本

在 Exchange Server (C:) 驱动器上创建两个文件夹:

  • 温度

  • 脚本

下载 Export-MailboxesTotalSize.ps1 PowerShell 脚本并将其放置在 C:\scripts 文件夹中。该脚本会将 CSV 文件导出到 C:\temp 文件夹。

确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Export-MailboxesTotalSize.ps1 并将其放置在 C:\scripts 文件夹中。

<#
    .SYNOPSIS
    Export-MailboxesTotalSize.ps1

    .DESCRIPTION
    Export mailboxes that are less than the defined total size.

    .LINK
    www.a-d.site/export-mailboxes-total-size-less-than/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.a-d.site
    LinkedIn:   linkedin.com/in/a-d

    .CHANGELOG
    V1.00, 10/22/2023 - Initial version
#>

# Define the target total size in GB
$targetTotalSizeGB = 100

# Specify the mailbox database name to search within
$mailboxDatabaseName = "DB01"

# Define the export file path
$exportFilePath = "C:\temp\Mailboxes.csv"

# Get a list of mailbox sizes within the specified mailbox database
$mailboxSizes = Get-Mailbox -ResultSize Unlimited -Database $mailboxDatabaseName |
Get-MailboxStatistics |
Select-Object DisplayName, @{Name = "TotalItemSizeGB"; Expression = { [math]::Round($_.TotalItemSize.Value.ToBytes() / 1GB, 2) } } |
Sort-Object TotalItemSizeGB

# Initialize variables
$selectedMailboxes = @()
$currentTotalSizeGB = 0

# Iterate through mailboxes to find a combination that adds up to the target total size
foreach ($mailbox in $mailboxSizes) {
    $mailboxSizeGB = $mailbox.TotalItemSizeGB

    # Check if adding this mailbox keeps the total size within or equal to the target size
    if (($currentTotalSizeGB + $mailboxSizeGB) -le $targetTotalSizeGB) {
        $selectedMailbox = Get-Mailbox $mailbox.DisplayName -ResultSize Unlimited
        $upn = $selectedMailbox.UserPrincipalName
        $smtpPrimary = $selectedMailbox.PrimarySmtpAddress
        $smtpAliases = $selectedMailbox.EmailAddresses | Where-Object { $_.PrefixString -eq "smtp" -and $_.SmtpAddress -ne $smtpPrimary } | ForEach-Object { $_.SmtpAddress }
        
        $selectedMailboxes += [PSCustomObject]@{
            'DisplayName'        = $mailbox.DisplayName
            'TotalSizeGB'        = $mailbox.TotalItemSizeGB
            'UserPrincipalName'  = $upn
            'PrimarySmtpAddress' = $smtpPrimary
            'SMTPAliasAddresses' = $smtpAliases -join ", "
        }
        
        $currentTotalSizeGB += $mailboxSizeGB

        # Check if the current total size equals the target size or exceeds it
        if ($currentTotalSizeGB -ge $targetTotalSizeGB) {
            break
        }
    }
}

# Display the selected mailboxes in an interactive table
$selectedMailboxes | Sort-Object TotalSizeGB -Descending | Out-GridView -Title "Selected Mailboxes"

Write-Host "Total Size: $currentTotalSizeGB GB" -ForegroundColor Cyan

# Export the selected mailboxes to a CSV file
$selectedMailboxes | Sort-Object TotalSizeGB -Descending | Export-Csv -Path $exportFilePath -NoTypeInformation -Encoding UTF8

# Display a message indicating successful export
Write-Host "File exported successfully to $exportFilePath" -ForegroundColor Green
  • 第 21 行:编辑总大小

  • 第24行:编辑数据库名称

  • 第 27 行:编辑导出 CSV 文件路径

运行 Export-MailboxesTotalSize PowerShell 脚本

以管理员身份运行 Exchange 命令行管理程序。更改脚本文件夹的路径。之后,运行 Export-MailboxesTotalSize.ps1 脚本。

PS C:\> cd c:\scripts\
PS C:\scripts> .\Export-MailboxesTotalSize.ps1

输出将显示它收集的总大小(以 GB 为单位)。

Total Size: 1.18 GB

在 Out-GridView 中检查邮箱

Out-GridView 将显示包含邮箱的列及其总大小(以 GB 为单位)。

[玩转系统] 如何导出小于总大小的邮箱

打开邮箱报告 CSV 文件

Export-MailboxesTotalSize.ps1 PowerShell 脚本会将小于总大小的邮箱导出到 CSV 文件。在路径 C:\temp 中找到 Mailboxes.csv 文件。

使用您喜欢的应用程序打开 CSV 文件。在此示例中,它是 Microsoft Excel。

[玩转系统] 如何导出小于总大小的邮箱

小于报告总大小的邮箱看起来很棒。

注意:您是否要将所有邮箱导出为 CSV 文件并获取所有邮箱大小和更多信息?阅读文章使用 PowerShell 获取 Exchange 中所有用户的邮箱大小。

结论

您学习了如何使用 Powershell 导出小于总大小的邮箱。使用 Export-MailboxesTotalSize.ps1 PowerShell 脚本获取邮箱小于总大小的报告,并仔细查看它。

您喜欢这篇文章吗?您可能还喜欢使用 CSV 文件迁移 Exchange 邮箱。不要忘记关注我们并分享这篇文章。

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

取消回复欢迎 发表评论:

关灯