Skip to main content

More fun with combining mutliple Powershell cmdlets to produce reports

Over the years one of the most popular and useful scripts i've written and gotten feedback and questions on is the various mailbox size scripts i've posted here and on Outlookexchange. The fact that this functionality has been pulled from the GUI in Exchange 2007 doesn't appear to have been a very popular decision amongst sysadmins i guess the majority of which don't want to look at a line of code or script. While this blog/post isn't probably for those people If you are really ruing the loss of Mailbox size information out of the GUI don't think about what you have lost but whats been gained through the flexibility of Powershell and the Exchange Cmdlets. GUI's are great for displaying simple information but anyone who's been working in IT for any length of time knows that requests are rarely simple and not always logical when dealing with management. Trying to make information you export from a GUI display the way you want can be time consuming and tedious and really tie up time that could be better spent elsewhere. While the unavoidable reality may be yes you need learn how to script and really these day when i say that I'm not talking about VBS which like the Model T Ford has had its day. The main point is you see that thing in fount of you with the flashing cursor and the keyboard designed to slow you down its main purpose in life is to save you time and make your life easier well isn't it about time it started to ? .

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

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why ...
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.