Author Posts

August 30, 2018 at 7:55 pm

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

August 31, 2018 at 1:42 pm

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

August 31, 2018 at 2:03 pm

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'}

August 31, 2018 at 2:34 pm

$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

August 31, 2018 at 2:39 pm

I'm having deja vu.

August 31, 2018 at 2:41 pm

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

August 31, 2018 at 2:43 pm

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

August 31, 2018 at 2:43 pm

Kvprasoon,

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

August 31, 2018 at 3:10 pm

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?

August 31, 2018 at 3:24 pm

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'}

August 31, 2018 at 3:53 pm

Those three lines still came blank!

August 31, 2018 at 3:59 pm

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'}

August 31, 2018 at 5:00 pm

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.