return 最小数据库的函数不工作

Function to return smallest db is not working

我有一些我在网上找到的 PoSH 函数,它将 return 最小的交换数据库。在测试脚本时,我发现它没有按预期工作;那是最小的数据库 returned 实际上不正确。

这是代码:

#http://izzy.org/scripts/Exchange/Admin/Create-Mailbox.ps1
$DBFilter = "MBX_*" # Limit databases to only those that start with "Primary"

Function Get-SmallestDB {
    Try {
        $MBXDbs = Get-MailboxDatabase | ? {$_.Identity -like $DBFilter } 
        $MBXDBCount = $PSSessions.Count
    }
    Catch {
        $MBXDBCount =  0
    }
    If (!$MBXDbs) {ExitScript "find databases with a name that matches a filter of [$DBFilter]." $False}

    # Loop through each of the MBXDbs
    ForEach ($MBXDB in $MBXDbs) {
        # Get current mailboxes sizes by summing the size of all mailboxes and "Deleted Items" in the database
        $TotalItemSize = Get-MailboxStatistics -Database $MBXDB | %{$_.TotalItemSize.Value.ToMB()} | Measure-Object -sum
        $TotalDeletedItemSize = Get-MailboxStatistics -Database $MBXDB.DistinguishedName | %{$_.TotalDeletedItemSize.Value.ToMB()} | Measure-Object -sum
        $TotalDBSize = $TotalItemSize.Sum + $TotalDeletedItemSize.Sum
        Write-Host "$MBXDB $($TotalItemSize.Sum) $($TotalDeletedItemSize.Sum) $TotalDBSize"
        # Compare the sizes to find the smallest DB
        If (($TotalDBSize -lt $SmallestDBsize) -or ($SmallestDBsize -eq $null)) {
            $SmallestDBsize = $DBsize
            $SmallestDB = $MBXDB }}
    return $SmallestDB }

基本上当我 运行 在我的 Exchange 环境中它 returns "MBX_20" 作为 $SmallestDB。但是我添加了一些调试代码(Write-Host 以输出 $($TotalItemSize.Sum、$($TotalDeletedItemSize.Sum) 和 $TotalDBSize 的值,结果是:

MBX_1 140561 5180 145741
MBX_2 190865 15882 206747
MBX_3 174393 1714 176107
MBX_4 122362 6479 128841
MBX_5 108833 15409 124242
MBX_6 196569 13793 210362
MBX_7 114298 2144 116442
MBX_8 140896 21558 162454
MBX_9 160024 13364 173388
MBX_10 188268 6046 194314
MBX_11 132256 15300 147556
MBX_12 173262 6486 179748
MBX_13 101107 3761 104868
MBX_14 131453 4930 136383
MBX_15 134682 4424 139106
MBX_16 146767 12484 159251
MBX_17 155224 2074 157298
MBX_18 117147 12270 129417
MBX_19 129101 6597 135698
MBX_20 134675 9059 143734

如您所见,MBX_20 143734 不是最小的数据库。我现在正在尝试修复代码,但我不太擅长 PoSH。有什么建议吗?

根据我的评论,我认为这是一个错字

$SmallestDBsize = $DBsize 

应该是

$SmallestDBsize = $TotalDBSize