Skip to content

Instantly share code, notes, and snippets.

@e-sterling
Last active March 7, 2021 13:40
Show Gist options
  • Select an option

  • Save e-sterling/ddbb86fce97415f39ea52c3332606b55 to your computer and use it in GitHub Desktop.

Select an option

Save e-sterling/ddbb86fce97415f39ea52c3332606b55 to your computer and use it in GitHub Desktop.
Script to connect to Exchange Online, MSOL, and AzureAD in one Powershell prompt
Write-Host "[~] " -ForegroundColor Yellow -NoNewline; Write-Host "Checking MSOL Modules..."
if (-not(Get-Command "Get-MsolDomain" -errorAction SilentlyContinue))
{
Write-Host "[-] " -ForegroundColor red -NoNewline; Write-Host "MSOL module not found, installing..."
Install-Module MSOnline -Scope CurrentUser
}
if (-not(Get-Command "Get-AzureADDomain" -errorAction SilentlyContinue))
{
Write-Host "[-] " -ForegroundColor red -NoNewline; Write-Host "AzureAD module not found, installing..."
Install-Module AzureAD -Scope CurrentUser
}
if($?){Write-Host "[+] " -ForegroundColor green -NoNewline; Write-Host "Modules installed"}
$UserCredential = Get-Credential
Write-Host "[~] " -ForegroundColor yellow -NoNewline; Write-Host "Connecting to MSOL services... "
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
if(!($?)){Write-Host "[-] " -ForegroundColor red -NoNewline; Write-Host "Failed to connect to Exchange"} else {if($?){Write-Host "[+] " -ForegroundColor green -NoNewline; Write-Host "Connected to Exchange Online"}}
Connect-MSOLService -Credential $UserCredential
if(!($?)){Write-Host "[-] " -ForegroundColor red -NoNewline; Write-Host "Failed to connect to MSOL"} else {if($?){Write-Host "[+] " -ForegroundColor green -NoNewline; Write-Host "Connected to MSOL"}}
Connect-AzureAD -Credential $UserCredential
if(!($?)){Write-Host "[-] " -ForegroundColor red -NoNewline; Write-Host "Failed to connect to AzureAD"} else {if($?){Write-Host "[+] " -ForegroundColor green -NoNewline; Write-Host "Connected to AzureAD"}}
@e-sterling
Copy link
Copy Markdown
Author

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -ExecutionPolicy Bypass -File "Start-MSOLShell.ps1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment