Alby11
Alby11

Reputation: 645

Powershell: Transform TotalItemSize into INT

Hi i'm writing a PS Script to cofront mailboxes size to a limit and to send an email to users when this limit is exceeded.

I've prepared the Size variable like this:

$Size=Get-MailboxStatistics -Identity $_.samaccountname | Select-Object @{n="TotalItemSize";e={($_.totalitemsize -split " \(")[0]}}

and i get something like:

"samaccountname"  @{TotalItemSize=1.991 GB}

I have 2 questions:

  1. Is it possible to get rid of everything except 1.991 GB ?
  2. Can i tranform this value into an INT?

Thanks in advance.

Upvotes: 2

Views: 5321

Answers (3)

JPBlanc
JPBlanc

Reputation: 72680

Have a look to $a

$a = (Get-MailboxStatistics -Identity jean-paul.blanc).TotalItemSize

$a | get-member

You can see that it contains a property value that is a Microsoft.Exchange.Data.ByteQuantifiedSize

Now have a look to Microsoft documentation, you can find the method you are looking for Tobytes() so you can write :

$a.value.ToBytes()

or in your case :

$size = (Get-MailboxStatistics -Identity "Your user identity").TotalItemSize.value.Tobytes()

Edited :

If you only have got the string let say "34.01 MB (35,666,338 bytes)"

You can rebuid localy the object using :

$a = [Microsoft.Exchange.Data.ByteQuantifiedSize]::parse("34.01 MB (35,666,338 bytes)")

Upvotes: 3

mjolinor
mjolinor

Reputation: 68341

This will get you the size as an int:

$Size=
 Get-MailboxStatistics -Identity $_.samaccountname | 
 Select-Object -ExpandProperty totalitemsize

 $Size = $Size -replace '^.+\((.+\))','$1' -replace '\D' -as [int]

I'd use that, and then divide by 1GB if you want an int GB value. Mailboxes with smaller sizes may be returned as MB or even KB. It's easier to start with the actual byte count and do the conversion yourself than parse all the possible string formats that may be returned.

But if you set the IssueWarningQuota on the mailbox, the system will automatically start sending them an email once a day when they exceed that quota.

Edit: there are also object methods available for getting the byte counts in various formats (like ToBytes()). These work fine as long as you're in an actual EMS shell. If you try to use the same script in an implicit remoting session it will fail because now you're working with deserialized objects, and you don't have those methods any more. The string parsing method is not as "pure" as using the object methods, but it's portable between those environments.

Upvotes: 3

Tim Ferrill
Tim Ferrill

Reputation: 1684

You can convert decimal to int (using rounding) by casting it as an [int64]:

[int64]$val = 1.991

Or if you want to round down you can use [math:

[math]::floor(1.991)

Upvotes: 0

Related Questions