0
votes

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

1

1 Answers

1
votes

Because you are running this in remote session, the types of the returned objects are lost. Try $stat.TotalItemSize.Value.GetType() and you'll notice it's PSCustomObject.

You could do with some String parsing. First split the Value from spaces:

$stat.TotalItemSize.Value -split " "

Take the first item from the resulting array:

($stat.TotalItemSize.Value -split " ")[0]

Finally cast it to number:

[float]($stat.TotalItemSize.Value -split " ")[0]

The line in your code should look like:

MBytes = [float]($stat.TotalItemSize.Value -split " ")[0]