Michael Leonard
asked on
need assistance with a powershell script to export out mailbox statistics for a number of users
Can someone provide a way for me to run this script but add the "import-csv" option so that i can run it against multiple accounts? [100+]
here is the script that works perfectly for a single mailbox to export out folder statistics:
Get-MailboxFolderStatistic s -id Joe_Smith | Select Identity,FolderSize,ItemsI nFolder,Ne westItemRe ceiveDate |Export-CSV c:\results.csv
thanks in advance!
Quest AD Cmdlet version would be fine as well
S.
here is the script that works perfectly for a single mailbox to export out folder statistics:
Get-MailboxFolderStatistic
thanks in advance!
Quest AD Cmdlet version would be fine as well
S.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Assume u have user in each line u can run the following:
gc users.csv | %{
Get-MailboxFolderStatistic s -id $_ | Select Identity,FolderSize,ItemsI nFolder,Ne westItemRe ceiveDate |Export-CSV c:\results.csv
}
gc users.csv | %{
Get-MailboxFolderStatistic
}
$accts = import-csv c:\accts.csv
$accts| foreach-object {do something
#for specific values
Get-MailboxFolderStatistic s -id $_.id | Select Identity,FolderSize,ItemsI nFolder,Ne westItemRe ceiveDate |Export-CSV c:\results.csv
}
$accts| foreach-object {do something
#for specific values
Get-MailboxFolderStatistic
}
$accts = import-csv c:\accts.csv
$outpath = c:\results.csv
$accts| foreach-object {do something
#for specific values
$mbinfo += Get-MailboxFolderStatistic s -id $_.id | Select Identity,FolderSize,ItemsI nFolder,Ne westItemRe ceiveDate
}
$mbinfo | Select * | Export-Csv $outpath -NoTypeInformation
$outpath = c:\results.csv
$accts| foreach-object {do something
#for specific values
$mbinfo += Get-MailboxFolderStatistic
}
$mbinfo | Select * | Export-Csv $outpath -NoTypeInformation
Export-Csv inside the foreach loop will cause the result file get overwritten for each user.. it should be placed outside the loop..
One approach would be to use custom objects to collect the information from each mailbox for later export. In this example, you'd have your list of users in a text file with a valid parameter (i.e. SamAccountName, DN, alias, etc.)
$folderStatsOutput = @()
Import-Csv c:\temp\users.txt | ForEach-Object {
$folderStatsObj = Get-MailboxFolderStatistics -id $_
$objTemp = New-Object PSObject -Property {
Identity = $folderStatsObj.Identity
FolderSize = "{0:0,0}"$folderStatsObj.FolderSize.ToKB()
ItemsInFolder = $folderStatsObj.ItemsInFolder
NewestItemReceiveDate = $folderStatsObj.NewestItemReceiveDate
}
$folderStatsOutput += $objTemp
}
$folderStatsOutput |
Select-Object Identity,FolderSize,ItemsInFolder,NewestItemReceivedDate |
Export-Csv C:\temp\folderstats_results.csv -NoType
Sorry..
# change this line
Import-Csv c:\temp\users.txt | ForEach-Object {
# to this
Get-Content c:\temp\users.txt | ForEach-Object {
$results = @()
gc users.csv | %{
$results += , @(Get-MailboxFolderStatist ics -id $_ | Select Identity,FolderSize,ItemsI nFolder,Ne westItemRe ceiveDate)
}
$results | export-csv c:\results.csv
gc users.csv | %{
$results += , @(Get-MailboxFolderStatist
}
$results | export-csv c:\results.csv
I can't test this at the moment but this was quick and dirty. If the hash array does not work let me know and I'll rework it when I get to my desk:
$list=@()
foreach($user in cat c:\scripts\userlist.txt)
{
$mbstat = Get-MailboxFolderStatistic s -id $user Select
$hash=@{
Identity = $mbstat Identity
Folder_Size = $mbstat FolderSize
Items = $mbstat ItemsInFolder
Newest_Item = $mbstat.NewestItemReceiveD ate
}
$obj = New-Object PSObject -Property $hash
$list += $obj
}
$list | export-csv C:\Scripts\MBInfo.csv
$list=@()
foreach($user in cat c:\scripts\userlist.txt)
{
$mbstat = Get-MailboxFolderStatistic
$hash=@{
Identity = $mbstat Identity
Folder_Size = $mbstat FolderSize
Items = $mbstat ItemsInFolder
Newest_Item = $mbstat.NewestItemReceiveD
}
$obj = New-Object PSObject -Property $hash
$list += $obj
}
$list | export-csv C:\Scripts\MBInfo.csv
ASKER
thanks all for the responses. Subsun you were first and this works perfectly. much appreciated.
S.
S.
$accts| foreach-object {do something}