mirror of
https://github.com/admindroid-community/powershell-scripts.git
synced 2025-12-17 08:25:20 +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
|
||||
(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[switch]$Disconnect,
|
||||
[switch]$MFA,
|
||||
[string]$UserName,
|
||||
[string]$Password
|
||||
)
|
||||
#Disconnect existing sessions
|
||||
if($Disconnect.IsPresent)
|
||||
{
|
||||
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
|
||||
if($Confirm -match "[yY]")
|
||||
{
|
||||
Write-Host Yes
|
||||
Start-Process "iexplore.exe" "https://cmdletpswmodule.blob.core.windows.net/exopsmodule/Microsoft.Online.CSE.PSModule.Client.application"
|
||||
}
|
||||
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/'
|
||||
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
|
||||
}
|
||||
|
||||
$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
|
||||
}
|
||||
<#
|
||||
=============================================================================================
|
||||
Name: Connect to Exchange Online PowerShell
|
||||
Version: 2.0
|
||||
Website: o365reports.com
|
||||
|
||||
#Check for connectivity
|
||||
if(!($Disconnect.IsPresent)){
|
||||
If ((Get-PSSession | Where-Object { $_.ConfigurationName -like "*Exchange*" }) -ne $null)
|
||||
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)
|
||||
{
|
||||
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 `nSuccessfully connected to Exchange Online
|
||||
Write-host "Installing Exchange Online PowerShell module"
|
||||
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
|
||||
Import-Module ExchangeOnlineManagement
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Host `nUnable to connect to Exchange Online. Error occurred -ForegroundColor Red
|
||||
}}
|
||||
Write-Host EXO module is required to connect Exchange Online. Please install module using Install-Module ExchangeOnlineManagement cmdlet.
|
||||
Exit
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host `nConnecting to Exchange Online...
|
||||
Connect-ExchangeOnline
|
||||
|
||||
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
|
||||
Loading…
x
Reference in New Issue
Block a user