-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure-resources.ps1
85 lines (71 loc) · 3.72 KB
/
azure-resources.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# A script to fetch user sign-in data from the Microsoft Graph and export it to CSV file.
#
#
# Define the values applicable for the application used to connect to the Graph ( **ensure to change the below 3 values of AppId or Client ID, Tenant ID and Client Secret for your application of your tenant**)
$AppId = ""
$TenantId = ""
$AppSecret = ""
# Construct URI and body needed for authentication
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$body = @{
client_id = $AppId
scope = "https://graph.microsoft.com/.default"
client_secret = $AppSecret
grant_type = "client_credentials" }
# Get OAuth 2.0 Token
$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
# Unpack Access Token
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token
# Base URL
$headers = @{Authorization = "Bearer $token"}
# Get User sign in data
Write-Host "Accessing the Graph to get user sign-in data..."
$URI = "https://graph.microsoft.com/beta/users?`$select=displayName,userPrincipalName, mail, id, CreatedDateTime, signInActivity, UserType&`$top=999"
$SignInData = (Invoke-RestMethod -Uri $URI -Headers $Headers -Method Get -ContentType "application/json")
$Report = [System.Collections.Generic.List[Object]]::new()
Foreach ($User in $SignInData.Value) {
If ($Null -ne $User.SignInActivity) {
$LastSignInDateTime = $User.SignInActivity.LastSignInDateTime
$LastNonInteractiveSignInDateTime = $User.SignInActivity.LastNonInteractiveSignInDateTime}
Else { #No sign in data for this user account
$LastSignInDateTime = ""
$LastNonInteractiveSignInDateTime = ""}
$ReportLine = [PSCustomObject] @{
UPN = $User.UserPrincipalName
DisplayName = $User.DisplayName
Email = $User.Mail
Id = $User.Id
Created = $User.CreatedDateTime
LastSignInDateTime = $LastSignInDateTime
LastNonInteractiveSignInDateTime = $LastNonInteractiveSignInDateTime
UserType = $User.UserType }
$Report.Add($ReportLine)
} # End ForEach
# Do we have extra data to fetch?
$NextLink = $SignInData.'@Odata.NextLink'
While ($Null -ne $NextLink) { # We do... so process them.
Write-Host "Still processing..."
$SignInData = Invoke-WebRequest -Method GET -Uri $NextLink -ContentType "application/json" -Headers $Headers
$SignInData = $SignInData | ConvertFrom-JSon
Foreach ($User in $SignInData.Value) {
If ($Null -ne $User.SignInActivity) {
$LastSignInDateTime = $User.SignInActivity.LastSignInDateTime
$LastNonInteractiveSignInDateTime = $User.SignInActivity.LastNonInteractiveSignInDateTime}
Else { #No sign in data for this user account
$LastSignInDateTime = ""
$LastNonInteractiveSignInDateTime = ""}
$ReportLine = [PSCustomObject] @{
UPN = $User.UserPrincipalName
DisplayName = $User.DisplayName
Email = $User.Mail
Id = $User.Id
Created = $User.CreatedDateTime
LastSignInDateTime = $LastSignInDateTime
LastNonInteractiveSignInDateTime = $LastNonInteractiveSignInDateTime
UserType = $User.UserType }
$Report.Add($ReportLine) }
# Check for more data
$NextLink = $SignInData.'@Odata.NextLink'
} # End While
Write-Host "All done. " $Report.Count "accounts processed - output available in c:\Temp\ReportUserSignin.csv."
$Report | Export-CSV -NoTypeInformation .\ReportUserSignin.csv