Home
%3CLINGO-SUB%20id%3D%22lingo-sub-608837%22%20slang%3D%22en-US%22%3EMonitoring%20Exchange%20Online%20User%20Client%20Access%20and%20Usage%20with%20Graph%2C%20PowerShell%20and%20Power%20BI%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-608837%22%20slang%3D%22en-US%22%3E%3CP%3E%3C%2FP%3E%3CH2%20id%3D%22toc-hId-1759369785%22%20id%3D%22toc-hId-1760056928%22%3EIntroduction%3C%2FH2%3EAs%20a%20Tenant%20Admin%20of%20an%20Office%20365%20Exchange%20Online%20organization%2C%20have%20you%20ever%20needed%20to%20monitor%20who%2C%20what%2C%20and%20where%20someone%20is%20connecting%20to%20your%20Exchange%20Online%20resources%2C%20like%20accessing%20mailboxes%20on%20mobile%20devices%3F%20I%20ran%20into%20this%20request%20a%20few%20weeks%20ago%2C%20from%20one%20of%20my%20customers.%20After%20hours%20of%20research%2C%20and%20testing%20I%20became%20a%20believer%20in%20the%20power%20of%20%3CA%20href%3D%22https%3A%2F%2Fdeveloper.microsoft.com%2Fen-us%2Fgraph%2F%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EMicrosoft%20Graph%3C%2FA%3E%20(Graph).%20By%20now%20you%E2%80%99re%20probably%20thinking%2C%20what%20is%20an%20Exchange%20engineer%20working%20with%20a%20graphing%20tool%20for%3F%20Well%20last%20month%2C%20that%20is%20exactly%20what%20I%20would%20have%20thought%20too.%20Surprising%20(to%20me)%20Graph%20is%20an%20extremely%20powerful%20tool%20that%20can%20interface%20with%20a%20large%20set%20of%20Microsoft%20services%20and%20technologies%20to%20pull%20data%20and%20perform%20tasks%20within%20the%20service%2Ftechnology.%20Pulling%20sign-in%20data%20from%20Azure%20Active%20Directory%20(AAD)%20is%20a%20breeze%20with%20Graph.%20After%20the%20data%20is%20extracted%2C%20using%20Power%20BI%20for%20visualization%20brings%20your%20reporting%20capabilities%20to%20a%20new%20level!%20Let%E2%80%99s%20walk%20thru%20a%20scenario%20setup%20where%20as%20a%20Tenant%20Admin%20you%20can%20find%20out%20who%20is%20accessing%20mailboxes%20in%20your%20Exchange%20Online%20tenant%20on%20mobile%20devices%2C%20using%20Exchange%20ActiveSync%20protocol%20(which%20is%20used%20by%20default%20mail%20apps%20on%20Apple%20%26amp%3B%20Android%20devices)%20from%20anywhere%20in%20the%20world.%3CP%20class%3D%22note%22%3ENote%3A%20For%20this%20procedure%20to%20work%20for%20you%2C%20you%20need%20to%20have%20two%20subscriptions%3A%20Exchange%20Online%20(like%20E1%20or%20E3)%20and%20Azure%20Active%20Directory%20Premium%20(like%20P1%20or%20P2).%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--792787176%22%20id%3D%22toc-hId--792100033%22%3EAllowing%20Graph%20Access%20to%20AAD%20Audit%20Log%20Data%3C%2FH2%3EAAD%20allows%20application%20access%20through%20the%20App%20Registration%20feature.%20To%20allow%20Microsoft%20Graph%20to%20query%20audit%20log%20data%20from%20AAD%20you%20must%20first%20create%20a%20new%20app%20registration.%20You%20can%20do%20this%20by%20logging%20into%20%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fportal.azure.com%3C%2FA%3E%20and%20going%20to%20%3CB%3E%3CI%3EAzure%20Active%20Directory%20%26gt%3B%20App%20registrations%3C%2FI%3E%3C%2FB%3E%20(you%20may%20also%20see%20one%20option%20as%20%E2%80%98App%20registrations%20(Preview)%E2%80%99%2C%20we%20will%20not%20use%20that%20one).%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi11.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi1%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi1%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi1_thumb1.jpg%22%20width%3D%221000%22%20height%3D%22498%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20From%20here%20simply%20create%20a%20%3CB%3E%3CI%3ENew%20app%20registration%3C%2FI%3E%3C%2FB%3E%20provide%20a%20%3CB%3E%3CI%3EName%3C%2FI%3E%3C%2FB%3E%20and%20enter%20%3CA%20href%3D%22https%3A%2F%2Flocalhost%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flocalhost%3C%2FA%3E%20as%20the%20%3CB%3E%3CI%3ESign-on%20URL%3C%2FI%3E%3C%2FB%3E.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi21.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi2%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi2%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi2_thumb1.jpg%22%20width%3D%22367%22%20height%3D%22282%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20After%20you%20have%20created%20the%20app%20registration%20you%20can%20now%20grant%20the%20required%20permissions%20by%20going%20to%20%3CB%3E%3CI%3ESettings%20%26gt%3B%20API%20Access%20%26gt%3B%20Required%20permissions%3C%2FI%3E%3C%2FB%3E.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi31.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi3%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi3%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi3_thumb1.jpg%22%20width%3D%22651%22%20height%3D%22265%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Now%20%3CB%3E%3CI%3EAdd%3C%2FI%3E%3C%2FB%3E%20%E2%80%98%3CB%3E%3CI%3EMicrosoft%20Graph%E2%80%99%3C%2FI%3E%3C%2FB%3E%20(under%20%E2%80%98Select%20an%20API%E2%80%99)%20and%20grant%20%3CB%3E%3CI%3ERead%20all%20audit%20log%20data%3C%2FI%3E%3C%2FB%3E%20permission%20(under%20%E2%80%98Select%20permissions%E2%80%99).%20Click%20%E2%80%98Done%E2%80%99%20to%20complete%20this%20step.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi41.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi4%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi4%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi4_thumb1.jpg%22%20width%3D%221075%22%20height%3D%22432%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20It%20will%20look%20like%20this%20after%20the%20above%20steps.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi51.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi5%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi5%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi5_thumb1.jpg%22%20width%3D%22978%22%20height%3D%22388%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Next%2C%20you%20will%20need%20to%20commit%20the%20change%20via%20the%20%3CB%3E%3CI%3EGrant%20permissions%3C%2FI%3E%3C%2FB%3E%20button%20by%20clicking%20on%20Yes%2C%20as%20in%20the%20screenshot%20below.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi61.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi6%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi6%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi6_thumb1.jpg%22%20width%3D%22618%22%20height%3D%22222%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20You%E2%80%99ll%20see%20this%20confirmation%20message%20on%20top%20right%20hand%20corner%20in%20the%20Azure%20Admin%20Portal.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi71.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi7%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi7%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi7_thumb1.jpg%22%20width%3D%22438%22%20height%3D%22102%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Next%2C%20we%20need%20to%20create%20the%20key%20secret.%20This%20can%20be%20done%20under%20%3CB%3ESettings%20%26gt%3B%20API%20Access%20%26gt%3B%3C%2FB%3E%20%3CB%3E%3CI%3EKeys%3C%2FI%3E%3C%2FB%3E%20%3CI%3E(keep%20in%20mind%20that%20your%20key%20secret%20will%20only%20be%20displayed%20once%20%26amp%3B%20you%20need%20to%20copy%20it%20for%20later%20use)%3C%2FI%3E.%20Under%20%3CB%3EPasswords%3C%2FB%3E%20section%2C%20give%20the%20key%20a%20short%20%3CB%3Edescription%3C%2FB%3E%20%26amp%3B%20set%20an%20%3CB%3Eexpiration%3C%2FB%3E%20time%2C%20and%20then%20click%20on%20Save%20button%2C%20which%20would%20result%20in%20a%20warning%20message%20(as%20in%20the%20screenshot%20below)%20asking%20you%20to%20copy%20the%20key%20value%2C%20please%20do%20so%20to%20use%20it%20later%20in%20the%20script%20below.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi81.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi8%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi8%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi8_thumb1.jpg%22%20width%3D%22943%22%20height%3D%22403%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%3CH2%20id%3D%22toc-hId-950023159%22%20id%3D%22toc-hId-950710302%22%3EPulling%20the%20data%20with%20PowerShell%3C%2FH2%3ETo%20connect%20to%20Graph%20with%20PowerShell%20you%20first%20need%20to%20obtain%20an%20OAuth%20token%20from%20logon.microsoft.com.%20For%20authentication%2C%20your%20application%20ID%20and%20key%20secret%20is%20used.%20This%20is%20done%20using%20the%20code%20below%3A%20You%20will%20need%20the%20following%20parameters%20for%20the%20PS%20script%20below.%20%3CB%3EApplication%20ID%3A%3C%2FB%3E%20%3CSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi91.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi9%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi9%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi9_thumb1.jpg%22%20width%3D%22729%22%20height%3D%22267%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%3C%2FSTRONG%3E%20%3CB%3EKey%20Secret%3A%3C%2FB%3E%20The%20value%20that%20you%20copied%20earlier%2C%20it%20would%20look%20something%20like%20this%20(example)%3A%206vNGGm5rAB4Zn32rOW9RT%2B4zEaqcx3l92qyGwb%2BvT2c%3D%3CB%3E%3C%2FB%3E%20%3CB%3ETenant%20Domain%3A%3C%2FB%3E%20The%20tenant%20domain%20that%E2%80%99s%20registered%20for%20your%20tenant%20in%20Office%20365%2C%20like%20contoso.com%2C%20for%20example%20(Admin%20Portal%20%7C%20Setup%20%7C%20Domains)%20%3CB%3EDirectory%20Path%3A%3C%2FB%3E%20The%20path%20on%20the%20local%20machine%20to%20save%20the%20output%20CSV%20file%20from%20this%20script%2C%20where%20this%20PS%20script%20is%20being%20executed%20by%20you%20as%20Tenant%20Admin%3CP%20class%3D%22code%22%3E%24ClientID%20%3D%20%22%5BINSERT%20APPLICATION%20ID%5D%22%3CBR%20%2F%3E%24ClientSecret%20%3D%20%22%5BINSERT%20KEY%20SECRET%5D%22%3CBR%20%2F%3E%24TenantDomain%20%3D%20%22%5BINSERT%20TENANT%20DOMAIN%5D%22%3CBR%20%2F%3E%24OutputDirectory%20%3D%20%22%5BINSERT%20DIRECTORY%20PATH%5D%22%3CBR%20%2F%3E%24loginURL%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Flogin.microsoft.com%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flogin.microsoft.com%3C%2FA%3E'%3CBR%20%2F%3E%24resource%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%3C%2FA%3E'%3CBR%20%2F%3E%24body%20%3D%20%40%7Bgrant_type%3D%22client_credentials%22%3Bresource%3D%24resource%3Bclient_id%3D%24ClientID%3Bclient_secret%3D%24ClientSecret%7D%3CBR%20%2F%3E%24oauth%20%3D%20Invoke-RestMethod%20-Method%20Post%20-Uri%20%24loginURL%2F%24TenantDomain%2Foauth2%2Ftoken%3Fapi-version%3D1.0%20-Body%20%24body%3C%2FP%3EOnce%20the%20OAuth%20token%20has%20been%20obtained%2C%20we%20can%20now%20request%20the%20data%20from%20Graph%20using%20a%20web%20request%3A%3CP%20class%3D%22code%22%3E%24headerParams%20%3D%20%40%7BAuthorization%3D%22%24(%24oauth.token_type)%20%24(%24oauth.access_token)%22%7D%3CBR%20%2F%3E%24url%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fbeta%2FauditLogs%2FsignIns%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fbeta%2FauditLogs%2FsignIns%3C%2FA%3E'%3CBR%20%2F%3E%24resultSet%20%3D%20(Invoke-WebRequest%20-UseBasicParsing%20-Headers%20%24headerParams%20-Uri%20%24url)%3C%2FP%3EWe%20can%20now%20filter%20the%20results%20and%20export%20them%20to%20a%20CSV%3A%3CP%20class%3D%22code%22%3E%24output%20%3D%20%40()%3CBR%20%2F%3EForEach(%24event%20in%20(%24resultSet.Content%20%7C%20ConvertFrom-Json).value)%20%7B%3CBR%20%2F%3EIf(%24event.clientAppUsed%20-eq%20%22Exchange%20ActiveSync%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24output%20%2B%3D%20%24event%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24output%20%7C%20Export-CSV%20%22%24OutputDirectory%5CEXOClientAccessUsageReport.csv%22%20-NoTypeInformation%3C%2FP%3EHere%20is%20a%20full%20example%20of%20the%20PowerShell%20script%3A%3CP%20class%3D%22code%22%3E%26lt%3B%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23Disclaimer%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3CBR%20%2F%3EThe%20sample%20scripts%20are%20not%20supported%20under%20any%20Microsoft%20standard%20support%3CBR%20%2F%3Eprogram%20or%20service.%20The%20sample%20scripts%20are%20provided%20AS%20IS%20without%20warranty%3CBR%20%2F%3Eof%20any%20kind.%20Microsoft%20further%20disclaims%20all%20implied%20warranties%20including%2C%3CBR%20%2F%3Ewithout%20limitation%2C%20any%20implied%20warranties%20of%20merchantability%20or%20of%20fitness%20for%3CBR%20%2F%3Ea%20particular%20purpose.%20The%20entire%20risk%20arising%20out%20of%20the%20use%20or%20performance%20of%3CBR%20%2F%3Ethe%20sample%20scripts%20and%20documentation%20remains%20with%20you.%20In%20no%20event%20shall%3CBR%20%2F%3EMicrosoft%2C%20its%20authors%2C%20or%20anyone%20else%20involved%20in%20the%20creation%2C%20production%2C%20or%3CBR%20%2F%3Edelivery%20of%20the%20scripts%20be%20liable%20for%20any%20damages%20whatsoever%20(including%2C%3CBR%20%2F%3Ewithout%20limitation%2C%20damages%20for%20loss%20of%20business%20profits%2C%20business%20interruption%2C%3CBR%20%2F%3Eloss%20of%20business%20information%2C%20or%20other%20pecuniary%20loss)%20arising%20out%20of%20the%20use%3CBR%20%2F%3Eof%20or%20inability%20to%20use%20the%20sample%20scripts%20or%20documentation%2C%20even%20if%20Microsoft%3CBR%20%2F%3Ehas%20been%20advised%20of%20the%20possibility%20of%20such%20damages.%3CBR%20%2F%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23Disclaimer%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%26gt%3B%3CBR%20%2F%3E%23Declare%20unique%20instance%20variables%3CBR%20%2F%3E%24ClientID%20%3D%20%22%5BINSERT%20APPLICATION%20ID%5D%22%3CBR%20%2F%3E%24ClientSecret%20%3D%20%22%5BINSERT%20KEY%20SECRET%5D%22%3CBR%20%2F%3E%24TenantDomain%20%3D%20%22%5BINSERT%20TENANT%20URL%5D%22%3CBR%20%2F%3E%24OutputDirectory%20%3D%20%22%5BINSERT%20DIRECTORY%20PATH%5D%22%3CBR%20%2F%3E%23Declare%20static%20variables%3CBR%20%2F%3E%24loginURL%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Flogin.microsoft.com%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flogin.microsoft.com%3C%2FA%3E'%3CBR%20%2F%3E%24resource%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%3C%2FA%3E'%3CBR%20%2F%3E%23Build%20OAuth%20tequest%3CBR%20%2F%3E%24body%20%3D%20%40%7Bgrant_type%3D%22client_credentials%22%3Bresource%3D%24resource%3Bclient_id%3D%24ClientID%3Bclient_secret%3D%24ClientSecret%7D%3CBR%20%2F%3E%23Request%20OAuth%20token%3CBR%20%2F%3E%24oauth%20%3D%20Invoke-RestMethod%20-Method%20Post%20-Uri%20%24loginURL%2F%24TenantDomain%2Foauth2%2Ftoken%3Fapi-version%3D1.0%20-Body%20%24body%3CBR%20%2F%3E%23If%20OAuth%20was%20successful%20request%20data%20from%20Microsoft%20Graph%3CBR%20%2F%3EIf(%24null%20-ne%20%24oauth.access_token)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%23Build%20Microsoft%20Graph%20web%20request%3CBR%20%2F%3E%24headerParams%20%3D%20%40%7BAuthorization%3D%22%24(%24oauth.token_type)%20%24(%24oauth.access_token)%22%7D%3CBR%20%2F%3E%24url%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fbeta%2FauditLogs%2FsignIns%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fbeta%2FauditLogs%2FsignIns%3C%2FA%3E'%3CBR%20%2F%3E%23Request%20data%20via%20web%20request%20to%20Microsoft%20Graph%3CBR%20%2F%3E%24resultSet%20%3D%20(Invoke-WebRequest%20-UseBasicParsing%20-Headers%20%24headerParams%20-Uri%20%24url)%3CBR%20%2F%3E%23Place%20all%20events%20related%20to%20EXO%20into%20an%20array%3CBR%20%2F%3E%24output%20%3D%20%40()%3CBR%20%2F%3EForEach(%24event%20in%20(%24resultSet.Content%20%7C%20ConvertFrom-Json).value)%20%7B%3CBR%20%2F%3EIf(%24event.appDisplayName%20-like%20%22*Exchange%20*%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24output%20%2B%3D%20%24event%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%23Export%20all%20EXO%20events%20to%20a%20CSV%3CBR%20%2F%3E%24output%20%7C%20Export-CSV%20%22%24OutputDirectory%5CEXOAccessReport.csv%22%20-NoTypeInformation%3CBR%20%2F%3E%7D%3CBR%20%2F%3EElse%3CBR%20%2F%3E%7B%3CBR%20%2F%3EWrite-Error%20%22Failed%20to%20authenticate%20to%20OAuth%2C%20no%20token%20obtained.%22%3CBR%20%2F%3E%7D%3C%2FP%3EAfter%20running%20the%20script%20above%2C%20which%20you%20can%20do%20thru%20Windows%20PowerShell%20on%20any%20Windows%20machine%2C%20you%E2%80%99ll%20have%20a%20csv%20file%20in%20your%20hands%2C%20i.e.%20EXOAccessReport.csv%2C%20in%20the%20output%20directory%20that%20you%20provided%20in%20the%20above%20script.%3CH2%20id%3D%22toc-hId--1602133802%22%20id%3D%22toc-hId--1601446659%22%3EVisualizing%20Data%20with%20Power%20BI%3C%2FH2%3EThe%20geniuses%20that%20developed%20Power%20BI%20(download%20the%20Power%20BI%20desktop%20app%20from%20%3CA%20href%3D%22https%3A%2F%2Faka.ms%2Fpbidesktopstore%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehere%3C%2FA%3E)%20have%20made%20this%20next%20step%20so%20easy%20even%20I%20can%20do%20it!%20Launch%20Power%20BI%20desktop%20app%20and%20simply%20import%20the%20data%20using%20%3CB%3E%3CI%3EGet%20Data%20%26gt%3B%20Text%2FCSV%3C%2FI%3E%3C%2FB%3E%2C%20select%20your%20report%20(if%20you%20used%20the%20defaults%20it%20will%20be%20named%20EXOAccessReport.csv)%2C%20and%20click%20%3CB%3E%3CI%3ELoad%3C%2FI%3E%3C%2FB%3E.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi10.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi10%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi10%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi10_thumb.jpg%22%20width%3D%22269%22%20height%3D%22630%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Once%20this%20is%20complete%20you%20can%20now%20select%20your%20visualization%20(I%20recommend%20ArcGIS%20Maps%2C%20or%20Maps)%2C%20and%20drag%20and%20drop%20the%20data%20fields%20to%20the%20visualization%20fields.%20Drag%20%3CB%3E%3CI%3Elocation%3C%2FI%3E%3C%2FB%3E%20to%20%3CB%3E%3CI%3ELocation%3C%2FI%3E%3C%2FB%3E%20and%20%3CB%3E%3CI%3ESize%3C%2FI%3E%3C%2FB%3E%2C%20%3CB%3E%3CI%3EuserDisplayName%3C%2FI%3E%3C%2FB%3E%20to%20%3CB%3E%3CI%3ELegend%3C%2FI%3E%3C%2FB%3E%2C%20and%20%3CB%3E%3CI%3EclientAppUsed%3C%2FI%3E%3C%2FB%3E%20and%20%3CB%3E%3CI%3EcreatedDateTime%3C%2FI%3E%3C%2FB%3E%20to%20%3CB%3E%3CI%3ETooltips%3C%2FI%3E%3C%2FB%3E.%20Note%20by%20selecting%20%3CB%3E%3CI%3EFirst%20%3C%2FI%3E%3C%2FB%3E(Default)%20for%20%3CB%3E%3CI%3EclientAppUsed%3C%2FI%3E%3C%2FB%3E%20this%20will%20select%20the%20latest%20login%20being%20as%20the%20CSV%20generated%20by%20the%20script%20is%20in%20descending%20order%20from%20most%20recent%20to%20least%20recent.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi111.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi11%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi11%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi11_thumb.jpg%22%20width%3D%22361%22%20height%3D%22767%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Further%20down%20the%20settings%20pane%2C%20you%20will%20find%20%3CB%3E%3CI%3EFilters%3C%2FI%3E%3C%2FB%3E%20the%20add%20%3CB%3E%3CI%3EclientAppUsed%2C%20userDisplayName%2C%3C%2FI%3E%3C%2FB%3E%20%3CB%3E%3CI%3Elocation%2C%20%3C%2FI%3E%3C%2FB%3Eand%3CB%3E%3CI%3E%20createdDateTime%3C%2FI%3E%3C%2FB%3E%20to%20%3CB%3E%3CI%3EReport%20level%20filters%3C%2FI%3E%3C%2FB%3E.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi12.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi12%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi12%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi12_thumb.jpg%22%20width%3D%22272%22%20height%3D%22700%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Finally%20you%20can%20review%20your%20report%20and%20publish%20it%20to%20your%20workspace%20in%20Power%20BI.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi13.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi13%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi13%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi13_thumb.jpg%22%20width%3D%2280%22%20height%3D%2298%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Remember%20that%20in%20order%20for%20Power%20BI%20to%20access%20the%20data%20from%20the%20cloud%20your%20Power%20BI%20Gateway%20must%20have%20access%20to%20the%20CSV%20file%20generated%20by%20the%20PowerShell%20script.%20See%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpower-bi%2Fservice-gateway-onprem%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EOn-premises%20data%20gateway%3C%2FA%3E%20for%20information%20and%20instructions%20on%20how%20to%20setup%20a%20Power%20BI%20gateway.%3CH2%20id%3D%22toc-hId-140676533%22%20id%3D%22toc-hId-141363676%22%3EKeeping%20it%20fresh%3C%2FH2%3ETo%20ensure%20that%20the%20report%20is%20always%20up%20to%20date%20with%20the%20latest%20data%2C%20I%20recommend%20you%20configure%20the%20PowerShell%20script%20to%20run%20at%20an%20interval%20that%20meets%20your%20needs.%20This%20task%20could%20be%20easily%20accomplished%20by%20Task%20Scheduler%2C%20System%20Center%20Orchestrator%2C%20and%20many%20other%20task%20scheduling%20solutions.%20For%20the%20purposes%20of%20this%20post%20we%20will%20use%20Task%20Scheduler%20since%20it%20is%20readily%20available%20on%20most%20versions%20of%20Windows.%20On%20the%20machine%20the%20script%20will%20be%20running%20from%2C%20launch%20Task%20Scheduler%20by%20pressing%20%3CB%3E%3CI%3EWindows%20%2B%20R%3C%2FI%3E%3C%2FB%3E%20then%20typing%20%3CB%3E%3CI%3Etaskschd.msc%3C%2FI%3E%3C%2FB%3E%20and%20clicking%20%3CB%3E%3CI%3EOK%3C%2FI%3E%3C%2FB%3E.%20Click%20%3CB%3E%3CI%3ECreate%20Task%3C%2FI%3E%3C%2FB%3E%20in%20the%20%3CB%3E%3CI%3EActions%3C%2FI%3E%3C%2FB%3E%20pane%20on%20the%20right%20hand%20side%20of%20the%20window.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi14.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi14%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi14%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi14_thumb.jpg%22%20width%3D%22952%22%20height%3D%22259%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20%3CB%3E%3CI%3EName%3C%2FI%3E%3C%2FB%3E%20your%20task%2C%20select%20%3CB%3E%3CI%3ERun%20whether%20user%20is%20logged%20on%20or%20not%3C%2FI%3E%3C%2FB%3E%20and%20check%20%3CB%3E%3CI%3ERun%20with%20highest%20privileges%3C%2FI%3E%3C%2FB%3E.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi15.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi15%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi15%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi15_thumb.jpg%22%20width%3D%22603%22%20height%3D%22457%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Select%20the%20%3CB%3E%3CI%3ETriggers%3C%2FI%3E%3C%2FB%3E%20tab%20and%20click%20%3CB%3E%3CI%3ENew%E2%80%A6%3C%2FI%3E%3C%2FB%3E%20Check%20%3CB%3E%3CI%3ERepeat%20task%20every%3C%2FI%3E%3C%2FB%3E%2C%20select%20%3CB%3E%3CI%3E30%20minutes%3C%2FI%3E%3C%2FB%3E%20for%20the%20interval%2C%20and%20%3CB%3E%3CI%3EIndefinitely%3C%2FI%3E%3C%2FB%3E%20for%20the%20duration%20then%20click%20%3CB%3E%3CI%3EOK%3C%2FI%3E%3C%2FB%3E.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi16.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi16%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi16%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi16_thumb.jpg%22%20width%3D%22605%22%20height%3D%22528%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Select%20the%20%3CB%3E%3CI%3EActions%3C%2FI%3E%3C%2FB%3E%20tab%2C%20then%20click%20%3CB%3E%3CI%3ENew%E2%80%A6%3C%2FI%3E%3C%2FB%3E%20Type%20%3CB%3E%3CI%3Epowershell.exe%3C%2FI%3E%3C%2FB%3E%20into%20%3CB%3E%3CI%3EProgram%2Fscript%3C%2FI%3E%3C%2FB%3E%20and%20enter%20the%20full%20path%20to%20your%20script%20into%20%3CB%3E%3CI%3EAdd%20arguments%20(optional)%3C%2FI%3E%3C%2FB%3E%20then%20click%20%3CB%3E%3CI%3EOK%3C%2FI%3E%3C%2FB%3E.%20Note%3A%20If%20there%20is%20a%20space%20in%20the%20full%20path%20to%20your%20script%20you%20must%20put%20a%20%3CB%3E%3CI%3E%E2%80%9C%3C%2FI%3E%3C%2FB%3E%20at%20the%20beginning%20and%20end%20of%20the%20path.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi17.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi17%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi17%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi17_thumb.jpg%22%20width%3D%22611%22%20height%3D%22672%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Click%20%3CB%3E%3CI%3EOK%3C%2FI%3E%3C%2FB%3E%20then%20provide%20your%20credentials%20or%20the%20credentials%20you%20are%20running%20the%20task%20as%20into%20the%20prompt.%3CH2%20id%3D%22toc-hId-1883486868%22%20id%3D%22toc-hId-1884174011%22%3EReading%20the%20Report%3C%2FH2%3ENow%20that%20we%20have%20created%20an%20amazing%20Power%20BI%20visualization%20how%20do%20we%20view%20it%3F%20Navigate%20to%20%3CA%20href%3D%22https%3A%2F%2Fapp.powerbi.com%2Fgroups%2Fme%2Flist%2Freports%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fapp.powerbi.com%2Fgroups%2Fme%2Flist%2Freports%3C%2FA%3E%20then%20click%20on%20the%20report%20we%20just%20created.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi18.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi18%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi18%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi18_thumb.jpg%22%20width%3D%221086%22%20height%3D%22295%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20Once%20the%20report%20launches%20and%20populates%20with%20the%20latest%20data%20you%20can%20see%20all%20user%20logins%20plotted%20on%20the%20map%20with%20circles.%20The%20circle%20gets%20larger%20when%20more%20users%20login%20from%20the%20same%20location.%20In%20the%20report%20below%20we%20can%20see%20an%20anomaly%20with%20one%20users%20login%20location.%20By%20hovering%20over%20the%20circle%20we%20can%20see%20the%20location%20(where)%2C%20display%20name%20(who)%2C%20number%20of%20times%20the%20user%20logged%20in%2C%20what%20technology%2Fmethod%20(what)%20was%20used%20to%20login%20during%20the%20most%20recent%20sign-in%2C%20and%20the%20date%20and%20time%20of%20the%20latest%20login.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi19.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi19%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi19%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi19_thumb.jpg%22%20width%3D%22907%22%20height%3D%22553%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20If%20there%20are%20multiple%20logins%20from%20this%20location%2C%20there%20could%20be%20multiple%20methods%20used%20to%20login.%20To%20determine%20which%20methods%20were%20used%20we%20can%20use%20the%20%3CB%3E%3CI%3EFilters%3C%2FI%3E%3C%2FB%3E%20pane%20and%20apply%20a%20%3CB%3E%3CI%3Elocation%3C%2FI%3E%3C%2FB%3E%20and%20%3CB%3E%3CI%3EuserDisplayName%3C%2FI%3E%3C%2FB%3E%20filter.%20Once%20this%20is%20complete%20when%20we%20expand%20%3CB%3E%3CI%3EclientAppUsed%3C%2FI%3E%3C%2FB%3E%20only%20the%20technology%2Fmethod%20of%20login%20is%20left%2C%20in%20this%20case%20%3CB%3E%3CI%3EExchange%20ActiveSync%3C%2FI%3E%3C%2FB%3E.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi20.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi20%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi20%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi20_thumb.jpg%22%20width%3D%22337%22%20height%3D%22936%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%3CH2%20id%3D%22toc-hId--668670093%22%20id%3D%22toc-hId--667982950%22%3EConclusion%3C%2FH2%3EUsing%20Microsoft%20Graph%2C%20PowerShell%2C%20Task%20Scheduler%2C%20and%20Power%20BI%20we%20created%20an%20auto%20updating%20report%20to%20track%20Exchange%20Online%20user%20logins.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi211.jpg%22%20target%3D%22_blank%22%3E%3CIMG%20title%3D%22Powerbi21%22%20style%3D%22border%3A%200px%20currentcolor%22%20alt%3D%22Powerbi21%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Flegacyfs%2Fonline%2Fmedia%2F2019%2F01%2FPowerbi21_thumb.jpg%22%20width%3D%22490%22%20height%3D%22430%22%20border%3D%220%22%20%2F%3E%3C%2FA%3E%20With%20our%20new%20skills%20Exchange%20Online%20doesn%E2%80%99t%20mark%20the%20end%20of%20custom%20reporting%20for%20organizations.%20With%20Microsoft%20Graph%20and%20Power%20BI%20our%20ability%20to%20generate%20custom%20reports%20is%20stronger%20than%20ever!%20Happy%20Graphing!%20%3CSPAN%20class%3D%22author%22%3EDana%20Garcia%3C%2FSPAN%3E%3C%2FLINGO-BODY%3E

Introduction

As a Tenant Admin of an Office 365 Exchange Online organization, have you ever needed to monitor who, what, and where someone is connecting to your Exchange Online resources, like accessing mailboxes on mobile devices? I ran into this request a few weeks ago, from one of my customers. After hours of research, and testing I became a believer in the power of Microsoft Graph (Graph). By now you’re probably thinking, what is an Exchange engineer working with a graphing tool for? Well last month, that is exactly what I would have thought too. Surprising (to me) Graph is an extremely powerful tool that can interface with a large set of Microsoft services and technologies to pull data and perform tasks within the service/technology. Pulling sign-in data from Azure Active Directory (AAD) is a breeze with Graph. After the data is extracted, using Power BI for visualization brings your reporting capabilities to a new level! Let’s walk thru a scenario setup where as a Tenant Admin you can find out who is accessing mailboxes in your Exchange Online tenant on mobile devices, using Exchange ActiveSync protocol (which is used by default mail apps on Apple & Android devices) from anywhere in the world.

Note: For this procedure to work for you, you need to have two subscriptions: Exchange Online (like E1 or E3) and Azure Active Directory Premium (like P1 or P2).

Allowing Graph Access to AAD Audit Log Data

AAD allows application access through the App Registration feature. To allow Microsoft Graph to query audit log data from AAD you must first create a new app registration. You can do this by logging into https://portal.azure.com and going to Azure Active Directory > App registrations (you may also see one option as ‘App registrations (Preview)’, we will not use that one). Powerbi1 From here simply create a New app registration provide a Name and enter https://localhost as the Sign-on URL. Powerbi2 After you have created the app registration you can now grant the required permissions by going to Settings > API Access > Required permissions. Powerbi3 Now AddMicrosoft Graph’ (under ‘Select an API’) and grant Read all audit log data permission (under ‘Select permissions’). Click ‘Done’ to complete this step. Powerbi4 It will look like this after the above steps. Powerbi5 Next, you will need to commit the change via the Grant permissions button by clicking on Yes, as in the screenshot below. Powerbi6 You’ll see this confirmation message on top right hand corner in the Azure Admin Portal. Powerbi7 Next, we need to create the key secret. This can be done under Settings > API Access > Keys (keep in mind that your key secret will only be displayed once & you need to copy it for later use). Under Passwords section, give the key a short description & set an expiration time, and then click on Save button, which would result in a warning message (as in the screenshot below) asking you to copy the key value, please do so to use it later in the script below. Powerbi8

Pulling the data with PowerShell

To connect to Graph with PowerShell you first need to obtain an OAuth token from logon.microsoft.com. For authentication, your application ID and key secret is used. This is done using the code below: You will need the following parameters for the PS script below. Application ID: Powerbi9 Key Secret: The value that you copied earlier, it would look something like this (example): 6vNGGm5rAB4Zn32rOW9RT+4zEaqcx3l92qyGwb+vT2c= Tenant Domain: The tenant domain that’s registered for your tenant in Office 365, like contoso.com, for example (Admin Portal | Setup | Domains) Directory Path: The path on the local machine to save the output CSV file from this script, where this PS script is being executed by you as Tenant Admin

$ClientID = "[INSERT APPLICATION ID]"
$ClientSecret = "[INSERT KEY SECRET]"
$TenantDomain = "[INSERT TENANT DOMAIN]"
$OutputDirectory = "[INSERT DIRECTORY PATH]"
$loginURL = 'https://login.microsoft.com'
$resource = 'https://graph.microsoft.com'
$body = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret}
$oauth = Invoke-RestMethod -Method Post -Uri $loginURL/$TenantDomain/oauth2/token?api-version=1.0 -Body $body

Once the OAuth token has been obtained, we can now request the data from Graph using a web request:

$headerParams = @{Authorization="$($oauth.token_type) $($oauth.access_token)"}
$url = 'https://graph.microsoft.com/beta/auditLogs/signIns'
$resultSet = (Invoke-WebRequest -UseBasicParsing -Headers $headerParams -Uri $url)

We can now filter the results and export them to a CSV:

$output = @()
ForEach($event in ($resultSet.Content | ConvertFrom-Json).value) {
If($event.clientAppUsed -eq "Exchange ActiveSync")
{
$output += $event
}
}
$output | Export-CSV "$OutputDirectory\EXOClientAccessUsageReport.csv" -NoTypeInformation

Here is a full example of the PowerShell script:

<###############Disclaimer#####################################################
The sample scripts are not supported under any Microsoft standard support
program or service. The sample scripts are provided AS IS without warranty
of any kind. Microsoft further disclaims all implied warranties including,
without limitation, any implied warranties of merchantability or of fitness for
a particular purpose. The entire risk arising out of the use or performance of
the sample scripts and documentation remains with you. In no event shall
Microsoft, its authors, or anyone else involved in the creation, production, or
delivery of the scripts be liable for any damages whatsoever (including,
without limitation, damages for loss of business profits, business interruption,
loss of business information, or other pecuniary loss) arising out of the use
of or inability to use the sample scripts or documentation, even if Microsoft
has been advised of the possibility of such damages.
###############Disclaimer#####################################################>
#Declare unique instance variables
$ClientID = "[INSERT APPLICATION ID]"
$ClientSecret = "[INSERT KEY SECRET]"
$TenantDomain = "[INSERT TENANT URL]"
$OutputDirectory = "[INSERT DIRECTORY PATH]"
#Declare static variables
$loginURL = 'https://login.microsoft.com'
$resource = 'https://graph.microsoft.com'
#Build OAuth tequest
$body = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret}
#Request OAuth token
$oauth = Invoke-RestMethod -Method Post -Uri $loginURL/$TenantDomain/oauth2/token?api-version=1.0 -Body $body
#If OAuth was successful request data from Microsoft Graph
If($null -ne $oauth.access_token)
{
#Build Microsoft Graph web request
$headerParams = @{Authorization="$($oauth.token_type) $($oauth.access_token)"}
$url = 'https://graph.microsoft.com/beta/auditLogs/signIns'
#Request data via web request to Microsoft Graph
$resultSet = (Invoke-WebRequest -UseBasicParsing -Headers $headerParams -Uri $url)
#Place all events related to EXO into an array
$output = @()
ForEach($event in ($resultSet.Content | ConvertFrom-Json).value) {
If($event.appDisplayName -like "*Exchange *")
{
$output += $event
}
}
#Export all EXO events to a CSV
$output | Export-CSV "$OutputDirectory\EXOAccessReport.csv" -NoTypeInformation
}
Else
{
Write-Error "Failed to authenticate to OAuth, no token obtained."
}

After running the script above, which you can do thru Windows PowerShell on any Windows machine, you’ll have a csv file in your hands, i.e. EXOAccessReport.csv, in the output directory that you provided in the above script.

Visualizing Data with Power BI

The geniuses that developed Power BI (download the Power BI desktop app from here) have made this next step so easy even I can do it! Launch Power BI desktop app and simply import the data using Get Data > Text/CSV, select your report (if you used the defaults it will be named EXOAccessReport.csv), and click Load. Powerbi10 Once this is complete you can now select your visualization (I recommend ArcGIS Maps, or Maps), and drag and drop the data fields to the visualization fields. Drag location to Location and Size, userDisplayName to Legend, and clientAppUsed and createdDateTime to Tooltips. Note by selecting First (Default) for clientAppUsed this will select the latest login being as the CSV generated by the script is in descending order from most recent to least recent. Powerbi11 Further down the settings pane, you will find Filters the add clientAppUsed, userDisplayName, location, and createdDateTime to Report level filters. Powerbi12 Finally you can review your report and publish it to your workspace in Power BI. Powerbi13 Remember that in order for Power BI to access the data from the cloud your Power BI Gateway must have access to the CSV file generated by the PowerShell script. See On-premises data gateway for information and instructions on how to setup a Power BI gateway.

Keeping it fresh

To ensure that the report is always up to date with the latest data, I recommend you configure the PowerShell script to run at an interval that meets your needs. This task could be easily accomplished by Task Scheduler, System Center Orchestrator, and many other task scheduling solutions. For the purposes of this post we will use Task Scheduler since it is readily available on most versions of Windows. On the machine the script will be running from, launch Task Scheduler by pressing Windows + R then typing taskschd.msc and clicking OK. Click Create Task in the Actions pane on the right hand side of the window. Powerbi14 Name your task, select Run whether user is logged on or not and check Run with highest privileges. Powerbi15 Select the Triggers tab and click New… Check Repeat task every, select 30 minutes for the interval, and Indefinitely for the duration then click OK. Powerbi16 Select the Actions tab, then click New… Type powershell.exe into Program/script and enter the full path to your script into Add arguments (optional) then click OK. Note: If there is a space in the full path to your script you must put a at the beginning and end of the path. Powerbi17 Click OK then provide your credentials or the credentials you are running the task as into the prompt.

Reading the Report

Now that we have created an amazing Power BI visualization how do we view it? Navigate to https://app.powerbi.com/groups/me/list/reports then click on the report we just created. Powerbi18 Once the report launches and populates with the latest data you can see all user logins plotted on the map with circles. The circle gets larger when more users login from the same location. In the report below we can see an anomaly with one users login location. By hovering over the circle we can see the location (where), display name (who), number of times the user logged in, what technology/method (what) was used to login during the most recent sign-in, and the date and time of the latest login. Powerbi19 If there are multiple logins from this location, there could be multiple methods used to login. To determine which methods were used we can use the Filters pane and apply a location and userDisplayName filter. Once this is complete when we expand clientAppUsed only the technology/method of login is left, in this case Exchange ActiveSync. Powerbi20

Conclusion

Using Microsoft Graph, PowerShell, Task Scheduler, and Power BI we created an auto updating report to track Exchange Online user logins. Powerbi21 With our new skills Exchange Online doesn’t mark the end of custom reporting for organizations. With Microsoft Graph and Power BI our ability to generate custom reports is stronger than ever! Happy Graphing! Dana Garcia
22 Comments
Not applicable
Hi,

Using your example script, I needed add some ' '

$loginURL = 'https://login.microsoft.com'

$resource = 'https://graph.microsoft.com'

$url = 'https://graph.microsoft.com/beta/auditLogs/signIns'

Or else, I got the:

Invoke-WebRequest : Cannot validate argument on parameter 'Uri'. The argument is null or empty. Provide an argument that is not null or emp

ty, and then try the command again.

Then it worked great.

Thanks.

Not applicable
Should be fixed now; thanks!
Not applicable
Wondering if you might be able to help me. I am geting a 403 error.

$resultSet = (Invoke-WebRequest -UseBasicParsing -Headers $headerParams -Uri $url)

Invoke-WebRequest : The remote server returned an error: (403) Forbidden.

Not applicable
JFoley2222,

During the creation and testing of this article we encountered this only when the tenant wasn't licensed for Azure AD Premium 1 or 2. You can start a trail on your tenant if you don't already have it by going here: https://azure.microsoft.com/en-us/trial/get-started-active-directory/.

Thanks,

Dana Garcia

Not applicable
I confirmed we are Azure AD Premium P1. I will continue to look at why I might be seeing this.
Not applicable
JFoley2222,

In this case, I would look at the application registration with Azure AD and Graph. Ensure that Graph has been granted permissions to Read all audit log data and that the "Grant Permissions" button was clicked followed by "Yes". Also I would generate a new Key Secret and triple check the values you put in the script for any leading or trailing spaces. Please let me know if this doesn't resolve your issue.

Thanks,

Dana Garcia

Not applicable
will it work off365 Azure AD ? I am getting

PS C:\Users\Satya> C:\satyam\scrips\o365loginaccess.ps1

Invoke-WebRequest : The remote server returned an error: (403) Forbidden.

Not applicable
One last thing is, make sure the domain you specified is your default domain I.E. if you have a custom domain contoso.com and your default tenant domain is contoso.onmicrosoft.com you would need to provide contoso.onmicrosoft.com.

Thanks,

Dana Garcia

Not applicable
I did make that change earlier as I had a domain other than the default listed. I am still seeing the 403 error but at some point it had to have worked because I have data in my CSV file. Just not sure it is updating.
Not applicable
Hi Satya11,

The requirements to use this solution are to have a tenant with Azure AD Premium 1 or 2, or a trial subscription for either of those.

Thanks,

Dana Garcia

Not applicable
I believe you have to be a global admin to run the script
Not applicable
Supplementary : @Carolyn Billups - if I change the logon to a user logon rather than logging on as a the app itself, then if the user has suitable privileges it works every time. Trying to do this as set out here - so the person running the script doesn't even need to have their own account in the tenant, but uses app's ID and Secret as a credential to for one scope of tasks (reading logs) seems to be unreliable.

I'm using it with a different mode of logging on and ( @ Dana) I've found 3 or 4 really useful nuggets of knowledge out of this - so thanks for the good work !

Not applicable
In my case, something is weird. Most of the time I am getting the 403 error. Sometimes I do not. I can run the script 100 times in a row and about 90 of the times I get the 403, 10 it works fine. Either way it appears as though even when I do get the 403, it is pulling data.
Not applicable
I'm getting the same as spadster and Jfoley2222 - I have Azure AD Premium P1, and the first time I ran the script it returned data, then it seemed to be 50/50 whether I would get a 403 error. And then it seemed to be almost every time. I had it working with my tenant guid as the logon name, but it makes no difference if I use [whatever].com

if I download the schema from https://graph.microsoft.com/beta/$metadata it doesn't show auditlogs in the entityContainer object , which makes me wonder if this is quite finished yet.

Not applicable
So in 2019, on a technetium blog, the suggestion is to run a powershell script as task on a Windows server machine to export a CSV to be used with Power BI gateway? Really guys?

Remove all the on-premises using an Azure storage account and a Logic App.

This should be in my opinion what is expected from Microsoft nowdays

Not applicable
Hi Francesco,

As mentioned in the article you can setup the PowerShell to run on a schedule in many different software solutions to achieve the same result. This article guided readers through using Task Scheduler because it is by far the most readily available tool everyone has access to. Your solution of using an Azure logic app is a great idea, I went with an Azure RunBook myself. Thanks for the feedback and have a great day!

Thanks,

Dana Garcia

Not applicable
I agree with @Francesco Ares Sodano, it does look messy and not right for 2019. Why can't we connect to Graph API straight from Power BI?
Not applicable
This looks handy, however, on first run of the script it begins to gather data then fails with a JSON error along the lines of;

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

Not applicable
Additionally, half the time the script will gather data, the other half the time it returns;

Invoke-WebRequest : The remote server returned an error: (403) Forbidden.

At D:\Scripts\oauth.ps1:16 char:19

+ $resultSettemp = (Invoke-WebRequest -UseBasicParsing -Headers $headerParams -Uri ...

Not sure if the 'https://graph.microsoft.com/beta/auditLogs/signIns' service is throttled?

Not applicable
Spadster,

In regards to your JSON length error it sounds like the length of the results it too great. You might be able to solve this by adding a filter to our GET request. To do this you would need to change the line $url = 'https://graph.microsoft.com/beta/auditLogs/signIns' to $url = 'https://graph.microsoft.com/beta/auditLogs/signIn?&$filter=createdDateTime ge ' + (Get-Date -Format 'yyyy-MM-DD').AddDays(-7). What this will do is only pull back signIns for the last 7 days, if you still experience the JSON length error you might have to shorten it by increasing the date closer to the current date. For the 403 error that you are experiencing, this could be do to either permissions or a claims issue. Do you have any claims access policies that could be effecting this? If the error states insufficient_claims this is likely what is causing this issue.

Thanks,

Dana Garcia

Not applicable
For those who are experiencing the "Invoke-WebRequest: The remote server returned an error: (403) Forbidden." error, I was also getting this error due to the fact that I wasn't using the Azure Premium Subscription. I just assigned the Azure P2 Subscription and the error's gone. By the way, nice tip Dana!
Not applicable
Denis,

Thanks for letting us know this helped you. Happy graphing!

Thanks,

Dana Garcia