如何在相同 table 中获取邮箱大小和存档大小 - powershell,exchange

How to get Mailbox size and Archive size in same table - powershell, exchange

对不起,如果我的英语不好。但这是我想做的。

atm 我有一个显示邮箱大小的脚本。和一个显示存档大小的脚本。稍后我将使用这些脚本向 Hudu 添加信息。

有没有办法将这些信息整合为一个table?

以下是我获取存档信息的方式:

#Getting archive info 
$Result = @() 
$mailboxes = Get-Mailbox -ResultSize Unlimited
$totalmbx = $mailboxes.Count
$i = 1 
$mailboxes | ForEach-Object {
    $i++
    $mbx = $_
    $size = $null
 
    Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"
 
    if ($mbx.ArchiveStatus -eq "Active") {
        $mbs = Get-MailboxStatistics $mbx.UserPrincipalName
 
        if ($mbs.TotalItemSize -ne $null) {
            $size = [math]::Round(($mbs.TotalItemSize.ToString().Split('(')[1].Split(' ')[0].Replace(',', '') / 1MB), 2)
        }
        else {
            $size = 0 
        }
    }
 
    $Result += New-Object -TypeName PSObject -Property $([ordered]@{ 
            UserName                    = $mbx.DisplayName
            UserPrincipalName           = $mbx.UserPrincipalName
            ArchiveStatus               = $mbx.ArchiveStatus
            ArchiveName                 = $mbx.ArchiveName
            ArchiveState                = $mbx.ArchiveState
            ArchiveMailboxSizeInMB      = $size
            ArchiveWarningQuota         = if ($mbx.ArchiveStatus -eq "Active") { $mbx.ArchiveWarningQuota } Else { $null } 
            ArchiveQuota                = if ($mbx.ArchiveStatus -eq "Active") { $mbx.ArchiveQuota } Else { $null } 
            AutoExpandingArchiveEnabled = $mbx.AutoExpandingArchiveEnabled
        })
}


$Result | Select UserName, UserPrincipalName, ArchiveMailboxSizeInMB, ArchiveWarningQuota, ArchiveQuota, AutoExpandingArchiveEnabled, ArchiveState| Format-Table

该脚本的输出如下所示:

UserName      UserPrincipalNam     ArchiveMailboxSizeInMB     ArchiveWarningQuota               ArchiveQuota                     AutoExpandingArchiveEnabled    ArchiveState
--------      -----------------     ----------------------     -------------------              ------------                     ---------------------------    ------------
User          user@domain.com                        14,12     90 GB (96,636,764,160 bytes)     100 GB (107,374,182,400 bytes)                         False    Local       
User          user@domain.com                                                                                                                          False    None        
User          user@domain.com                                                                                                                          False    None        
User          user@domain.com                         2,42     90 GB (96,636,764,160 bytes)     100 GB (107,374,182,400 bytes)                         False    Local       

我希望 table 还显示:邮箱大小、项目计数和上次登录时间。就像你通过 运行:

$mailboxstatspruser = $mailboxes | Get-MailboxStatistics | select-object DisplayName, @{name=”TotalItemSize (GB)”;expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}},ItemCount,LastLogonTime

有没有一种方法可以将它们匹配在一起并获得包含两者信息的 table?将字段添加到 $result 很容易。但随后输出看起来一团糟。所以匹配 tables 是我卡在 atm 的地方。

将计算的 属性 表转换为单个属性哈希表非常简单:

  1. 使用 Name 值作为键
  2. 使用Expression值的内容作为值
  3. $_替换为源变量的名称(在本例中为$mbs
    $Result += New-Object -TypeName PSObject -Property $([ordered]@{ 
            UserName                    = $mbx.DisplayName
            UserPrincipalName           = $mbx.UserPrincipalName
            ArchiveStatus               = $mbx.ArchiveStatus
            ArchiveName                 = $mbx.ArchiveName
            ArchiveState                = $mbx.ArchiveState
            ArchiveMailboxSizeInMB      = $size
            ArchiveWarningQuota         = if ($mbx.ArchiveStatus -eq "Active") { $mbx.ArchiveWarningQuota } Else { $null } 
            ArchiveQuota                = if ($mbx.ArchiveStatus -eq "Active") { $mbx.ArchiveQuota } Else { $null } 
            AutoExpandingArchiveEnabled = $mbx.AutoExpandingArchiveEnabled
            'TotalItemSize (GB)'        = [math]::Round((($mbs.TotalItemSize.Value.ToString()).Split("(")[1].Split(" ")[0].Replace(",","")/1GB),2)
            ItemCount                   = $mbs.ItemCount
            LastLogonTime               = $mbs.LastLogonTime
        })

我稍微更新了一下:

  1. 解决了您总是跳过前 2 个邮箱(从循环结束时的 i = 0 和 i++ 开始)的问题
  2. 添加了可恢复的项目文件夹,因此您还可以报告保留大小(非常慢,但很有用)
  3. 添加了邮箱大小
  4. 删除了一些我不需要的东西
  5. 已更新为 EXO cmdlet 以提高速度
$Domain = "*@domain.com" #change to domain you need
$mailboxes = Get-Mailbox -ResultSize Unlimited -filter "(emailaddresses -like '$Domain') -and (recipienttypedetails -ne 'RoomMailbox')"
$totalmbx = $mailboxes.Count
$Result=@()
$i = 0
$mailboxes | ForEach-Object {
    
    $mbx = $_
    $size = $null
    $RecoverableItems = $null
    $RecoverableItemsSize = $null

    Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"
    $mbs = Get-EXOMailboxStatistics $mbx.UserPrincipalName
    $Size = [math]::Round(($mbs.TotalItemSize.ToString().Split('(')[1].Split(' ')[0].Replace(',','')/1MB),2)
    
    $RecoverableItems = Get-EXOMailboxFolderStatistics $mbx.UserPrincipalName -FolderScope RecoverableItems | Where-Object {$_.name -eq "Recoverable Items"}
    $RecoverableItemsSize = [math]::Round(($RecoverableItems.FolderAndSubfolderSize.ToString().Split('(')[1].Split(' ')[0].Replace(',','')/1MB),2)

    if ($mbx.ArchiveStatus -eq "Active"){
        $mbsArchive = Get-EXOMailboxStatistics $mbx.UserPrincipalName -archive
        
        if ($mbs.TotalItemSize -ne $null){
            $ArchiveSize = [math]::Round(($mbsArchive.TotalItemSize.ToString().Split('(')[1].Split(' ')[0].Replace(',','')/1MB),2)
            $ArchiveRecoverableItems = Get-EXOMailboxFolderStatistics $mbx.UserPrincipalName -Archive -FolderScope RecoverableItems | Where-Object {$_.name -eq "Recoverable Items"}
            $ArchiveRecoverableItemsSize = [math]::Round(($ArchiveRecoverableItems.FolderAndSubfolderSize.ToString().Split('(')[1].Split(' ')[0].Replace(',','')/1MB),2)

        }else{
            $ArchiveSize = 0 
            $ArchiveRecoverableItems = 0
            $ArchiveRecoverableItemsSize = 0

        }
    }

        $Result += New-Object -TypeName PSObject -Property $([ordered]@{ 
            UserName = $mbx.DisplayName
            UserPrincipalName = $mbx.UserPrincipalName
            ArchiveName =$mbx.ArchiveName
            MailboxSizeInMB = $Size
            ArchiveMailboxSizeInMB = $ArchiveSize
            HoldSize = $RecoverableItemsSize
            ArchiveHoldSize = $ArchiveRecoverableItemsSize
        })
    $i++
}
$Result | Export-CSV "C:\Temp\Archive-Mailbox-Report.csv" -NoTypeInformation -Encoding UTF8