When I run the below PowerShell script to get the list of inactive AD user account Exchange mailbox that is NOT categorized as a Shared Mailbox.
Script:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRODMAIL01-VM/PowerShell/ -Authentication Kerberos
Import-PSSession $Session -AllowClobber
$filter = '(Enabled -eq $false) -and (msExchRecipientTypeDetails -ne 4) -and (homeMDB -ne "$null")'
$properties = @('homeMDB', 'mailNickName', 'mail', 'DisplayName', 'SamAccountName', 'ProxyAddresses')
Get-ADUser -Filter $filter -Properties $properties |
ForEach-Object {
$stat = Get-MailboxStatistics $_.SamAccountName
$smtpAddresses = ($_.ProxyAddresses | Where-Object {$_ -like "*smtp:*" }) -replace 'smtp:'
New-Object -TypeName PSObject -Property ([ordered]@{
DisplayName = $_.DisplayName
mailNickName = $_.mailNickName
SamAccountName = $_.SamAccountName
mail = $_.mail
ProxyAddresses = $smtpAddresses -join ';'
HomeMDB = $_.homeMDB.Split(',=')[1]
MBytes = $stat.TotalItemSize.Value.ToMB()
LastLogonTime = $stat.LastLogonTime
LastLoggedOnUserAccount = $stat.SamAccountName
DisconnectDate = $stat.DisconnectDate
})
} |
Sort-Object MBytes -Descending |
Export-Csv C:\TEMP\Results.csv -NoTypeInformation
This is the error message that gets repeated thousands of times:
Method invocation failed because [Deserialized.Microsoft.Exchange.Data.ByteQuantifiedSize] does not contain a method named 'ToMB'. At line:15 char:5 + New-Object -TypeName PSObject -Property ([ordered]@{ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (ToMB:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
If the incoming data is a mix of MB, GB, KB, etc., you are better off parsing out the bytes in parentheses.
Here is how to take the original value and get it to bytes. After obtaining bytes, divide the number by 1000^n or 1024^n to get to the unit of measure you are seeking.
Example mailbox - 6.916 GB in size using the TotalItemSize attribute.
This will convert to GB, rounded to 3 decimals:
[math]::Round(([long]((($s.totalitemsize.value -split "\(")[1] -split " ")[0] -split "," -join ""))/[math]::Pow(1024,3),3)
How we get there:
TotalItemSize alone will not work, need its value, but that value contains data we cannot use as-is.
So, we split the data on the left parenthesis, which results in 2 values. We want the 2nd value, which is index 1 (index 0 is the first value). This is the 1 in the 4th cmdlet entry.
We want to strip off " bytes)" from this. So, split on the space, and grab index 0.
Now we need to remove the commas from the data. Split on the comma, and join it all back together using an empty string for the join.
This is still a string, however, and we cannot perform any math functions yet. We need to convert to a long integer - not just a regular integer. [long]
We can convert to MB by dividing by 1024 twice (yeah, I know that is really MiB to some people).
Or, we can divide by 1024 to the 2nd power [math]::Power(1024,2). To GB, go to the 3rd power.