Summary

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.

[ Note:  (Dec 2017) the following PowerShell commands are for the older MSOnline V1 PowerShell module that would use the sign-in assistant. Also, customers are encouraged to use the newer Azure Active Directory V2 PowerShell module (aka Active Directory PowerShell for Graph). Though, you can use the soon to be deprecated legacy modules for Exchange Online and Azure Active Directory Module for Azure AD administrative tasks such as user management, domain management and for configuring single sign-on. However, I encourage you to install the newest version of Azure AD PowerShell module and use the new exchange PowerShell module “ExoPowershellModule”. If you have MFA configured for your admin accounts, which is recommended and best practice, then you will have to use the new exchange online module. ]

Option 1) 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

[powershell]
$Cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic –AllowRedirection
Import-PSSession $Session
Import-Module msonline
Get-Command –Module msonline
Connect-msolservice -Credential $Cred
[/powershell]

Option 2) 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

[powershell]
$Cred = Get-Credential
$proxysettings = New-PSSessionOption -ProxyAccessType IEConfig
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://ps.outlook.com/powershell/” -Credential $cred -Authentication Basic -AllowRedirection -SessionOption $proxysettings
Import-PSSession $Session
Connect-msolservice -Credential $Cred
[/powershell]

Option 3) 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:///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

[powershell]
Get-ExecutionPolicy
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
$RemoteExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http:///PowerShell/ -Authentication Kerberos -Credential (Get-credential)
Import-PSSession $RemoteExchangeSession -allowclobber
Remove-PSSession $RemoteExchangeSession
[/powershell]

Option 4) Securely connect to Office 365 using PowerShell using the new Exchange Online modules and MFA option

  1. Read through the steps here: How to: Securely connect to Office 365 and Azure AD using PowerShell & MFA

References:

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply