共用方式為


Using PowerShell ISE to connect to Exchange Online when MFA is enabled

The customer here wished to be able to use the PowerShell ISE and still be able to connect with MFA (Multi-Factor Authentication) enabled for their account.

We already have a PowerShell module designed to allow connection with MFA enabled, but there is no direct integration of it with the PowerShell ISE.

When the Microsoft Exchange PowerShell app (https://technet.microsoft.com/en-us/library/mt775114%28v=exchg.160%29.aspx) is installed and executed, it launches a PowerShell session and runs a script to load the necessary modules and functions into the current PowerShell execution to provide the cmdlet.

The files related to the app are all loaded into a folder with a path similar to (as this is a Click To Run app):
C:\Users\<username>\AppData\Local\Apps\2.0\LC7A9808.VWQ\TDNEY3XY.VWX\micr..tion_c3bce3770c238a49_0010.0000_213d7102fbbdf9ba

To find the folder with the files in it, I first went to my Start Menu\Programs folder to find the shortcut for it:
C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft Corporation

 

When I get the properties of that item, I see:

 

Looking at the Folder path value, I now know the name of the folder the module points to, I can go to the C:\Users\<username>\AppData\Local\Apps\2.0  folder and find the appropriate folder.

To successfully use the Connect-EXOPSSession cmdlet in the ISE, I need to "dot load" the CreateExoPSSession.ps1 script found in the above folder into my ISE execution. When it runs, it loads the necessary modules and functions into the session so that the Connect-EXOPSSession cmdlet is available and works to connect to Exchange Online with MFA enabled. This is done with the following syntax after changing to the location of the module:
. .\CreateExoPSSession.ps1

Update 4/5/18 - One of my coworkers brought me these 2 lines that we can run, which will find the module for us and load it up in our ISE session, without having to go out and search for it ourselves: $CreateEXOPSSession = (Get-ChildItem -Path $env:userprofile -Filter CreateExoPSSession.ps1 -Recurse -ErrorAction SilentlyContinue -Force | Select -Last 1).DirectoryName . "$CreateEXOPSSession\CreateExoPSSession.ps1"

Comments

  • Anonymous
    September 04, 2018
    Great Script. This was what I was looking for, it's makes my job a lot easier. Thanks
  • Anonymous
    November 22, 2018
    That was really handy to allow use of ISE. Thanks to you and co-worker :)
  • Anonymous
    December 12, 2018
    Just made my day. Thanks!!!
  • Anonymous
    February 26, 2019
    Thank you!!!!