Skip to content

Instantly share code, notes, and snippets.

@pax8-dkirby
Created May 18, 2020 22:18
Show Gist options
  • Save pax8-dkirby/912b1f43d8f04c00e7447b7580a05eae to your computer and use it in GitHub Desktop.
Save pax8-dkirby/912b1f43d8f04c00e7447b7580a05eae to your computer and use it in GitHub Desktop.
A function that checks for the new Exchange Online Management Shell and will use it if it's available. Otherwise, it falls back to the old connector, but with a warning.
function Connect-EOCustom {
if(Get-Command Connect-ExchangeOnline -ErrorAction SilentlyContinue) {
#The new module is installed
Write-Host "You have the modern management shell installed, it will be used to connect to EO!" -ForegroundColor Green;
Import-Module ExchangeOnlineManagement;
Connect-ExchangeOnline;
} else {
#the new module is not installed
Write-Host "NOTICE: You are not using the modern management shell. The old shell will soon be deprecated, but the script will continue for now." -ForegroundColor Yellow;
Write-Host "It is strongly recommended that you install the new shell using Install-Module -Name ExchangeOnlineManagement" -ForegroundColor Yellow;
Pause;
$UserCredential = Get-Credential;
try {
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session;
} catch {
Write-Host "Couldn't connect to Exchange Online." -ForegroundColor red;
Pause;
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment