SOLVED

Azure Automation connecting to Exchange with MFA enforced

%3CLINGO-SUB%20id%3D%22lingo-sub-548668%22%20slang%3D%22en-US%22%3EAzure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-548668%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20have%20a%20tenant%20with%20MFA%20a%20requirement%20for%20any%20account%20with%20elevated%20privileges.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EI%20can%20use%20Azure%20Automation%20PowerShell%20runbook%20for%20Azure%20AD%20using%20the%20service%20principal%20and%20certificate%20e.g.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%23%20Get%20Azure%20Run%20As%20Connection%20Name%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24connectionName%20%3D%20%22AzureRunAsConnection%22%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%23%20Get%20the%20Service%20Principal%20connection%20details%20for%20the%20Connection%20name%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24servicePrincipalConnection%20%3D%20Get-AutomationConnection%20-Name%20%24connectionName%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%23%20Logging%20in%20to%20Azure%20AD%20with%20Service%20Principal%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%22Logging%20in%20to%20Azure%20AD...%22%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EConnect-AzureAD%20-TenantId%20%24servicePrincipalConnection.TenantId%20%60%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20-ApplicationId%20%24servicePrincipalConnection.ApplicationId%20%60%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20-CertificateThumbprint%20%24servicePrincipalConnection.CertificateThumbprint%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EI%20cannot%20however%20seem%20to%20work%20out%20a%20way%20to%20connect%20to%20Exchange%20when%20MFA%20is%20enforced%3A%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3ETo%20demonstrate%2C%20I%20configure%20the%20automation%20account%20credentials%20to%20use%20a%20global%20admin%20account%20and%20then%20try%3A%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EConnect-ExchangeOnlineShell%20-Credential%20%24Credential%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EI%20get%20an%20error%20'you%20must%20use%20multi-factor%20authentication%20to%20access'%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EIt%20appears%20there%20isn't%20an%20equivalent%20certificate%20based%20login%20for%20Exchange%20as%20in%20place%20for%20Azure%20AD%20so%20my%20only%20option%20is%20to%20use%20credentials%20for%20an%20account%20which%20doesn't%20have%20MFA%2C%20which%20from%20security%20is%20going%20to%20problematic.%20Does%20anyone%20therefore%20know%20how%20to%20connect%20to%20EOL%20when%20MFA%20is%20enforced%3F%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3ENote%3A%20Connect-ExchangeOnlineShell%20is%20a%20wrapper%20for%20Import-PSSession%20%24Session%20method%20to%20connect%20to%20Exchange%20online%20which%20I%20have%20also%20investigated.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-548668%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-552116%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-552116%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F100245%22%20target%3D%22_blank%22%3E%40Chris%20Johnston%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHave%20you%20looked%20at%20using%20application%20passwords%20for%20the%20office%20365%20account%20with%20MFA%3F%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fuser-help%2Fmulti-factor-authentication-end-user-app-passwords%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fuser-help%2Fmulti-factor-authentication-end-user-app-passwords%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20could%20potentially%20store%20the%20application%20password%20in%20a%20keyVault%20and%20call%20it%20from%20your%20service%20principal.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-552189%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-552189%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F50205%22%20target%3D%22_blank%22%3E%40Richard%20Hooper%3C%2FA%3E%26nbsp%3BI%20have%20used%20the%20equivalent%20for%20automation%2C%20i.e.%20app%20secret%20for%20services%20that%20can%20accept%20these%20connection%20parameters%20e.g.%20PnP%3C%2FP%3E%3CP%3E%24servicePrincipalConnection%3DGet-AutomationConnection%20-Name%20%22AzureRunAsConnection%22%3CBR%20%2F%3E%24appsecret%20%3D%20Get-AutomationVariable%20-Name%20%22SPAppSecret%22%3C%2FP%3E%3CP%3EConnect-PnPOnline%20-AppId%20%24servicePrincipalConnection.ApplicationId%20-AppSecret%20%24appsecret%20-Url%20%22%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20don't%20see%20where%20Exchange%20connection%20supports%20app%20secrets.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWould%20you%20be%20able%20to%20provide%20a%20working%20or%20theoretical%20example%20of%20how%20the%20connection%20to%20Exchange%20online%20would%20use%20an%20application%20password%20as%20you%20suggest%20please%20as%20I'm%20not%20sure%20how%20I'd%20do%20this.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-552280%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-552280%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F100245%22%20target%3D%22_blank%22%3E%40Chris%20Johnston%3C%2FA%3E%26nbsp%3BUnfortunately%20what%20I%20was%20suggesting%20will%20not%20work.%20There%20seems%20to%20be%20a%20user%20voice%20regarding%20this.%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Foffice365.uservoice.com%2Fforums%2F273493-office-365-admin%2Fsuggestions%2F14890308-allow-admin-to-create-powershell-session-using-app%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Foffice365.uservoice.com%2Fforums%2F273493-office-365-admin%2Fsuggestions%2F14890308-allow-admin-to-create-powershell-session-using-app%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20thinking%20that%20you%20will%20not%20be%20able%20to%20do%20what%20you%20want%20to%20do%20with%20MFA%20enabled.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-557321%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-557321%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F50205%22%20target%3D%22_blank%22%3E%40Richard%20Hooper%3C%2FA%3E%26nbsp%3BHopefully%20I'll%20get%20an%20answer%20to%20this%20but%20in%20the%20meantime%20as%20a%20workaround%2C%20I'll%20look%20at%20using%20conditional%20access%20to%20block%20a%20service%20account%20(that%20doesn't%20have%20MFA%20enforced)%20unless%20it's%20from%20a%20named%20location%26nbsp%3Bof%20the%20data%20centre%20where%20the%20automation%20account%20has%20been%20provisioned.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fdownload%2Fdetails.aspx%3Fid%3D41653%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fdownload%2Fdetails.aspx%3Fid%3D41653%3C%2FA%3E%3C%2FP%3E%3CP%3EDoesn't%20look%20like%20you%20can%20be%20more%20granular%20than%20that%20for%20a%20runbook%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fsocial.msdn.microsoft.com%2FForums%2Fazure%2Fen-US%2F26bd07d4-05bc-446f-a4d5-c185f517d8bb%2Fstorage-account-firewall-and-azure-automation%3Fforum%3Dwindowsazuredata%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsocial.msdn.microsoft.com%2FForums%2Fazure%2Fen-US%2F26bd07d4-05bc-446f-a4d5-c185f517d8bb%2Fstorage-account-firewall-and-azure-automation%3Fforum%3Dwindowsazuredata%3C%2FA%3E%3C%2FP%3E%3CP%3Eand%20presumably%20the%20IPs%20are%20subject%20to%20change%2C%20plus%20I%20will%20now%20have%20MFA%20exclusions%20requiring%20security%20approval%20so%20its%20not%20an%20ideal%20workaround%20tbh.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-826279%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-826279%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F100245%22%20target%3D%22_blank%22%3E%40Chris%20Johnston%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHave%20someone%20solved%20to%20connect%20to%20EOP%20from%20Runbook%20with%20MFA%20enabled%20from%20Conditional%20Access%3F%20Or%20a%20workaround%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-826301%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-826301%22%20slang%3D%22en-US%22%3EProbably%20this%20will%20work%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fwww.powershellgallery.com%2Fpackages%2FExchangeOnlineShell%2F2.0.3.3%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.powershellgallery.com%2Fpackages%2FExchangeOnlineShell%2F2.0.3.3%3C%2FA%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-846037%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-846037%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F186510%22%20target%3D%22_blank%22%3E%40Magnus%20Tengmo%3C%2FA%3E%26nbsp%3Bdid%20you%20manage%20to%20solve%20this%20%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-900164%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-900164%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F100245%22%20target%3D%22_blank%22%3E%40Chris%20Johnston%3C%2FA%3E%26nbsp%3BThe%20answer%20is%20deceptively%20simple....%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20published%20a%20Runbook%20script%20to%20get%20you%20started%20with%20the%20initial%20connection%2C%20then%20you%20can%20add%20your%20own%20script%20form%20there%20on.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.powershellgallery.com%2Fpackages%2FAzureAutomationAgainstExchangeOnlineWithMFAEnabledAccount%2F1.0.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.powershellgallery.com%2Fpackages%2FAzureAutomationAgainstExchangeOnlineWithMFAEnabledAccount%2F1.0.0%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-902570%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-902570%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F345807%22%20target%3D%22_blank%22%3E%40MichaelMardahl%3C%2FA%3E%26nbsp%3Blooking%20at%20the%20script%20comments%20the%20key%20is%20that%26nbsp%3B%3CSPAN%3Ethe%20user%20account%20used%2C%20has%20never%20logged%20on%2C%20which%20then%20lets%20us%20bypass%20MFA.%20It%20has%20the%20feel%20of%20a%20loophole%20that%20may%20close%20at%20some%20point%20but%20many%20thanks%20for%20the%20post%2C%20I'll%20give%20this%20a%20go%20%3A)%3C%2Fimg%3E%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-903745%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-903745%22%20slang%3D%22en-US%22%3EI%20got%20it%20from%20the%20official%20partner%20documentation%20%3A)%3C%2Fimg%3E%3CBR%20%2F%3EI%20am%20contemplating%20the%20issues.%20But%20for%20now%20I%20just%20configure%20a%2050char%20password%20and%20put%20it%20away%20in%20a%20locked%20box.%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20you%20wanna%20be%20REALLY%20secure.%20Then%20use%20the%20demo%20azure%20atomation%20script%20that%20connects%20securely%20to%20azure%20ad%20using%20a%20cert.%3CBR%20%2F%3EFrom%20there%2C%20create%20the%20user%20with%20my%20method.%3CBR%20%2F%3EThe%20do%20your%20thing%20with%20exchange.%20And%20delete%20the%20user%20again!%20%3A)%3C%2Fimg%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESeems%20messy%2C%20but%20the%20odds%20of%20anyone%20being%20able%20to%20compromise%20the%20account%20in%20the%20few%20minutes%20it%20lives%2C%20are%200.00000000000001%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-910814%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-910814%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F100245%22%20target%3D%22_blank%22%3E%40Chris%20Johnston%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20could%20be%20an%20issue%20with%20how%20the%20connection%20is%20happening.%20If%20you%20are%20using%20a%20Microsoft%20hosted%20server%20to%20run%20the%20runbook%2C%20it%20would%20be%20using%20the%20default%20Exchange%20Online%20PowerShell.%20Once%20MFA%20is%20enabled%20you%20need%20to%20switch%20to%20a%20new%20connection.%20See%20the%20URL%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fexchange%2Fexchange-online%2Fconnect-to-exchange-online-powershell%2Fmfa-connect-to-exchange-online-powershell%3Fview%3Dexchange-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fexchange%2Fexchange-online%2Fconnect-to-exchange-online-powershell%2Fmfa-connect-to-exchange-online-powershell%3Fview%3Dexchange-ps%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20get%20this%20working%20on%20Azure%20AUtomation%2C%20try%20using%20a%20hybrid%20run%20book%20server.%20You%20can%20then%20download%20the%20correct%20PowerShell%20modules%20and%20get%20the%20service%20account%20to%20work%20from%20there.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-910815%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-910815%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Craig.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThe%20example%20runbook%20in%20the%20PS%20gallery%20I%20have%20linked%20to%2C%20works%20just%20fine%20without%20a%20hybrid%20worker.%3CBR%20%2F%3ENo%20exclusions%2C%20or%20trusted%20site%20lists%2C%20with%20full%20enforced%20MFA%20for%20priv.%20accounts.%3C%2FP%3E%3CP%3EIt%20uses%20a%20module%20called%20exchangeOnlineShell%20which%20works%20like%20the%20one%20you%20are%20linking%20to%2C%20but%20without%20requiring%20the%20installation%2C%20which%20is%20a%20problem%20in%20azure%20automation%2C%20and%20yes%2C%20that%20would%20require%20a%20hybrid%20worker%20as%20you%20say.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20sure%20we%20will%20soon%20see%20some%20solutions%20from%20microsoft%20for%20authenticating%20with%20certificates%20against%20Exchange%20Online...%20(Fingers%20crossed)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1004452%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1004452%22%20slang%3D%22en-US%22%3E%3CP%3EMicrosoft%20have%20released%20a%20complete%20new%20module%20for%20Exchange%20with%20support%20for%20modern%20authentication%20alot%20of%20other%20improvements.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fexchange%2Fexchange-online%2Fexchange-online-powershell-v2%2Fexchange-online-powershell-v2%3Fview%3Dexchange-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fexchange%2Fexchange-online%2Fexchange-online-powershell-v2%2Fexchange-online-powershell-v2%3Fview%3Dexchange-ps%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1601852%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Automation%20connecting%20to%20Exchange%20with%20MFA%20enforced%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1601852%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F345807%22%20target%3D%22_blank%22%3E%40MichaelMardahl%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20deployed%20this%20and%20the%20only%20thing%20I%20changed%20was%20the%20user%20account%20name%20(which%20is%20definately%20in%20Azure%20Automation%20Credentials)%20and%20it%20just%20gets%20stuck%20on%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22azc-log-stream-text%20azc-text-stream-wrap-word-text%22%3ELogging%20in%20to%20Exchange%20Online...%3C%2FSPAN%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3Band%20never%20stops.%20The%20account%20has%20permissions%20and%20can%20login%20using%20powershell.%3C%2FP%3E%3CP%3EIm%20finding%20every%20solution%20I%20have%20tried%20to%20use%20Exchange%20Online%20with%20Azure%20Automation%20does%20the%20same%20and%20gets%20stuck.%20Any%20ideas%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Contributor

I have a tenant with MFA a requirement for any account with elevated privileges.
I can use Azure Automation PowerShell runbook for Azure AD using the service principal and certificate e.g.

# Get Azure Run As Connection Name
$connectionName = "AzureRunAsConnection"
# Get the Service Principal connection details for the Connection name
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName         

# Logging in to Azure AD with Service Principal
"Logging in to Azure AD..."
Connect-AzureAD -TenantId $servicePrincipalConnection.TenantId `
    -ApplicationId $servicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

I cannot however seem to work out a way to connect to Exchange when MFA is enforced:
To demonstrate, I configure the automation account credentials to use a global admin account and then try:
Connect-ExchangeOnlineShell -Credential $Credential
I get an error 'you must use multi-factor authentication to access'
It appears there isn't an equivalent certificate based login for Exchange as in place for Azure AD so my only option is to use credentials for an account which doesn't have MFA, which from security is going to problematic. Does anyone therefore know how to connect to EOL when MFA is enforced? 
Note: Connect-ExchangeOnlineShell is a wrapper for Import-PSSession $Session method to connect to Exchange online which I have also investigated. 

14 Replies
Highlighted

@Chris Johnston 

Have you looked at using application passwords for the office 365 account with MFA? https://docs.microsoft.com/en-us/azure/active-directory/user-help/multi-factor-authentication-end-us...

 

You could potentially store the application password in a keyVault and call it from your service principal.

Highlighted

@Richard Hooper I have used the equivalent for automation, i.e. app secret for services that can accept these connection parameters e.g. PnP

$servicePrincipalConnection=Get-AutomationConnection -Name "AzureRunAsConnection"
$appsecret = Get-AutomationVariable -Name "SPAppSecret"

Connect-PnPOnline -AppId $servicePrincipalConnection.ApplicationId -AppSecret $appsecret -Url ""

 

I don't see where Exchange connection supports app secrets. 

 

Would you be able to provide a working or theoretical example of how the connection to Exchange online would use an application password as you suggest please as I'm not sure how I'd do this. 

 

 

Highlighted

@Chris Johnston Unfortunately what I was suggesting will not work. There seems to be a user voice regarding this. https://office365.uservoice.com/forums/273493-office-365-admin/suggestions/14890308-allow-admin-to-c...

 

I am thinking that you will not be able to do what you want to do with MFA enabled.

Highlighted

@Richard Hooper Hopefully I'll get an answer to this but in the meantime as a workaround, I'll look at using conditional access to block a service account (that doesn't have MFA enforced) unless it's from a named location of the data centre where the automation account has been provisioned.

https://www.microsoft.com/en-us/download/details.aspx?id=41653

Doesn't look like you can be more granular than that for a runbook 

https://social.msdn.microsoft.com/Forums/azure/en-US/26bd07d4-05bc-446f-a4d5-c185f517d8bb/storage-ac...

and presumably the IPs are subject to change, plus I will now have MFA exclusions requiring security approval so its not an ideal workaround tbh. 

 

Highlighted

@Chris Johnston 

 

Have someone solved to connect to EOP from Runbook with MFA enabled from Conditional Access? Or a workaround?

Highlighted

@Magnus Tengmo did you manage to solve this ?

Highlighted
Best Response confirmed by Chris Johnston (Contributor)
Solution

@Chris Johnston The answer is deceptively simple....

 

I published a Runbook script to get you started with the initial connection, then you can add your own script form there on.

https://www.powershellgallery.com/packages/AzureAutomationAgainstExchangeOnlineWithMFAEnabledAccount...

 

 

Highlighted

@MichaelMardahl looking at the script comments the key is that the user account used, has never logged on, which then lets us bypass MFA. It has the feel of a loophole that may close at some point but many thanks for the post, I'll give this a go :)    

Highlighted
I got it from the official partner documentation :)
I am contemplating the issues. But for now I just configure a 50char password and put it away in a locked box.

If you wanna be REALLY secure. Then use the demo azure atomation script that connects securely to azure ad using a cert.
From there, create the user with my method.
The do your thing with exchange. And delete the user again! :)

Seems messy, but the odds of anyone being able to compromise the account in the few minutes it lives, are 0.00000000000001
Highlighted

Hi @Chris Johnston 

 

This could be an issue with how the connection is happening. If you are using a Microsoft hosted server to run the runbook, it would be using the default Exchange Online PowerShell. Once MFA is enabled you need to switch to a new connection. See the URL https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powe...

 

To get this working on Azure AUtomation, try using a hybrid run book server. You can then download the correct PowerShell modules and get the service account to work from there.

 

 

Highlighted

Hi Craig. 


The example runbook in the PS gallery I have linked to, works just fine without a hybrid worker.
No exclusions, or trusted site lists, with full enforced MFA for priv. accounts.

It uses a module called exchangeOnlineShell which works like the one you are linking to, but without requiring the installation, which is a problem in azure automation, and yes, that would require a hybrid worker as you say.

 

I am sure we will soon see some solutions from microsoft for authenticating with certificates against Exchange Online... (Fingers crossed)

Highlighted

Microsoft have released a complete new module for Exchange with support for modern authentication alot of other improvements. 

 

https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/exchange-online-powershell-v2/e...

 

Highlighted

@MichaelMardahl 

I deployed this and the only thing I changed was the user account name (which is definately in Azure Automation Credentials) and it just gets stuck on 

Logging in to Exchange Online...

 and never stops. The account has permissions and can login using powershell.

Im finding every solution I have tried to use Exchange Online with Azure Automation does the same and gets stuck. Any ideas?