Executing Powershell for Exchange Online and MSOnline modules secured with MFA?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Executing Powershell for Exchange Online and MSOnline modules secured with MFA?

Post by ITEngineer »

I need some help and suggestion in how to run the Powershell for Exchange Online and MSOnline modules when the Admin account is secured with MFA via my phone?

Code: Select all

If (-not (Get-Module MSOnline) ) {
    Write-Host "No Microsoft Online PowerShell module installed" -WarningAction
    Try { Install-Module MSOnline -ErrorAction Stop; Import-Module MSOnline -ErrorAction Stop }
    Catch { Write-Warning "Unable to load Microsoft Office 365 module because $($Error[0])"; Exit }
}
 
Try {
    $UserCredential = Get-Credential
    Connect-MsolService -Credential $UserCredential
 
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    Import-PSSession $Session -AllowClobber -DisableNameChecking
 
    #Start Script...
	
	Get-Mailbox Enterprise.Administrator
	Get-MSOLUser Enterprise.Administrator
 
    #End Script...
}
Catch { Write-Warning "Unable to execute PowerShell remoting session because $($Error[0])"; Exit }
Finally {If ($Session) {Remove-PSSession $Session}}
I will need to run the script above via SApien IDE or even from Powershell IDE, hence your suggestion would be greatly appreciated.

Some information I've been looking into but still confused integrating to the script above.
App Password: this is insecure since I must save it as plain text
https://support.office.com/en-us/articl ... 3953ee1183

Global Reader Role, this sounds good, but I cannot change any settings or delete some values:
https://docs.microsoft.com/en-us/azure/ ... bal-reader

So far I can only execute manual line by line of the powershell script using the steps defined in: https://docs.microsoft.com/en-us/powers ... xchange-ps


But it is impractical to use it for Scheduled script and bigger script.

Thank you.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Executing Powershell for Exchange Online and MSOnline modules secured with MFA?

Post by jvierra »

See the following: https://docs.microsoft.com/en-us/powers ... xchange-ps

There is no bypass for MFA. To allow bypass defeats the purpose of MFA.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Executing Powershell for Exchange Online and MSOnline modules secured with MFA?

Post by jvierra »

You can connect to Exchange using this newer module: https://docs.microsoft.com/en-us/powers ... xchange-ps
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Executing Powershell for Exchange Online and MSOnline modules secured with MFA?

Post by ITEngineer »

jvierra wrote: Tue Nov 26, 2019 1:11 am You can connect to Exchange using this newer module: https://docs.microsoft.com/en-us/powers ... xchange-ps
OK, so is there any steps I can follow so the IDE (Powershell ISE, Sapien, VS code) can understand it?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Executing Powershell for Exchange Online and MSOnline modules secured with MFA?

Post by jvierra »

Understand what?
This topic is 4 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked