Freigeben über


O365 and PowerShell Part III (Gotchas and proposed solution)

Gotcha

The thing that occurred to me as I  was connecting to each environment was the nuances, slight differences of connecting to each one.  At the end of my preparation I had 4 different consoles going and an Integrated Scripting window running. 

 

More detail on the problem

Connecting to Exchange online

  1. Open a PowerShell Command Prompt as an Administrator
  2. Create a credential

$Cred = New-Object  PSCredential “UserName@o365domain.onmicrosoft.com”, (ConvertTo-SecureString “<Password>” –AsPlainText –Force)

  1. Create an Exchange Online Session

$exchangeSession = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri https://ps.outlook.com/powershell -AllowRedirect -Authentication Basic -Credential $Cred

  1. Import the newly created Exchange session

Import-PSSession $exchangeSession

What this whole process does is loads up in memory the Exchange online cmdlets to the current PowerShell host as well as creates a persistent connection to Exchange online so you can act upon your instance remotely.

Connecting to SharePoint Online

  1. Open the SharePoint  Online Management Shell as an Administrator
  2. Create a credential

$Cred = New-Object  PSCredential “UserName@o365domain.onmicrosoft.com”, (ConvertTo-SecureString “<Password>” –AsPlainText –Force)

  1. Connect to the SharePoint online administration site

Connect-SPOService –Url https://<o365domain>-admin.sharepoint.com –credential $Cred

This whole process does a similar thing as the Exchange Online, however the PowerShell cmdlets are already loaded in the session and available within the management console. 

Connecting to Lync Online

  1. Open a PowerShell Command Prompt as an Administrator
  2. Create a credential

$Cred = New-Object  PSCredential “UserName@o365domain.onmicrosoft.com”, (ConvertTo-SecureString “<Password>” –AsPlainText –Force)

  1. Import the Lync Online connector module

Import-Module LyncOnlineConnector

  1. Create an Lync Online Session

$lyncSession = New-CsOnlineSession –Credential $Cred

Import-PSSession $lyncSession

NOTE: I did have problems at one point connecting to the session, I may not have cleaned up after myself, clearing out the session and the credentials got stale.  This was remedied by rebooting the machine. 

Connecting to O365

  1. Open the Windows Azure Active Directory Module for Windows PowerShell as an Administrator
  2. Create a credential

$Cred = New-Object  PSCredential “UserName@o365domain.onmicrosoft.com”, (ConvertTo-SecureString “<Password>” –AsPlainText –Force)

  1. Connect to the O365 Service

Connect-MSolService –Credential $Cred

Summary

Four different products to connect to, four slightly different ways to connect.  This is what I call nuance and if I have to do something in a hurry I am less apt to do it through a scripting environment.

A Possible Solution

To ease my pain I setup my profile for the current user all hosts with some commands to get me going. Below is the script and steps to get it done

  1.  Open PowerShell ISE as an Administrator, this may not be required, I do this out of habit.
  2. Create a new directory

New-Item –ItemType Directory –Path $env:UserProfile\Document\WindowsPowershell

  1. Create a new Profile.ps1 file

New-Item –ItemType File –Path $env:UserProfile\Document\WindowsPowershell\Profile.ps1

  1. Open the newly created profile in the ISE and paste the next set of scripts, changing the <Password> and <O365Domain> to your respective domains and passwords.

 #region Setup Credentials 

        $password = ConvertTo-SecureString "<Password>" -AsPlainText -Force

    # Read-host -prompt "What is the password" -AsSecureString

        $Cred = New-Object pscredential "admin@<o365Domain>.onmicrosoft.com", $password # (ConvertTo-SecureString "pass@word1" -AsPlainText -Force)

#endregion

 

#region connections

function Connect-O365Plus {

   

    #region Setup for O365

        Import-Module msonline

        Connect-MsolService -Credential $Cred

    #endregion

 

    #region Setup for SharePoint Online

        Import-Module Microsoft.Online.SharePoint.Powershell

        Connect-SPOService -Url https://<o365Domain>-admin.sharepoint.com -Credential $o365Cred

    #endregion

 

    #region Setup for Lync Online

        Import-Module LyncOnlineConnector

        $lyncSession = New-CsOnlineSession -Credential $Cred

        Import-PSSession $lyncSession

    #endregion

 

    #region Setup for Exchange Online

        $exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Cred -Authentication Basic -AllowRedirection

        Import-PSSession $exchangeSession

    #endregion

}

 

function Connect-LyncOnline  {

    Import-Module LyncOnlineConnector

    $lyncSession = New-CsOnlineSession -Credential $Cred

    Import-PSSession $lyncSession

   

}

 

function Connect-ExchangeOnline {

 

    #region Setup for Exchange Online

        $exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange `

                    -ConnectionUri https://ps.outlook.com/powershell `

                    -Credential $Cred -Authentication Basic -AllowRedirection

        Import-PSSession $exchangeSession

    #endregion 

}

 

function Connect-SharePointOnline {

  #region Setup for SharePoint Online

        Import-Module Microsoft.Online.SharePoint.Powershell

        Connect-SPOService -Url https://marcusfdemo-admin.sharepoint.com -Credential $Cred

    #endregion

}

 

function Connect-O365 {

    #region Setup for O365

        Import-Module msonline

        Connect-MsolService -Credential $Cred

    #endregion

}

#endregion 

  1. Save and close the ISE and now when you open up a PowerShell console as an administrator you can connect to SharePoint Online (Connect-SharePointOnline), Exchange Online (Connect-ExchangeOnline), Lync Online (Connect-LyncOnline), O365 (Connect-O365), or the entire suite (Connect-O365Plus).  Without having to recall the nuances. 

Hope everyone enjoys and let me know if you have any feedback. 

References

Connect to Office 365 by using a single Windows PowerShell window:  https://technet.microsoft.com/en-US/library/dn568015.aspx