-
Notifications
You must be signed in to change notification settings - Fork 119
/
AzureRM - Audit Logs.ps1
83 lines (45 loc) · 1.9 KB
/
AzureRM - Audit Logs.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
# Sign-in with Azure account credentials
Login-AzureRmAccount
# Select Azure Subscription
$subscriptionId =
(Get-AzureRmSubscription |
Out-GridView `
-Title "Select an Azure Subscription ..." `
-PassThru).SubscriptionId
Select-AzureRmSubscription `
-SubscriptionId $subscriptionId
# Determine owner for each resource from Audit Logs
$resourceOwners = @{}
Get-AzureRmResource | % {
$resourceId = $_.ResourceId
$currentTime = Get-Date
$endTime = $currentTime
$startTime = $endTime.AddDays(-15)
do {
$resourceOwner = (Get-AzureRmLog -StartTime $startTime -EndTime $endTime -ResourceId $resourceId | ? Caller -Like "*@*" | Select-Object -Property Caller -First 1 -Wait).Caller
$endTime = $endTime.AddDays(-15)
$startTime = $startTime.AddDays(-15)
}
until ( ( $resourceOwner ) -or ( $startTime -lt $currentTime.AddDays(-90) ) )
if ( $resourceOwner ) {
$resourceOwners.Add($resourceId, $resourceOwner)
}
}
$resourceOwners | Format-Table -AutoSize
# Alternate approach - pulling all audit logs entries once (faster, but may list resources that are no longer deployed)
$auditLog = $null
$currentTime = Get-Date
$endTime = $currentTime
$startTime = $endTime.AddDays(-15)
do {
$auditLog += Get-AzureRmLog -StartTime $startTime -EndTime $endTime -DetailedOutput
$endTime = $endTime.AddDays(-15)
$startTime = $startTime.AddDays(-15)
}
until ( $startTime -lt $currentTime.AddDays(-90) )
$auditLog |
? Caller -like "*@*" |
? ResourceGroupName -notlike "" |
Sort-Object -Property ResourceGroupName, ResourceId |
Select-Object -Property ResourceGroupName, Caller, ResourceId -Unique
Get-AzureRmResource | ? Tags -like $null