0

im sorry if my english is bad. but here is what im trying to do.

atm i have a script that shows Mailbox size. and a script that shows archive size. im using these scripts to add information to Hudu later.

is there a way to get this information into one table?

below is how i'm getting archive info:

#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

the output from that script would look something like this:

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       

i would like that table to also show: Mailbox Size, Item count and last logon time. like you get by running:

$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

is there a way to match these to together and get a table with information from both? adding fields into $result is ofc easy. but then the output looks messed up. so matching the tables is where im stuck atm.

Elly
  • 51
  • 8

2 Answers2

0

Translating calculated property tables to a single hashtable of properties is pretty straight forward:

  1. Use the Name value as the key
  2. Use the contents of the Expression value as the value
  3. Replace $_ with the name of the source variable (in this case $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
        })
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • bruh ive tried and failed ALL day doing this. thank u so SO SO SO much lol! it worked like A CHARM – Elly Nov 03 '21 at 10:10
-1

I updated it a little bit:

  1. Fixed issue where you always skip the first 2 mailboxes (start with i = 0 and i++ at the end of the loop)
  2. Added recoverable items folder so you can also report on hold sizes (very slow, but useful)
  3. Added mailbox sizes
  4. removed some stuff I didn't need
  5. Updated to EXO cmdlets for more speed
$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