Get-Mailbox combine with Get-MailboxStatistics

Welcome Forums General PowerShell Q&A Get-Mailbox combine with Get-MailboxStatistics

This topic contains 12 replies, has 3 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #110563

    Participant
    Points: 19
    Rank: Member

    My below script works fine but runs slow.  I need to run with -resultsize unlimited.  when I run it with -resultsize 550 users it takes 12 minutes to finish.  What can I change in my code to run it faster.

    Note: I shorten the last line $Results

    Thank you!
    $DataPath = "C:\temp85\o365UserData.csv"
    $Results = @()
    $MailboxUsers = get-EXOmailbox -resultsize 550

    foreach($user in $mailboxusers)
    {
    $UPN = $user.userprincipalname
    $License = Get-MsolUser -userprincipalname $UPN
    $MbxStats = Get-EXOMailboxStatistics $UPN

    $Properties = @{
    Name = $user.name
    UPN = $UPN
    Alias = $user.alias
    # UsageLocation = $user.usagelocation
    License = $License.Licenses[0].AccountSkuId
    # ArchiveStatus = $user.archivestatus
    LitigationHoldEnabled = $user.LitigationHoldEnabled
    LitigationHoldDate = $User.LitigationHoldDate
    LitigationHoldOwner = $User.LitigationHoldOwner
    LitigationHoldDuration = $User.LitigationHoldDuration
    RetentionPolicy = $user.RetentionPolicy
    RetentionHoldEnabled = $User.RetentionHoldEnabled
    ExchangeGuid = $user.ExchangeGuid
    SamAccountName = $User.SamAccountName
    Office = $user.Office
    InPlaceHolds = $User.InPlaceHolds
    IsInactiveMailbox = $User.IsInactiveMailbox
    IsSoftDeletedByRemove = $User.IsSoftDeletedByRemove
    IsSoftDeletedByDisable = $User.IsSoftDeletedByDisable
    AccountDisabled = $User.AccountDisabled
    OrganizationalUnit = $User.OrganizationalUnit
    DisplayName = $User.DisplayName
    PrimarySmtpAddress = $User.PrimarySmtpAddress
    RecipientTypeDetails = $User.RecipientTypeDetails
    WhenCreated = $User.WhenCreated
    WhenMailboxCreated = $User.WhenMailboxCreated
    WhenSoftDeleted = $User.WhenSoftDeleted
    # ServerName = $MbxStats.servername
    # DatabaseName = $MbxStats.databasename
    TotItemSize = $MbxStats.totalitemsize
    ItemCount = $MbxStats.ItemCount
    }

    $Results += New-Object psobject -Property $properties

    }

    $Results | Select-Object Name,DisplayName,License......,License | Export-Csv -notypeinformation -Path $DataPath

  • #110639

    Participant
    Points: 894
    Helping Hand
    Rank: Major Contributor

    Hi, I request you to format the code in the forum which makes other to easily understand your code, below link will help you.

  • #110642

    Participant
    Points: 894
    Helping Hand
    Rank: Major Contributor

    Not sure how much it will improve, below is another way of doing it.

    $MailboxUsers = Get-EXOmailbox -ResultSize 550 | Select-Object -Property Name,UPN,Alias, ` 
                                                                             LitigationHoldEnabled, ` 
                                                                             LitigationHoldDate, `
                                                                             LitigationHoldOwner, `
                                                                             LitigationHoldDuration, `
                                                                             RetentionPolicy, `
                                                                             RetentionHoldEnabled, `
                                                                             ExchangeGuid, `
                                                                             SamAccountName, `
                                                                             Office, `
                                                                             InPlaceHolds, `
                                                                             IsInactiveMailbox, `
                                                                             IsSoftDeletedByRemove, `
                                                                             IsSoftDeletedByDisable, `
                                                                             AccountDisabled, `
                                                                             OrganizationalUnit, `
                                                                             DisplayName, `
                                                                             PrimarySmtpAddress, `
                                                                             RecipientTypeDetails, `
                                                                             WhenCreated, `
                                                                             WhenMailboxCreated, `
                                                                             WhenSoftDeleted, `
                                                                             @{ E={(Get-MsolUser -userprincipalname $_.userprincipalname ).Licenses[0].AccountSkuId};L= 'License'}, `
                                                                             @{ E={(Get-EXOMailboxStatistics $_.userprincipalname ).totalitemsize};L= 'TotItemSize'}, `
                                                                             @{ E={(Get-EXOMailboxStatistics $_.userprincipalname ).ItemCount};L= 'ItemCount'}
    
    • #110654
      js

      Participant
      Points: 326
      Helping Hand
      Rank: Contributor

      Kvprasoon, you can continue at the comma. You don't need the backtick.

  • #110645

    Participant
    Points: 19
    Rank: Member
    $DataPath = "C:\temp85\o365UserData.csv"
    $Results = @()
    $MailboxUsers = get-EXOmailbox -resultsize 550
    
    foreach($user in $mailboxusers)
    {
    $UPN = $user.userprincipalname
    $License = Get-MsolUser -userprincipalname $UPN
    $MbxStats = Get-EXOMailboxStatistics $UPN
    
          $Properties = @{
          Name = $user.name
          UPN = $UPN
          Alias = $user.alias
    #     UsageLocation = $user.usagelocation
          License = $License.Licenses[0].AccountSkuId
    #     ArchiveStatus = $user.archivestatus
          LitigationHoldEnabled = $user.LitigationHoldEnabled
          LitigationHoldDate = $User.LitigationHoldDate
          LitigationHoldOwner = $User.LitigationHoldOwner
          LitigationHoldDuration = $User.LitigationHoldDuration
          RetentionPolicy = $user.RetentionPolicy
          RetentionHoldEnabled = $User.RetentionHoldEnabled
          ExchangeGuid = $user.ExchangeGuid
          SamAccountName = $User.SamAccountName
          Office = $user.Office
          InPlaceHolds = $User.InPlaceHolds
          IsInactiveMailbox = $User.IsInactiveMailbox
          IsSoftDeletedByRemove = $User.IsSoftDeletedByRemove
          IsSoftDeletedByDisable = $User.IsSoftDeletedByDisable
          AccountDisabled = $User.AccountDisabled
          OrganizationalUnit = $User.OrganizationalUnit
          DisplayName = $User.DisplayName
          PrimarySmtpAddress = $User.PrimarySmtpAddress
          RecipientTypeDetails = $User.RecipientTypeDetails
          WhenCreated = $User.WhenCreated
          WhenMailboxCreated = $User.WhenMailboxCreated
          WhenSoftDeleted = $User.WhenSoftDeleted
    #     ServerName = $MbxStats.servername
    #     DatabaseName = $MbxStats.databasename
          TotItemSize = $MbxStats.totalitemsize
          ItemCount = $MbxStats.ItemCount
          }
    
    $Results += New-Object psobject -Property $properties
    
    }
    
    $Results | Select-Object Name,DisplayName,License......,License | Export-Csv -notypeinformation -Path $DataPath
    • #110651

      Participant
      Points: 19
      Rank: Member

      Thank you Kvprasoon.  Sorry for not doing the code right, my first post.  Please see above.

      Your suggested code gave me this error:

      At C:\Temp85\t\users2.ps1:2 char:95
      + ... tionHoldEnabled, `
      +                    ~
      Missing argument in parameter list.
      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : MissingArgument

  • #110648
    js

    Participant
    Points: 326
    Helping Hand
    Rank: Contributor

    I'm having deja vu.

  • #110656

    Participant
    Points: 19
    Rank: Member

    Kvprasoon,

    Is it possible for you to save your code as .ps1 file and email it to me please?

  • #110659

    Participant
    Points: 19
    Rank: Member

    I added a coma (,) after the Alias and error went away.  But the last three lines data came blank!

    Any ideas about the last three lines: License, itemsize, & itemcount?

  • #110662
    js

    Participant
    Points: 326
    Helping Hand
    Rank: Contributor

    Hmm, I think it's alright? I don't have those commands.

    $MailboxUsers = Get-EXOmailbox -ResultSize 550 | Select-Object -Property Name,
    UPN, Alias, LitigationHoldEnabled, LitigationHoldDate,
    LitigationHoldOwner, LitigationHoldDuration, RetentionPolicy,
    RetentionHoldEnabled, ExchangeGuid, SamAccountName, Office,
    InPlaceHolds, IsInactiveMailbox, IsSoftDeletedByRemove,
    IsSoftDeletedByDisable, AccountDisabled, OrganizationalUnit,
    DisplayName, PrimarySmtpAddress, RecipientTypeDetails,
    WhenCreated, WhenMailboxCreated, WhenSoftDeleted,
    @{E={(Get-MsolUser -userprincipalname $_.UPN).Licenses[0].AccountSkuId};L='License'},
    @{E={(Get-EXOMailboxStatistics $_.UPN).totalitemsize};L='TotItemSize'},
    @{E={(Get-EXOMailboxStatistics $_.UPN).ItemCount};L='ItemCount'}
  • #110663

    Participant
    Points: 19
    Rank: Member

    Those three lines still came blank!

  • #110665

    Participant
    Points: 894
    Helping Hand
    Rank: Major Contributor

    There is no UPN property for Get-EXOmailbox output, its userprincipalname

    #Change
    @{E={(Get-MsolUser -userprincipalname $_.UPN).Licenses[0].AccountSkuId};L='License'},
    @{E={(Get-EXOMailboxStatistics $_.UPN).totalitemsize};L='TotItemSize'},
    @{E={(Get-EXOMailboxStatistics $_.UPN).ItemCount};L='ItemCount'}
    
    #To
    @{E={(Get-MsolUser -userprincipalname $_.userprincipalname ).Licenses[0].AccountSkuId};L='License'},
    @{E={(Get-EXOMailboxStatistics $_.userprincipalname ).totalitemsize};L='TotItemSize'},
    @{E={(Get-EXOMailboxStatistics $_.UPN).userprincipalname };L='ItemCount'}
    
  • #110666

    Participant
    Points: 19
    Rank: Member

    The script is working ok now but it took 18 minutes to finish 550 users vs 12 minutes before.  I read Get-MailboxStatistics is slow.  I have to run it fo 6K plus users.

    Thank you all for the help.

The topic ‘Get-Mailbox combine with Get-MailboxStatistics’ is closed to new replies.