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

Ask your Windows 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.
Post Reply
User avatar
ITEngineer
Posts: 171
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Nov 26, 2019 1:48 am

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: 14016
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Nov 26, 2019 2:09 am

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: 14016
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Nov 26, 2019 2:11 am

You can connect to Exchange using this newer module: https://docs.microsoft.com/en-us/powers ... xchange-ps

User avatar
ITEngineer
Posts: 171
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Nov 26, 2019 4:53 am

jvierra wrote:
Tue Nov 26, 2019 2: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 */


Post Reply