Tuesday, March 11, 2014

Getting list of Outlook contacts using PowerShell Script.


Quote of the Day “We can't help everyone, but everyone can help someone. - Ronald Reagan

Greeting of the day.

Today I was working on a script, that script need to access the contact folder of the users default Outlook profile.

Managing Outlook and make it working using PowerShell is very easy, the only complicated part is to make the Outlook items working with it. You have to know, which code to use to access “Contacts”, and “Calendars” items or so on.

I have written a small script, which can retrieve a list of contacts from your default outlook profile. You can download the script from the below TechNet script gallery.

Once you have contact data in your hand, you can export it to CSV, HTML, or so, and I think, it is quick and easy way to back up your contacts {if you or your users still use POP3/IMAP with Outlook}.
Why I need this kind of script to retrieve contacts from the outlook, you may know the answer in my coming blog posts.



Screenshot

11-03-2014 13-16-23



The script is fairly simple and the only complicated part of the script is :
$contactObject  = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderContacts)

Rest of all is easy to understand. In this script,
1.    First we are adding the assemply for Microsoft Outlook.
2.    Then we are creating  com object of “Microsoft Application”
3.     Then we are getting the default MAPI namespace for outlook.
4.    Then we are binding contact folder in to “$contactObject
5.    After that we are using Foreach to iterate each entry in the “$contactObject
6.    You are saving the result in the in a custom object.

Script :

#==================| 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    : 11-03-2014
#            File    :          Playing with KeyBaord 
#            Purpose :  Accessing Outlook Contacts using PowerShell       
#            Version : 1 
#           
#
#            My Pet Spider :          /^(o.o)^\  
#========================================================================


BEGIN
       {
      
              # Try one or more commands
              try {
                    
                     #
                     Add-Type -assembly "Microsoft.Office.Interop.Outlook" -ErrorAction Stop -ErrorVariable "OutlookError"
                     $Outlook = New-Object -comobject Outlook.Application -ErrorAction stop -ErrorVariable "ApplicationError"
                     $namespace = $Outlook.GetNameSpace("MAPI")
                     $contactObject  = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderContacts)
                     $contactList = $contactObject.Items;
              }
             
              # Catch all other exceptions thrown by one of those commands
              catch {
             
                     $OutlookError
                     $ApplicationError
              }
              # Execute these commands even if there is an exception thrown from the try block
              finally {
              }
      
      
       }

PROCESS
       {
      

              foreach ( $name in $contactList ) {
             
             
                     $props = @{
                           #'Title' = $name.Title;
                           'FullName' = $name.FullName;
                           'FirstName' = $name.FirstName;
                           'Email Address' = $name.Email1Address;
                           'Mobile' = $name.MobileTelephoneNumber;
             
             
                     }     
               $object = New-Object -TypeName PsObject -Property $props
                     Write-Output $object
                     # You may find more contact field details from this link => : http://msdn.microsoft.com/en-us/library/ee160254(v=exchg.80).aspx

}
      
       }

END
       {
      

       }

# end of the script.


I hope you find it useful.
Regards
Aman Dhally
clip_image017 clip_image018 clip_image019 clip_image020 imageclip_image021

Thursday, March 6, 2014

Part-3: Azure and PowerShell: Create a New Website on Windows Azure using PowerShell.

 

Part-1 : Getting Started With Windows Azure And PowerShell

Part-2: Connect To Windows Azure Using PowerShell

 

Hi, in my previous post, I had shown to you how to connect with “Windows Azure” using “PowerShell”.

Now, it’s time to “Do” and create something.

What about creating a website?

Nice idea! Isn’t!

Let’s get started.

In my Azure web portal you can see, currently I have no website.

clip_image002

 

Before open and start working on our “PowerShell” console, let’s have a quick look on what are the options those are required to create a website using Windows Azure web portal.

 

clip_image004

 

You can see in above picture, when we are creating a new website using “Quick Create” option in “Windows Azure” it is asking for provide  “URL” name and select a “Region”.

That’s fine. Let’s do it in our “PowerShell” way.

Open PowerShell console.

First, to get the list of geographical locations, we can use the “Get-AzureLocation” cmdlet. 

 

Run “Get-AzureLocation” cmdlet and pipe the output to “Select-Object” and choose Name property to select.

 

Get-AzureLocation | Select Name

 

Now we know which “Location” name has to use while creating a website. I live in India, to me it seems the “Southeast Asia” is a proper option.

clip_image005

 

Now the next step is to create a website.

For creating a website, use “New-AzureWebsite” cmdlet.

We are creating a website, using location “Southeast Asia’” and with a website name ‘ADhally’. 

New-AzureWebsite -Location ‘Southeast Asia’ -Name ‘ADhally’ 

 

clip_image007

 

When I run the above command, it throw the “EndpoingNotFoundException” error. I was wondering why it is not able to find endpoint.

To solve my curiosity, I checked the geographical locations available while creating website in the Azure web portal. And you can see there are no option of “SouthEast Asia” is listed.

clip_image008 

 

Ohk!

Now for me the closest option to “Southeast Asia” seems to be “East Asia” {because it has Asia word in it !! HA HA !) , let’s try that.

New-AzureWebsite -Location "East Asia" -Name "ADhally" 

 

It work well as expected, without throwing any error.

 

clip_image010

 

In hostname property it has given us the URL link to our newly created website.

Now’ let’s try opening a website which we have just created adhally.azurewebsites.net

 

clip_image012

 

Here you go, website is created successfully.

On our Azure web portal you able to see this newly website too J

 

clip_image014

 

To get the list of all of your website using PowerShell, you can use the “Get-AzureWebsite” cmdlet.

 

clip_image015

 

How to Video

Create a Website on Windows Azure using PowerShell.

 

That’s all for now J  see you in my next blog post.

Regards

Aman

clip_image017 clip_image018 clip_image019 clip_image020  clip_image021