This is a question i recieved via email this week and its typical of the kind of thing im talking about.
How do i export the list for Exchange Users from Exchange Server 2007 SP1 by using a cmdlet:
I want something like the following table
Example:
Department , Alias , DefaultQuota ,OverQuota ,TotalitemSize, LastAccessTime
------------------------------------------------------------------------------------------------
ITD Glen 50 MB 55 MB 55 MB March3rd 2008
Now it would be nice if there was a simple Oneliner powershell cmdlet that could return this information but there isn't. What you need to do is combine a number of different cmdlet's to get the information you want. And this is really where the Power in Powershell comes in because it allows you to easily integrate the result of different operations. So to get this information we need to combine get-mailbox, get-mailboxstatitics and to get information about what department the users is in straight ADSI is the best way to go. To be able to use the Export cmdlets that come with Powerhell its best to store the result of your combination in a custom object and then when it comes time to produce the report you can the use something like export-csv or converttohtml . So the following script is one method of solving the above question it reuses a cut down version of the mailbox size gui script that been tweaked to show the required information and produces a CSV file at the end. The script has one variable that is hardcoded which is the name of the server you want to run the script against which you will need to change. I've put a download of this script here the script it self looks like
$snServerName = "servername"
$fname = "c:\mbreport.csv"
$usrquotas = @{ }
$mstoresquotas = @{ }
$mbcombCollection = @()
get-mailboxdatabase -server $snServerName | ForEach-Object{
if ($_.ProhibitSendReceiveQuota.IsUnlimited -ne $true){
$mstoresquotas.add($_.identity,$_.ProhibitSendReceiveQuota)
}
}
$usrquotas = @{ }
Get-Mailbox -server $snServerName -ResultSize Unlimited | foreach-object{
if($_.ProhibitSendReceiveQuota.IsUnlimited -ne $true){
$usrquotas.add($_.ExchangeGuid,$_.ProhibitSendReceiveQuota)
}
}
$quQuotaval = 0
get-mailboxstatistics -Server $snServerName | Where {$_.DisconnectDate -eq $null} | ForEach-Object{
$quQuota = "0"
if ($usrquotas.ContainsKey($_.MailboxGUID)){
if ($usrquotas[$_.MailboxGUID].Value -ne $null){
$quQuotaval = $usrquotas[$_.MailboxGUID].Value.ToMB()
$quQuota = "{0:P0}" -f ($_.TotalItemSize.Value.ToMB()/$usrquotas[$_.MailboxGUID].Value.ToMB())}
}
else{
if ($mstoresquotas.ContainsKey($_.database)){
if ($mstoresquotas[$_.database].Value -ne $null){
$quQuotaval = $mstoresquotas[$_.database].Value.ToMB()
$quQuota = "{0:P0}" -f ($_.TotalItemSize.Value.ToMB()/$mstoresquotas[$_.database].Value.ToMB())}}
}
$icount = 0
$tisize = 0
$disize = 0
if ($_.DisplayName -ne $null){$dname = $_.DisplayName}
if ($_.ItemCount -ne $null){$icount = $_.ItemCount}
if ($_.TotalItemSize.Value.ToMB() -ne $null){$tisize = $_.TotalItemSize.Value.ToMB()}
if ($_.TotalDeletedItemSize.Value.ToMB() -ne $null){$disize = $_.TotalDeletedItemSize.Value.ToMB()}
$mbcomb = "" | select DisplayName,QuotaSize,TotalItemSize,Department,LastLogonTime
$mbcomb.DisplayName = $dname
$mbcomb.QuotaSize = $quQuotaval
$mbcomb.TotalItemSize = $tisize
$usrString = 'LDAP://' + $_.identity
$usr = [ADSI]$usrString
$mbcomb.Department = $usr.Department
$mbcomb.LastLogonTime = $_.LastLogonTime
$mbcombCollection += $mbcomb
}
$mbcombCollection | export-csv -noTypeInformation $fname
6 comments:
Nice script Glenn. I like your use of ADSI, I have a similar script that runs that uses dsquery/dsget that is a little easier to read, but requires the admin tools installed. This is a great way to break away from that requirement.
Attempted to divide by zero.
At line:12 char:55
+ $quQuota = "{0:P0}" -f ($_.TotalItemSize.Value.ToMB()/$ <<<< usrquotas[$_.Mai
lboxGUID].Value.ToMB())}
I get this error message when I run the script, any ideas?
This happens if you have userquotas that are less the 1MB so you need to check for this eg
if ($usrquotas[$_.MailboxGUID].Value.ToMB() -gt 0){
$quQuota = "{0:P0}" -f ($_.TotalItemSize.Value.ToMB()/$usrquotas[$_.MailboxGUID].Value.ToMB())}
else{
$quQuota = "100"
}
}
Glen,
I am getting the following error message when I run the comprep.ps1. I have even did the "unblock" from the properties of this file in Windows Explorer. Thanks for helping.
File C:\scripts\comprep.ps1 cannot be loaded. The file C:\scripts\comprep.ps1
s not digitally signed. The script will not execute on the system. Please see
get-help about_signing" for more details..
At line:1 char:13
+ .\comprep.ps1 <<<<
I managed to run the script by changing the Executionpolicy set to "RemoteSigned" - but now another error message appears as below:
Get-MailboxDatabase : Exchange server "servername" was not found. Please make s
ure you have typed it correctly.
At C:\scripts\comprep.ps1:7 char:20
+ get-mailboxdatabase <<<< -server $snServerName | ForEach-Object{
Get-Mailbox : Exchange server "servername" was not found. Please make sure you
have typed it correctly.
At C:\scripts\comprep.ps1:15 char:12
+ Get-Mailbox <<<< -server $snServerName -ResultSize Unlimited | foreach-objec
t{
Get-MailboxStatistics : Exchange server "servername" was not found. Please make
sure you have typed it correctly.
At C:\scripts\comprep.ps1:21 char:22
+ get-mailboxstatistics <<<< -Server $snServerName | Where {$_.DisconnectDate
-eq $null} | ForEach-Object{
Try setting the servername you want the script to run against in the $servername varible
$snServerName = "servername"
Cheers
Glen
Post a Comment