Here are my notes on the steps to connect to Office 365 and/or Exchange hybrid for the management using PowerShell. Some of this is very common information but the one uncommon scenario is connecting to Office 365 over a web proxy. I had an issue once when PowerShell would not allow me to connect behind the web proxy and these steps worked for me.

#Steps If No Web Proxy Used

  1. To connect powershell to Office 365, open Powershell
  2. Type the following command: $Cred = Get-Credential
  3. Enter the username / password of a valid O365 admin account
  4. Type the following command: $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic –AllowRedirection
  5. Type the following command: Import-PSSession $Session
  6. (If connecting to Office 365 from Powershell without the the Microsoft Online Service module installed type: import-module msonline)
  7. (Verify MSOnline is registered. Type: Get-Command –Module msonline)
  8. Type the following command: Connect-msolservice -Credential $Cred

#Steps If Connecting Over a Web Proxy

  1. To connect powershell to Office 365, open Powershell
  2. Type the following command: $Cred = Get-Credential
  3. Enter the username / password of a valid O365 admin account
  4. Type the following command: $proxysettings = New-PSSessionOption -ProxyAccessType IEConfig
  5. Type the following command: $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://ps.outlook.com/powershell/” -Credential $cred -Authentication Basic -AllowRedirection -SessionOption $proxysettings
  6. Type the following command: Import-PSSession $Session
  7. Type the following command: Connect-msolservice -Credential $Cred

#Steps If Connecting to an On-Premise Exchange Server in Hybrid Mode

  1. Verify the execution policy is set to RemoteSigned or UnRestricted. Type: Get-ExecutionPolicy
  2. If not connecting with the Exchange Management shell type: add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
  3. It’s recommended not to load the Exchange snapin but this is my preferred option to Exchange remoting when I need to remote into both cloud and on-premise environments
  4. Remote into the on-premise Exchange CAS server by typing: $RemoteExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<server FQDN>/PowerShell/ -Authentication Kerberos -Credential (Get-credential)
  5. (Note: You can leverage your domain credentials if you authenticated with a domain joined machine. I’m assuming you need to be authenticated.)
  6. Import-PSSession $RemoteExchangeSession -allowclobber
  7. When complete remove the session from memory by typing: Remove-PSSession $RemoteExchangeSession

References:

  • https://blogs.technet.microsoft.com/rmilne/2015/01/28/directly-loading-exchange-2010-or-2013-snapin-is-not-supported/
  • https://technet.microsoft.com/en-us/library/jj984289(v=exchg.160).aspx
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *