Advertisement
Guest User

Untitled

a guest
Jul 8th, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.    
  3.     This script retrieves Azure Active Directory Risk Sign-in Events from the Microsoft Graph API and send an email alert report.
  4.     Only the active events from the last 30 days will be retrieved (that can be modified via the $filter value in uriGraphEndpoint or removed to get all events).
  5.     See the official documentation for more info:
  6.       https://docs.microsoft.com/en-us/azure/active-directory/active-directory-identityprotection-graph-getting-started
  7.       https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_sendmail
  8.      
  9. #>
  10.  
  11. $ClientID = "xx"
  12. $ClientSecret = "xx"
  13. ##$ClientID = "xx"
  14. ##$ClientSecret = "xx"
  15. $tenantDomain = "xxxxx"
  16.  
  17.  
  18. $loginURL = "https://login.microsoft.com"
  19. $resource = "https://graph.microsoft.com"
  20. $body       = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret}
  21. $oauth      = Invoke-RestMethod -Method Post -Uri $loginURL/$tenantdomain/oauth2/token?api-version=1.0 -Body $body
  22.  
  23. "upn" | Out-File -FilePath "C:\Users\xxxxxx\Desktop\risk.csv"
  24.  
  25. if ($oauth.access_token -ne $null) {
  26.    
  27.     $headerParams = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
  28.  
  29.     [uri]$uriGraphEndpoint = [uri]$uriGraphEndpoint = "https://graph.microsoft.com/beta/identityRiskEvents?`$filter=createdDateTime gt $(Get-Date -date (Get-Date).AddHours(-1).ToUniversalTime() -Format o) and riskEventStatus eq 'active' and riskEventType eq 'ImpossibleTravelRiskEvent'"
  30.  
  31.     $response = Invoke-RestMethod -Method Get -Uri $uriGraphEndpoint.AbsoluteUri -Headers $headerParams
  32.  
  33.     if ($response.value -ne $null) {
  34.  
  35.         foreach ( $event in $response.value ) {
  36.          
  37.         $upn = $event.userPrincipalName
  38.  
  39.         $upn| Out-File -Append "C:\Users\xxxx\Desktop\risk.csv"
  40.  
  41.         }
  42.  
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement