mirror of
https://github.com/admindroid-community/powershell-scripts.git
synced 2025-12-17 16:35:19 +00:00
Connect to Exchange Online PowerShell
Connect to Exchange Online PowerShell
This commit is contained in:
parent
4640797f27
commit
fc94e1fd5c
@ -1,85 +1,35 @@
|
|||||||
#Connect to Exchange Online PowerShell
|
<#
|
||||||
Param
|
=============================================================================================
|
||||||
(
|
Name: Connect to Exchange Online PowerShell
|
||||||
[Parameter(Mandatory = $false)]
|
Version: 2.0
|
||||||
[switch]$Disconnect,
|
Website: o365reports.com
|
||||||
[switch]$MFA,
|
|
||||||
[string]$UserName,
|
For detailed script execution: https://o365reports.com/2019/08/22/connect-exchange-online-powershell/
|
||||||
[string]$Password
|
============================================================================================
|
||||||
)
|
#>
|
||||||
#Disconnect existing sessions
|
#Due RPS and Basic Auth retirement in Exchange Online, we are no longer able to use modules earlier than EXO V3.
|
||||||
if($Disconnect.IsPresent)
|
#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 Exchange Online PowerShell module is not available -ForegroundColor yellow
|
||||||
Write-Host All sessions in the current window has been removed. -ForegroundColor Yellow
|
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
|
||||||
}
|
|
||||||
#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
|
|
||||||
if($Confirm -match "[yY]")
|
if($Confirm -match "[yY]")
|
||||||
{
|
{
|
||||||
Write-Host Yes
|
Write-host "Installing Exchange Online PowerShell module"
|
||||||
Start-Process "iexplore.exe" "https://cmdletpswmodule.blob.core.windows.net/exopsmodule/Microsoft.Online.CSE.PSModule.Client.application"
|
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
|
||||||
|
Import-Module ExchangeOnlineManagement
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
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
|
|
||||||
}
|
|
||||||
$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/'
|
|
||||||
Exit
|
Exit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Importing Exchange MFA Module
|
Write-Host `nConnecting to Exchange Online...
|
||||||
write-host aaaa
|
Connect-ExchangeOnline
|
||||||
. "$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
|
|
||||||
}
|
|
||||||
|
|
||||||
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
|
Write-Host Script executed successfully!
|
||||||
Import-PSSession $Session -DisableNameChecking -AllowClobber -WarningAction SilentlyContinue | Out-Null
|
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
|
||||||
|
|
||||||
#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
|
|
||||||
}}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user