Showing posts with label Scripting Techniques. Show all posts
Showing posts with label Scripting Techniques. Show all posts

Wednesday, June 11, 2014

PowerShell Script : Office 365 Mailbox Sizes and Mailbox Quotas Email Reporting.

 

Picture1

We all love office 365.

Especially I like the Exchange Online most.

On Exchange Online when we create any new mailboxes we do set the various storage limits on the user mailbox and we assign different mailbox size as per the user requirements or company policies.

Sometime, we need to know the reports of:

1.     How much mailbox space is assigned to the user?

2.     How much his currently mail box usage is.

3.     What is the name of mailbox database?

Monitoring and Reporting are the two key main areas in our IT industry. We should monitor our IT stuff and we should often generate and view the reports.

I have created this script to know about the mailbox status of my all Office 365 Exchange Online mailboxes and the current usage of them.  It also contains the information on, Users Last Login time , Log Off time, size of this issue warning quota limit and size of total deleted items.

How to run the script?

It’s simple.

1.     Configure, your SMTP server, TO, FROM, Subject filed in the script.

2.     Run the script

3.     It ask you for your Office 365 Administrator Credentials.

4.     After providing credential, it will send an HTML email to the provided TO email id.

That’s all J Simple. Isnt?

 

You can download the script from Technet Gallery : http://gallery.technet.microsoft.com/scriptcenter/Office-365-Mailbox-Sizes-92b94563

 

#==================| Satnaam Waheguru Ji |===============================   
#
# Author : Aman Dhally
# E-Mail : amandhally@gmail.com
# website : www.amandhally.net
# twitter : @AmanDhally
# blog : http://newdelhipowershellusergroup.blogspot.in/
# facebook: http://www.facebook.com/groups/254997707860848/
# Linkedin: http://www.linkedin.com/profile/view?id=23651495
#
# Creation Date : 03-June-2014
# File : Email Reporting on Office 365 mail Statictic
#
# Version : 3
#
#
# My Pet Spider : /^(o.o)^\
#

#------------------------------------------------------------------------------------------------------------


function Send-O365MailStats {



begin {

try {
Write-Output "$(get-date) : Script Start."
Write-Output "$(get-date) : Asking for Office365 Administrative Credentials."
$UserCredential = Get-Credential
Write-Output "$(get-date) : Creating a Online PS Session with Office 365."
$ouSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection -ErrorAction 'Stop' -ErrorVariable 'ConnectionError'
Write-Output "$(get-date) : Importing PS Session."
Import-PSSession $ouSession -AllowClobber -ErrorAction 'Stop' -ErrorVariable 'SessionError'
}

catch {
$ConnectionError
$SessionError

}
}


process {

try {
#Your SMTP sever
$smtp = 'YOUR EMAIL SERVER'
# To E-Mail Address
$to = 'AMANdhally@gmail.com'
# From E-Mail Address
$from = 'Scripter@YourDOMAIN.COM'
# Subject of the Email
$subject = 'Office 365 MailBox Stats Report'
#Empty Array String
$body2 = @()
#Just a Counter
$int = 1
#All Magic Done here
Write-Output "$(get-date) : Running the Get-Mailbox and Get-MailboxStatistics cmdlet"
$userList = Get-Mailbox -Filter " RecipientType -eq 'UserMailbox'" | Get-MailboxStatistics

#Processing
Write-Output "$(get-date) : Processing the Results."
foreach ( $user in $userList)
{
#$user | fl * -Force

$body2 += "<tr>"
$body2 += "<td>" + $int++ + "</td>"
$body2 += "<td>" + $user.DisplayName + "</td>"

#manuplating
$UserTa= $user.TotalItemSize.value.ToString().split(" ")[0]
$UserTb = $user.TotalItemSize.value.ToString().split(" ")[1]
$userTotalSize = $UserTa + " " + $UserTb

$body2 += "<td>" + $userTotalSize + "</td>"

$body2 += "<td>" + $user.LastLogonTime + "</td>"
$body2 += "<td>" + $user.LastLogoffTime + "</td>"
$body2 += "<td>" + $user.ServerName + "</td>"
$body2 += "<td>" + $user.DatabaseName + "</td>"

#Manuplating
$userDDLa = $user.TotalDeletedItemSize.value.ToString().split(" ")[0]
$userDDLb = $user.TotalDeletedItemSize.value.ToString().split(" ")[1]
$userDelted = $userDDLa + " " + $userDDLb

$body2 += "<td>" + $userDelted + "</td>"

#Manuplated
$userDBa = $user.DatabaseProhibitSendReceiveQuota.value.ToString().split(" ")[0]
$userDBb = $user.DatabaseProhibitSendReceiveQuota.value.ToString().split(" ")[1]
$userDBQuota = $userDBa + " " + $userDBb

$body2 += "<td>" + $userDBQuota + "</td>"

#Manuplating
$userWQa = $user.DatabaseIssueWarningQuota.value.ToString().split(" ")[0]
$userWQb = $user.DatabaseIssueWarningQuota.value.ToString().split(" ")[1]
$userWquota = $userWQa + " " + $userWQb

$body2 += "<td>" + $userWquota+ "</td>"

#Manuplating
$UserDBPa = $user.DatabaseProhibitSendQuota.value.ToString().split(" ")[0]
$UserDBPb = $user.DatabaseProhibitSendQuota.value.ToString().split(" ")[1]
$userDBPS = $UserDBPa + " " + $UserDBPb

$body2 += "<td>" + $userDBPS + "</td>"
$body2 += "</tr>"


}

$body = "<h3>Office 365 {Exchnage Online}, User Mailbox Statics Report.</h3>"
$body += "<br>"
$body += "<br>"
$body += "<table border=2 style=background-color:silver;border-color:black >"
$body += "<tr>"
$body += "<th>S. No</th>"
$body += "<th>Display Name</th>"
$body += "<th>Total Mailbox Size </th>"
$body += "<th>Last LogonTime</th>"
$body += "<th>Last Logoff</th>"
$body += "<th>Server Name </th>"
$body += "<th>Database Name</th>"
$body += "<th>Deleted Item Size</th>"
$body += "<th>Prohibit S/R Quota</th>"
$body += "<th>Issue Warning Quota </th>"
$body += "<th>Prohibit Send Quota</th>"
$body += "</tr>"
$body += $body2
$body += "</table>"

#Sending Email Message to the $to
Write-Output "$(get-date) : Sending the Email."
Send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -UseSsl

}

catch
{

}


}


end {

try {

#Remvoing the Session with Office 365
Write-Output "$(get-date) : Removing PS Session."
Remove-PSSession $ouSession
Write-Output "$(get-date) : Script End."
}

catch {
}
}
}

Send-O365MailStats

# end of the script.


The output should be look like the below screenshot.


09-06-2014 12-57-09


You can download the script from Technet Gallery : http://gallery.technet.microsoft.com/scriptcenter/Office-365-Mailbox-Sizes-92b94563



Regards



Aman Dhally


Come and join my journey of : “100 Days of Self Improvement” on


 


Facebook: https://www.facebook.com/100DoSI 



If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.


Saturday, September 14, 2013

PowerShell Techniques : Alias v/s Full Cmdlets usage.

 

Hi Everyone,

I hope you are enjoying your weekend.

This is my another post on Powershell techniques.

We all love Powershell alias's they are good and they handy and saved lots of key strokes.  I use Powershell alias a lot. But i have seen few Powershell scripter and they do use Powershell alias in their scripts. 

In Console

1 (fig. 1)

2(fig. 2)

You can see when we use alias in Powershell Console, it use less space and less key strokes,

Good, we know Alias are good so?

In Powershell scripts i don’t prefer to user Powershell alias. Powershell scripts are used by other too , we share the script , so if someone want to change anything or append anything then it is bit difficult  and intercept the code.

So for easy reading, easy interception using full cmdlets are very useful to use in the scripts.

3

Note :- This is my personal opinion and view. 

Happy Scripting ;)

That’s all for now.

Thanks

Aman Dhally

clip_image001 clip_image002 clip_image003 clip_image005clip_image007

Thursday, September 12, 2013

PowerShell Techniques : Commenting Scripts, it’s always helpful.

 

Hi,

I hope you are enjoying my Powershell Techniques series of web post. I always mention in most of my blog posts that, learning or memorizing what cmdlets do is really easy , but it is bit difficult to find techniques.

In my opinion these tips and trick or we can say techniques are universal, these are more then a logic and can apply anywhere and in any scripting language.

When you write Powershell script, it is always handy to add comments on them, as we know, in Powershell we use hash(#) character to add comments.

Why it is important to add comments?

In my point of view it is very important, because:

  1. If you are writing a long script, you will know what you are doing.
  2. If you want to share the script with one else,, they can also understand the script by reading comments.
  3. We can define a separate section for variable,function using comments.
  4. if we are using lots of foreach,if, loops, it is easy to identify ending of each loops.

 

In my scripts I use comments as separator. You can see that, I define a separate space for the Variable, Modules, Functions, So when next time, if i want to add of troubleshoot any functions, at that time i know where to look for them,

12-09-2013 23-09-27

It is always good to add comments above your code, so that you know what that code is doing.,This helps you and helps other to understand your scripts.

12-09-2013 23-16-52

If you are using multiple if/else or other loop and statements, then you can also use #comments in the end of } braces, you that you know which logic is applying on it.

image

That’s all for now.

Thanks

Aman Dhally

clip_image001 clip_image002 clip_image003 clip_image005clip_image007

Monday, July 29, 2013

Powershell Scripting Techniques : Using While in the Scripts to wait for Network Connection to be Establish.

Hi,
In any scripting language, the one of the  important things are techniques.
You can call it scripting techniques.
Sometime when you are writing scripts, there are some  certain problems come , and to solve them we need to add a special logics and special techniques to handle them.
Same thing was happen to me, i was writing a script and that script has to run every time users logon, and that script require a server to be available before running the script.
Flow Chart.
The logic is something like below flowchart.
Flow
How i solve it.
I solved it using WHILE  script code.
I have added Comments to the below script code, which make it self explanatory.
# Setting bit as False First
$bit = $false
# running while.
while ( $bit -eq $false ) 
        
        {
        # just a notification
        Write-Host 'Checking Internet Connection.' -ForegroundColor 'Yellow'
        
        # Testing Network Connection and storing it in to a vraible
        # when we use -QUIET Parameter with Test-Connection, it stored the value in 
        # true or false
        $testInternetConnectivity = Test-Connection -Count 3 "www.google.com" -Quiet    
        
        # Test-Connection CMDLET return output in TRUE and False
        # we are assigning the output of $testInternetConnectivity to the $bit Variable
        $bit =     $testInternetConnectivity
        
        #if the $bit is false then 
        if ( $testInternetConnectivity -ne $true) 
            {
            # show notifications
            Write-Host 'Please check your connection, I am not able to Ping www.google.com.'
            # just adding a new blank line
            "`n" 





#sleeping for 10 Seconds

Start-Sleep 10
} #end of IF


}
# end of while
# if the $bit is $true , then we are good to go.
if ( $bit -eq $true )
{
# notifications
"`n"
Write-Host 'Internet is Pingable, we are good to go.' -ForegroundColor 'Green'

}
#end of IF

# end of the script




You can also download the script code from this link :

Download Link : http://gallery.technet.microsoft.com/scriptcenter/Keep-Testing-Internet-f5c29243

Screenshots

I run the script and it found the “google.com” and give us an notification that Internet is Pingable.

29-07-2013 14-50-40

I have disconnected my laptop from internet and run the script, you can check it is continuously running.

29-07-2013 14-51-01

The internet is still disconnected and i run the script and it is showing that Internet connection is not there and in the middle I plugged in the LAN and you can see it found that google.com is working and  script is stopped.

29-07-2013 14-53-07



What i want to accomplished?

My “Main” script run when users login to the laptop. This script copy a .xls file everyday to a server before user start working on a laptop.

The problem was, sometime user not plugged in the LAN cable and sometime they are not connected to WIFI. so most of the time script fails.

So , i want to write a code , by which this script always run until it found a backup server.

To accomplish it i wrote above script code.

Description

I am setting $bit variable to False first.

$bit = $false

Now I am running the lope that, util $bit is equal to false, keep try to ping google.com



while ( $bit -eq $false ) {

              Write-Host 'Checking Internet Connection.' -ForegroundColor 'Yellow'Now I am testing the connection with www.google.com and storing the result in to variable

$testInternetConnectivity = Test-Connection -Count 3 "www.google.com" –Quiet

Now , I am updating the $bit varable with the value in $testInternetConnectivity

$bit =        $testInternetConnectivity        

if ( $testInternetConnectivity -ne $true)

{

Write-Host 'Please check your connection, I am not able to Ping www.google.com.'

                     }

}



When $bit variable became to true the below script code will run.



if ( $bit -eq $true ) {


"`n”          

Write-Host 'Internet is Pingable, we are good to go.'  -ForegroundColor 'Green }



---------------------------------------

The only confusing bit in the script code is , how are storing the Test-Connection to “google.com” and true or false.

and the trick is when we use –Quiet parameter with Test-Connection cmdlet it give the output in true and false.

Please see the below screenshots are everything will be clear.

In below screenshot, i am storing the output of Test-Connection in to variable , without –Quiet parameter and it is give me full details of Source,Destination, IP address rather then giving me result in True or false.

29-07-2013 14-56-36

Now, i added the –Quiet Parameter and you can see the result is shown is True now.

29-07-2013 14-56-59

Same here.

29-07-2013 14-57-50

29-07-2013 15-05-41

Download Link : http://gallery.technet.microsoft.com/scriptcenter/Keep-Testing-Internet-f5c29243

I hope that this trick will help you.

Thanks

Aman Dhally




clip_image001 clip_image002 clip_image003 clip_image005clip_image007