Skip to content

Instantly share code, notes, and snippets.

@chrisbrownie
Created March 1, 2016 00:07
Show Gist options
  • Save chrisbrownie/64ba2ba585358757a738 to your computer and use it in GitHub Desktop.
Save chrisbrownie/64ba2ba585358757a738 to your computer and use it in GitHub Desktop.
Connects PowerShell to Office 365 Exchange Online
function Connect-ExchangeOnline() {
Param(
$Credential = $(Get-Credential)
)
if (-not $global:eoSession) {
$Global:eoSession = New-PSSession `
-ConfigurationName Microsoft.Exchange `
-ConnectionUri "https://outlook.office365.com/powershell-liveid" `
-Authentication Basic `
-AllowRedirection `
-Credential $Credential
if ($global:eoSession) {
Import-PSSession $Global:eoSession
} else {
throw "Unable to connect to Exchange Online!"
}
} else {
throw "Already connected to Exchange Online! Run Disconnect-ExchangeOnline first"
}
}
function Disconnect-ExchangeOnline() {
if ($global:eoSession) {
Remove-PSSession $global:eoSession
} else {
Write-Warning "Not currently connected to Exchange Online"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment