r/PowerShell • • Feb 05 '19

PowerShell Function to Connect to All Office 365 Services With Support for MFA

https://www.thelazyadministrator.com/2019/02/05/powershell-function-to-connect-to-all-office-365-services-with-support-for-mfa/
177 Upvotes

21 comments sorted by

12

u/a-rich Feb 05 '19 edited Feb 05 '19

Nice.

What about adding support for trusted locations?

I wrote a similar script for me and my team to connect to O365 services, but since we have trusted locations enabled with some of our static IPs, they dont have to go through MFA when in one of our offices.

The script calls http://ipinfo.io/json and checks against an array of IPs in the script, so there would be some user config required, but its minimal.

And when they're offsite the MFA option will kick in.

5

u/TheLazyAdministrator Feb 05 '19

Are you in Plano? Hello from Geneva!

I like this idea a lot. We do use trusted locations as well so this is a good idea. I will look into it :)

4

u/andrewjnet Feb 05 '19

And hello from DeKalb! I was looking into this recently and didn’t quite get it 100%. You just saved me a lot of time! Thanks!

1

u/TheLazyAdministrator Feb 05 '19

Nice! You are quite close in Dekalb!

2

u/a-rich Feb 05 '19

Plano, TX? Nope.

Hello from Jersey though!

3

u/ComicOzzy Feb 05 '19

Hello from Austin!

I just wanted to be included.

2

u/hdh33 Feb 06 '19

From Round Rock!

2

u/ComicOzzy Feb 06 '19

Neighbor!

1

u/M3jsh Feb 05 '19

We have trusted IPs set up for our offices so that MFA is bypassed. But remoting into O365 services has still necessitated using the modules that enable modern authentication. Am I missing something to allow us to avoid the need to use MFA while in the office?

3

u/a-rich Feb 06 '19

Some modules support bypassing MFA while on a trusted location, while others do not.

In my script I first get the public IP, then I have an if statement that checks an array of our IPs against the current public one.

So for modules that dont support the bypass, if Im on a trusted IP I do my own bypass by passing the cred (-credential $cred) otherwise I dont pass it and MFA kicks in.

For example:

$PublicIp = (Invoke-RestMethod http://ipinfo.io/json).ip
$TrustedIPs = "x.x.x.x", "x.x.x.x", "x.x.x.x"

# Connect to Sharepoint online
If ($trustedIPs -contains $PublicIp) {
	Connect-SPOService -Url https://MyOrg-admin.sharepoint.com -credential $cred
}
Else {
	Connect-SPOService -Url https://MyOrg-admin.sharepoint.com
}

This is a good page for a reference of which modules support bypassing MFA on a trusted location.

Oddly enough, that page doesnt show the MsolService, but it doesnt seem to support bypassing MFA either.I have a bypass for that in my login script as well.

2

u/M3jsh Feb 06 '19

Thanks a lot!

4

u/KaliUK Feb 05 '19

Is there anything Azure MFA needs in order to be setup? We have it setup but it’s not sending the code. Would I use these scripts, or are they just to enable/connect to powershell sessions?

1

u/TheLazyAdministrator Feb 05 '19

do you have the proper module for MFA? Its downloaded from your tenant. LMK

1

u/KaliUK Feb 05 '19

Yes, I installed the Azure NPS extension, and NPS setup, but it’s not requesting verification when I go to RDWEB.

1

u/TheLazyAdministrator Feb 05 '19

? I’m confused a bit on any reference to NPS or RDS. Can you expand a bit

2

u/800oz_gorilla Feb 05 '19

I think you're not understanding.

The MFA extension that you put on an NPS server allows you to take internal authentication requests (like Radius, dot1x Wifi, etc) and redirect it to Azure for MFA approval once it passes the internal checks (group membership, password, client ip address, etc). This allows you to get around having to build your own MFA server and maintain it. Simply turn it on in the cloud for the user and point your NPS server to it.

This extension doesn't apply to OP's post. OP is talking about connecting a powershell module to O365's cloud services for administration, and his account is MFA enabled there. So rather than open a new powershell window for each O365 service, he wrote a script that connects to all of them. The MFA piece gets thrown into this by Microsoft being stupid and not updating powershell to sense MFA and use it if enabled. There's a separate command you have to run/import in order for powershell to use modern authentication (mfa) instead of traditional. So he put that in his script as well.

As far as your issue, I have limited understanding of NPS, but I've set it up for Wifi access. You have to make sure you have your client's properly identified (client meaning the device you're trying to remote into), your connection request policy and your network policies all set up. And if you're trying to do a range of client IPs, you need to use regular expressions to properly identify the range.

http://www.analyticsmarket.com/freetools/ipregex

2

u/KaliUK Feb 06 '19

Yeah, I think your right, it’s outside the scope of the OP’s post. I’ll make a post somewhere with all the details and post the link here if you guys are interested.

2

u/efiggy Feb 06 '19

Very awesome! Would you consider adding an option to install modules if they're not available? Are those modules even in PSGallery? I can't remember now. In any case, great job and thanks for sharing!

4

u/TheLazyAdministrator Feb 06 '19

Yes working on that now. Most are available on the PSGallery. So it will need admin rights to install. The only one not available is the exchange MFA one.

2

u/efiggy Feb 06 '19

Cool, thanks again!!

3

u/Mkep Feb 05 '19

Oh it’s a miracle, I’ve been looking into doing this recently because that separate power shell console is annoying to have to launch m