I’ve been working on some PowerShell scripts for use with Office 365 and needed to invoke/test Office 365 (Exchange Online) cmdlets via RemotePowerShell. This post relates to connections to Office 365 and Exchange online. My previous post How to invoke test exchange 2010 cmdlets via remote PowerShell 2.0 window, however the following allows for the credentials to be different from the current logged on user.
Office 365 allows powershell access with a generic connection location and with the added cmdlet switch of ‘-AllowRedirection’ redirects your powershell connection to you correct CAS location.
First capture the credentials used to connect to Office 365 into a memory variable with the command:
1 |
[PS] C:\>$creds = Get-Credential |
(Enter you Office 365 admin logon details)
1 |
[PS] C:\>$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $creds -Authentication Basic -AllowRedirection |
This makes the connection to Office 365 RemotePowerShell endpoint.
1 |
[PS] C:\>Import-PSSession $Session |
This will complete the connection and download the cmdlets from Office 365 to your Powershell Window.
To check if the connection is complete use:
1 |
[PS] C:\>Get-OrganizationConfig |
You can use the -prefix to make serval Office 365 connections to different tenants at the same time or connect to Exchange On-premises with Office 365.
An example to allow such commands of New-O365Mailbox or Get-O365OrganizationConfig
1 |
[PS] C:\>$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $creds -Authentication Basic -AllowRedirection -Prefix <strong>O365</strong> |