locked
Any ways to convert a mailbox's TotalItemSize to a number for comparison RRS feed

  • Question

  • Hello,

    I am trying to write a script, which Get-MailboxStatistics of a user's mailbox and determines if the size of the mailbox is less than 50GB, then it will remove the O365 license from the account.

    I have done countless researches and tried everything suggested on the internet forums, but I cannot use the value of the TotalItemSize for comparison.

    For example, consider these lines:

    $size = Get-MailboxStatistics user@email.com | Select @{name=”TotalItemSize (GB)”;expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}}
    $size

    The output of $size:

    TotalItemSize (GB)
    ------------------
                  5.88

    How can I use this number to compare? let's say:

    if ($size -lt "50.00") { then go and remove the user's license }

    I get error with that line.

    Cannot compare "@{TotalItemSize (GB)=5.88}" to "50.00" because the objects are not the same type or the object "@{TotalItemSize (GB)=5.88}" does not
    implement "IComparable".

    Thank you in advance for your inputs!


    Wednesday, April 1, 2020 9:15 PM

All replies

  • Does $TotalItemSize.ToBytes() (or ToGB()) work? Not sure if you want to compare bytes to bytes, or GB gigabytes.

    See here: https://docs.microsoft.com/en-us/previous-versions/office/exchange-server-api/ff325423(v=exchg.150)


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, April 1, 2020 9:37 PM
  • You have to "expand" the value from your output. You try to compare an Selected-object to a Integerobject

    just try this 
    $size = Get-MailboxStatistics user@email.com | Select -expandproperty @{name=”TotalItemSize (GB)”;expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}}

    www.netlogix.de

    Thursday, April 2, 2020 9:52 AM
  • Even without converting your "$size" you could just do this:

         if ($_.TotalItemSize -lt 50GB) { #do something }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Thursday, April 2, 2020 2:36 PM
  • Thank you for all your inputs. I have tried a your suggestions but still got errors
    Monday, April 6, 2020 2:08 PM
  • Thank you for all your inputs. I have tried a your suggestions but still got errors

    Rich's last post is how you do this although it will fail if the value is null.


    \_(ツ)_/

    Monday, April 6, 2020 3:17 PM
  • What errors???

    Let's see the code you're using and the value in the TotalItemSize property of Get-MailBoxStatistics output.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, April 6, 2020 7:29 PM
  • I kept getting

    Cannot compare "@{TotalItemSize (GB)=5.88}" to "50.00" because the objects are not the same type or the object "@{TotalItemSize (GB)=5.88}" does not
    implement "IComparable".


    But, I found where my error was, it was in this piece

    expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}

    It should be 0 at the end, not 2. Then it rounds the value to 6 instead of 5.88. The comparison works.

    Thank you!

    Tuesday, April 7, 2020 2:25 PM
  • Your mistake is trying very hard not to use the much easier (and clearer) way to compare the mailbox's total item size.

    You're already using the value "1GB" in your conversion of a string to a number. Why not just use the "50GB" as suggested???


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, April 7, 2020 3:33 PM