Connect to Exchange Online PowerShell

Connect to Exchange Online PowerShell
This commit is contained in:
AdminDroid 2023-09-28 12:40:02 +05:30
parent 4640797f27
commit fc94e1fd5c

View File

@ -1,85 +1,35 @@
#Connect to Exchange Online PowerShell
Param
(
[Parameter(Mandatory = $false)]
[switch]$Disconnect,
[switch]$MFA,
[string]$UserName,
[string]$Password
)
#Disconnect existing sessions
if($Disconnect.IsPresent)
<#
=============================================================================================
Name: Connect to Exchange Online PowerShell
Version: 2.0
Website: o365reports.com
For detailed script execution: https://o365reports.com/2019/08/22/connect-exchange-online-powershell/
============================================================================================
#>
#Due RPS and Basic Auth retirement in Exchange Online, we are no longer able to use modules earlier than EXO V3.
#Check for EXO v3 module installation
$Module = (Get-Module ExchangeOnlineManagement -ListAvailable) | where {$_.Version.major -ge 3}
if($Module.count -eq 0)
{
Get-PSSession | Remove-PSSession
Write-Host All sessions in the current window has been removed. -ForegroundColor Yellow
}
#Connect Exchnage Online with MFA
elseif($MFA.IsPresent)
{
#Check for MFA mosule
$MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1)
If ($MFAExchangeModule -eq $null)
{
Write-Host `nPlease install Exchange Online MFA Module. -ForegroundColor yellow
Write-Host You can install module using below blog : `nLink `nOR you can install module directly by entering "Y"`n
$Confirm= Read-Host Are you sure you want to install module directly? [Y] Yes [N] No
Write-Host Exchange Online PowerShell module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Write-Host Yes
Start-Process "iexplore.exe" "https://cmdletpswmodule.blob.core.windows.net/exopsmodule/Microsoft.Online.CSE.PSModule.Client.application"
Write-host "Installing Exchange Online PowerShell module"
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
Import-Module ExchangeOnlineManagement
}
else
{
Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/'
Exit
}
$Confirmation= Read-Host Have you installed Exchange Online MFA Module? [Y] Yes [N] No
if($Confirmation -match "[yY]")
{
$MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1)
If ($MFAExchangeModule -eq $null)
{
Write-Host Exchange Online MFA module is not available -ForegroundColor red
Exit
}
}
else
{
Write-Host Exchange Online PowerShell Module is required
Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/'
Write-Host EXO module is required to connect Exchange Online. Please install module using Install-Module ExchangeOnlineManagement cmdlet.
Exit
}
}
#Importing Exchange MFA Module
write-host aaaa
. "$MFAExchangeModule"
Connect-EXOPSSession -WarningAction SilentlyContinue | Out-Null
}
#Connect Exchnage Online with Non-MFA
else
{
if(($UserName -ne "") -and ($Password -ne ""))
{
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword
}
else
{
$Credential=Get-Credential -Credential $null
}
Write-Host `nConnecting to Exchange Online...
Connect-ExchangeOnline
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking -AllowClobber -WarningAction SilentlyContinue | Out-Null
}
#Check for connectivity
if(!($Disconnect.IsPresent)){
If ((Get-PSSession | Where-Object { $_.ConfigurationName -like "*Exchange*" }) -ne $null)
{
Write-Host `nSuccessfully connected to Exchange Online
}
else
{
Write-Host `nUnable to connect to Exchange Online. Error occurred -ForegroundColor Red
}}
Write-Host Script executed successfully!
Write-Host `n~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n