Last active
March 7, 2021 13:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"}} |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -ExecutionPolicy Bypass -File "Start-MSOLShell.ps1"