4 minute read

I just started to play with a Microsoft Office 365 environment (Azure Active Directory, Lync Online and Exchange Online) and I thought I would make it through PowerShell obviously :-)

But when you start your PowerShell console… you need to load modules, connect to each services, enter your credentials…yada yada yada…

With Office 365 you can administer the following services using PowerShell:

    * Azure Active Directory * Exchange Online PowerShell * SharePoint Online PowerShell * Lync Online PowerShell

Note: However I was not able to test the SharePoint part, so this is not included in the function below yet.

Here is a very handy function that you can include to your PowerShell Profil to connect to all the service at once.

Requirements

    * Azure Active DirectoryDownload * Exchange Online PowerShell(no download needed, the function will create an implicit remoting module) * SharePoint Online PowerShell(no download needed, the function will create an implicit remoting module) * Lync Online PowerShellDownload

PowerShell Function Connect-Office365

function Connect-Office365
{
<#
.SYNOPSIS
    This function will prompt for credentials, load module MSOLservice,
    load implicit modules for Office 365 Services (AD, Lync, Exchange) using PSSession.
.DESCRIPTION
    This function will prompt for credentials, load module MSOLservice,
    load implicit modules for Office 365 Services (AD, Lync, Exchange) using PSSession.
.EXAMPLE
    Connect-Office365
   
    This will prompt for your credentials and connect to the Office365 services
.EXAMPLE
    Connect-Office365 -verbose
   
    This will prompt for your credentials and connect to the Office365 services.
    Additionally you will see verbose messages on the screen to follow what is happening in the background
.NOTES
    Francois-Xavier Cat
    lazywinadmin.com
    @lazywinadmin
#>
    [CmdletBinding()]
    PARAM ()
    BEGIN
    {
        TRY
        {
            #Modules
            IF (-not (<span style="color: blue; font-size: 12px; font-weight: bold;">Get-Module -Name MSOnline -ListAvailable))
            {
                <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Verbose -Message "BEGIN - Import module Azure Active Directory"
                <span style="color: blue; font-size: 12px; font-weight: bold;">Import-Module -Name MSOnline -ErrorAction Stop -ErrorVariable ErrorBeginIpmoMSOnline
            }
            
            IF (-not (<span style="color: blue; font-size: 12px; font-weight: bold;">Get-Module -Name LyncOnlineConnector -ListAvailable))
            {
                <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Verbose -Message "BEGIN - Import module Lync Online"
                <span style="color: blue; font-size: 12px; font-weight: bold;">Import-Module -Name LyncOnlineConnector -ErrorAction Stop -ErrorVariable ErrorBeginIpmoLyncOnline
            }
        }
        CATCH
        {
            <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message "BEGIN - Something went wrong!"
            IF ($ErrorBeginIpmoMSOnline)
            {
                <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message "BEGIN - Error while importing MSOnline module"
            }
            IF ($ErrorBeginIpmoLyncOnline)
            {
                <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message "BEGIN - Error while importing LyncOnlineConnector module"
            }
            
            <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message $error[0].exception.message
        }
    }
    PROCESS
    {
        TRY
        {
            
            # CREDENTIAL
            <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Verbose -Message "PROCESS - Ask for Office365 Credential"
            $O365cred = <span style="color: blue; font-size: 12px; font-weight: bold;">Get-Credential -ErrorAction Stop -ErrorVariable ErrorCredential
            
            # AZURE ACTIVE DIRECTORY (MSOnline)
            <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Verbose -Message "PROCESS - Connect to Azure Active Directory"
            Connect-MsolService -Credential $O365cred -ErrorAction Stop -ErrorVariable ErrorConnectMSOL
            
            # EXCHANGE ONLINE (Implicit Remoting module)
            <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Verbose -Message "PROCESS - Create session to Exchange online"
            $ExchangeURL = "https://ps.outlook.com/powershell/"
            $O365PS = <span style="color: blue; font-size: 12px; font-weight: bold;">New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ExchangeURL -Credential $O365cred -Authentication Basic -AllowRedirection -ErrorAction Stop -ErrorVariable ErrorConnectExchange
            
            <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Verbose -Message "PROCESS - Open session to Exchange online (Prefix: Cloud)"
            <span style="color: blue; font-size: 12px; font-weight: bold;">Import-PSSession -Session $O365PS –Prefix ExchCloud
            
            # LYNC ONLINE (LyncOnlineConnector)
            <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Verbose -Message "PROCESS - Create session to Lync online"
            $lyncsession = New-CsOnlineSession –Credential $O365cred -ErrorAction Stop -ErrorVariable ErrorConnectExchange
            <span style="color: blue; font-size: 12px; font-weight: bold;">Import-PSSession -Session $lyncsession -Prefix LyncCloud
            
            # SHAREPOINT ONLINE (Implicit Remoting module)
            #Connect-SPOService -Url https://contoso-admin.sharepoint.com –credential $O365cred
        }
        CATCH
        {
            <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message "PROCESS - Something went wrong!"
            IF ($ErrorCredential)
            {
                <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message "PROCESS - Error while gathering credential"
            }
            IF ($ErrorConnectMSOL)
            {
                <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message "PROCESS - Error while connecting to Azure AD"
            }
            IF ($ErrorConnectExchange)
            {
                <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message "PROCESS - Error while connecting to Exchange Online"
            }
            IF ($ErrorConnectLync)
            {
                <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message "PROCESS - Error while connecting to Lync Online"
            }
            
            <span style="color: blue; font-size: 12px; font-weight: bold;">Write-Warning -Message $error[0].exception.message
        }
    }
}

Running the function

```


Here the result in action

<a href="http://3.bp.blogspot.com/-p6i5LnXDInE/U7R69oyeIwI/AAAAAAABl0o/KA052Ymv5Js/s1600/7-2-2014+5-34-23+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-p6i5LnXDInE/U7R69oyeIwI/AAAAAAABl0o/KA052Ymv5Js/s1600/7-2-2014+5-34-23+PM.png" /></a>





<b><u>Adding the function to your PowerShell profile</u></b>

<a href="https://lazywinadmin.github.io/images/2014/20140701_PowerShell_-_Handy_function_to_connect_to_Office365_services/2014-07-01_21-13-55__1698217232__-772x122.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://lazywinadmin.github.io/images/2014/20140701_PowerShell_-_Handy_function_to_connect_to_Office365_services/2014-07-01_21-13-55__1698217232__-772x122.png" /></a>```

The next time you reload your PowerShell, the function Connect-Office365 will be available to your PowerShell.

Finally we can see two implicit modules created for Lync and Exchange with a sample of cmdlets available. Those cmdlets contains the prefix we defined in the function ExchCloud and LyncCloud.

Implicit remoting modules loaded start by a "tmp". You can see a sample of the Cmdlets available with the prefix we included.

Download

Space for improvements

    * Check if PSsession already opened ? Same credential used? * Parameters
      * [Switch]$AzureAD, * [Switch]$LyncOnline, * [Switch]$ExchangeOnline

Leave a comment