-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGet expired Azure Apps and SP from Azure.ps1
176 lines (137 loc) · 5.48 KB
/
Get expired Azure Apps and SP from Azure.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#Settings
$TimeSpanInDays=90
$MailSender="Mail Sender Mail"
$MailRecipient="Mail Recipient Mail"
#Azure App Credentials to get Apps and SP
$EXPIRE_AppId = "your EXPIRE APP Client ID"
$EXPIRE_secret = "your EXPIRE APP Secret"
$tenantID = "Azure Tenant ID"
#Azure App Credentials to send the Mail
$MAIL_AppId = "your Mail Client ID"
$MAIL_secret = "your Mail Secret"
#STOP HERE!
#Connect to GRAPH API with EXPIRE credentials
$EXPIRE_tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $EXPIRE_AppId
Client_Secret = $EXPIRE_secret
}
$EXPIRE_tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $EXPIRE_tokenBody
$EXPIRE_headers = @{
"Authorization" = "Bearer $($EXPIRE_tokenResponse.access_token)"
"Content-type" = "application/json"
}
#Connect to GRAPH API with MAIL Credentials
$MAIL_tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $MAIL_AppId
Client_Secret = $MAIL_secret
}
$MAIL_tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $MAIL_tokenBody
$MAIL_headers = @{
"Authorization" = "Bearer $($MAIL_tokenResponse.access_token)"
"Content-type" = "application/json"
}
#functions
function Get-AzureResourcePaging {
param (
$URL,
$AuthHeader
)
# List Get all Apps from Azure
$Response = Invoke-RestMethod -Method GET -Uri $URL -Headers $AuthHeader
$Resources = $Response.value
$ResponseNextLink = $Response."@odata.nextLink"
while ($ResponseNextLink -ne $null) {
$Response = (Invoke-RestMethod -Uri $ResponseNextLink -Headers $AuthHeader -Method Get)
$ResponseNextLink = $Response."@odata.nextLink"
$Resources += $Response.value
}
return $Resources
}
#Build Array to store PSCustomObject
$Array = @()
# List Get all Apps from Azure
$URLGetApps = "https://graph.microsoft.com/v1.0/applications"
$AllApps = Get-AzureResourcePaging -URL $URLGetApps -AuthHeader $EXPIRE_headers
#Go through each App and add to our Array
foreach ($App in $AllApps) {
$URLGetApp = "https://graph.microsoft.com/v1.0/applications/$($App.ID)"
$App = Invoke-RestMethod -Method GET -Uri $URLGetApp -Headers $EXPIRE_headers
if ($App.passwordCredentials) {
foreach ($item in $App.passwordCredentials) {
$Array += [PSCustomObject]@{
"Type" = "AZAPP"
"displayName" = $app.displayName
"ID" = $App.ID
"AppID" = $app.appId
"SecType" = "Secret"
"Secret" = $item.displayName
"Secret-EndDate" = (Get-date $item.endDateTime)
}
}
}
if ($App.keyCredentials) {
foreach ($item in $App.keyCredentials) {
$Array += [PSCustomObject]@{
'Type' = "AZAPP"
'displayName' = $app.displayName
'ID' = $App.ID
'AppID' = $app.appId
'SecType' = "Zert"
'Secret' = $item.displayName
'Secret-EndDate' = (Get-date $item.endDateTime)
}
}
}
}
#Get all Service Principals
$servicePrincipals = "https://graph.microsoft.com/v1.0/servicePrincipals"
$SPList = Get-AzureResourcePaging -URL $servicePrincipals -AuthHeader $EXPIRE_headers
#Go through each SP and add to our Array
foreach ($SAML in $SPList) {
if ($Saml.passwordCredentials) {
foreach ($PW in $Saml.passwordCredentials) {
$Array += [PSCustomObject]@{
'Type' = "SP"
'displayName' = $SAML.displayName
'ID' = $SAML.id
'AppID' = $Saml.appId
'SecType' = "Secret"
'Secret' = $PW.displayName
'Secret-EndDate' = (Get-date $PW.endDateTime)
}
}
}
}
$ExpireringZerts = $Array | Where-Object -Property Secret-EndDate -Value (Get-Date).AddDays($TimeSpanInDays) -lt | Where-Object -Property Secret-EndDate -Value (Get-Date) -gt
foreach ($Zert in $ExpireringZerts) {
$HTML = $Zert | Convertto-HTML -Fragment -As List
$URLsend = "https://graph.microsoft.com/v1.0/users/$MailSender/sendMail"
$BodyJsonsend = @"
{
"message": {
"subject": "Azure App or SPN will expire soon $($Zert.displayName)",
"body": {
"contentType": "HTML",
"content": "$HTML
<br>
Michael Seidl (au2mator)
<br>
"
},
"toRecipients": [
{
"emailAddress": {
"address": "$MailRecipient"
}
}
]
},
"saveToSentItems": "false"
}
"@
Invoke-RestMethod -Method POST -Uri $URLsend -Headers $MAIL_headers -Body $BodyJsonsend
}