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
Hi, I request you to format the code in the forum which makes other to easily understand your code, below link will help you.
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'}
$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
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
Missing argument in parameter list.
CategoryInfo : ParserError: ( , ParentContainsErrorRecordException
FullyQualifiedErrorId : MissingArgument
js2010
August 31, 2018, 9:13am
7
Kvprasoon, you can continue at the comma. You don’t need the backtick.
Kvprasoon,
Is it possible for you to save your code as .ps1 file and email it to me please?
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?
js2010
August 31, 2018, 9:54am
10
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'}
Those three lines still came blank!
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'}
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.