Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module MSOnline
  2. $keyPath = "C:\O365\key\TENANTNAME.key"
  3. $key = Test-Path $keyPath
  4. if($key){
  5.     $admin = "dirsync@pmuseum.onmicrosoft.com"
  6.     $Password = cat $keyPath | ConvertTo-SecureString
  7.     $Credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $admin, $Password
  8. }else{
  9.     $Credentials = Get-Credential -Message "Please enter your Office 365 admin credentials!"
  10. }
  11. #********************* Check Variables Ends *********************
  12.  
  13. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://ps.outlook.com/powershell/-Credential $Credentials -Authentication Basic -AllowRedirection
  14. #Enter-PSSession  $Session
  15. Import-PSSession $Session -AllowClobber
  16. Connect-MsolService -Credential $Credentials
  17.  
  18.  
  19. #New-ComplianceSearchAction -SearchName "Tafeeda 4" -Purge -PurgeType SoftDelete
  20. #Remove-PSSession $Session
  21. $startDate = (Get-Date).AddDays(-30)
  22. $endDate = (Get-Date)
  23. $Logs = @()
  24. Write-Host "Retrieving logs" -ForegroundColor Blue
  25. do {
  26.     $logs += Search-unifiedAuditLog -SessionCommand ReturnLargeSet -SessionId "UALSearch" -ResultSize 5000 -StartDate $startDate -EndDate $endDate -Operations UserLoggedIn #-SessionId "$($customer.name)"
  27.     Write-Host "Retrieved $($logs.count) logs" -ForegroundColor Yellow
  28. }while ($Logs.count % 5000 -eq 0 -and $logs.count -ne 0)
  29. Write-Host "Finished Retrieving logs" -ForegroundColor Green
  30.  
  31. $userIds = $logs.userIds | Sort-Object -Unique
  32.  
  33. $ignorecountries = 'Palestinian Territory','Israel','Palestine', 'Jordan','Hashemite Kingdom of Jordan'
  34. $count = 1
  35. $starttime = Get-Date
  36. foreach ($userId in $userIds) {
  37.     $ips = @()
  38.     Write-Host "Getting logon IPs for $userId"
  39.     $searchResult = ($logs | Where-Object {$_.userIds -contains $userId}).auditdata | ConvertFrom-Json -ErrorAction SilentlyContinue
  40.     Write-Host "$userId has $($searchResult.count) logs" -ForegroundColor Green
  41.  
  42.     $ips = $searchResult.clientip | Sort-Object -Unique
  43.    
  44.     Write-Host "Found $($ips.count) unique IP addresses for $userId"
  45.     foreach ($ip in $ips) {
  46.         $currenttime = Get-Date
  47.         if($count -gt 44){
  48.             $count = 1
  49.             $timedif = $currenttime - $starttime
  50.             #api-ip allows 45 requests/minute, so wait until the minute has rests, 70 instead of 60 just to be sure
  51.             Start-sleep -s (70 -[int]$timedif.Seconds)
  52.             $starttime = Get-Date
  53.         }
  54.         $count++
  55.         Write-Host "Checking $ip" -ForegroundColor Yellow
  56.         #continue
  57.         $mergedObject = @{}
  58.         $singleResult = $searchResult | Where-Object {$_.clientip -contains $ip} | Select-Object -First 1
  59.         Start-sleep -m 400
  60.         $ipresult = Invoke-restmethod -method get -uri http://ip-api.com/json/$ip
  61.         $UserAgent = $singleResult.extendedproperties.value[0]
  62.         Write-Host "Country: $($ipResult.country) UserAgent: $UserAgent"
  63.         $singleResultProperties = $singleResult | Get-Member -MemberType NoteProperty
  64.         foreach ($property in $singleResultProperties) {
  65.             if ($property.Definition -match "object") {
  66.                 $string = $singleResult.($property.Name) | ConvertTo-Json -Depth 10
  67.                 $mergedObject | Add-Member -Name $property.Name -Value $string -MemberType NoteProperty    
  68.             }
  69.             else {$mergedObject | Add-Member -Name $property.Name -Value $singleResult.($property.Name) -MemberType NoteProperty}          
  70.         }
  71.         $property = $null
  72.         $ipProperties = $ipresult | get-member -MemberType NoteProperty
  73.  
  74.         foreach ($property in $ipProperties) {
  75.             $mergedObject | Add-Member -Name $property.Name -Value $ipresult.($property.Name) -MemberType NoteProperty
  76.         }
  77.         $mergedObject | Select-Object UserId, Operation, CreationTime, @{Name = "UserAgent"; Expression = {$UserAgent}}, Query, ISP, City, RegionName, Country  | export-csv C:\temp\UserLocationDataGCITS.csv -Append -NoTypeInformation
  78.     }
  79. }
  80. Remove-PSSession $Session
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement