-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppRegistrationsWithExpiringCertAndSecrets.ps1
More file actions
217 lines (187 loc) · 7.37 KB
/
AppRegistrationsWithExpiringCertAndSecrets.ps1
File metadata and controls
217 lines (187 loc) · 7.37 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<#
=============================================================================================
Name: Retrieve Entra App registrations with expiring client secrets & certificates
Description: The script retrieves the expiration date of app registrations' client secrets and certificates in Microsoft 365.
Version: 1.0
Website: admindroid.com
Script Highlights:
1. The script automatically verifies and installs the Microsoft Graph PowerShell SDK module (if not installed already) upon your confirmation.
2. Exports all the Entra ID apps with expiring client secrets and certificates into a CSV file.
3. Allows to retrieve app registrations and their client secret expiry details.
4. Allows to retrieve app regsitrations and their certificate expiration details.
4. Allows to export the list soon to expire client secrets and certificates (i.e., 30 days, 90 days, etc)
5. Supports certificate-based authentication (CBA) too.
6. The script is scheduler-friendly.
For detailed Script execution: https://blog.admindroid.com/retrieve-entra-app-registrations-with-expiring-client-secrets-and-certificates/
============================================================================================
#>
Param
(
[switch]$CreateSession,
[string]$TenantId,
[string]$ClientId,
[string]$CertificateThumbprint,
[Switch]$ClientSecretsOnly,
[Switch]$CertificatesOnly,
[int]$SoonToExpireInDays
)
Function Connect_MgGraph
{
#Check for module installation
$Module=Get-Module -Name microsoft.graph.beta -ListAvailable
if($Module.count -eq 0)
{
Write-Host Microsoft Graph PowerShell SDK is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Write-host "Installing Microsoft Graph PowerShell module..."
Install-Module Microsoft.Graph.beta -Repository PSGallery -Scope CurrentUser -AllowClobber -Force
}
else
{
Write-Host "Microsoft Graph Beta PowerShell module is required to run this script. Please install module using Install-Module Microsoft.Graph cmdlet."
Exit
}
}
#Disconnect Existing MgGraph session
if($CreateSession.IsPresent)
{
Disconnect-MgGraph
}
Write-Host Connecting to Microsoft Graph...
if(($TenantId -ne "") -and ($ClientId -ne "") -and ($CertificateThumbprint -ne ""))
{
Connect-MgGraph -TenantId $TenantId -AppId $ClientId -CertificateThumbprint $CertificateThumbprint -NoWelcome
}
else
{
Connect-MgGraph -Scopes "Application.Read.All" -NoWelcome
}
}
Connect_MgGraph
$Location=Get-Location
$ExportCSV = "$Location\AppRegistration_with_Expiring_CertificatesAndSecrets_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm-ss` tt).ToString()).csv"
$ExportResult=""
$ExportResults=@()
$AppCount=0
$PrintedCount=0
if(($CertificatesOnly.IsPresent) -or ($ClientSecretsOnly.IsPresent) -or ($SoonToExpireInDays -ne ""))
{
$SwitchPresent=$True
}
else
{
$SwitchPresent=$false
}
$RequiredProperties=@('DisplayName','AppId','Id','KeyCredentials','PasswordCredentials','CreatedDateTime','SigninAudience')
Get-MgBetaApplication -All -Property $RequiredProperties | foreach {
$AppCount++
$AppName=$_.DisplayName
Write-Progress -Activity "`n Processed App registration: $AppCount - $AppName "
$AppId=$_.Id
$Secrets=$_.PasswordCredentials
$Certificates=$_.KeyCredentials
$AppCreationDate=$_.CreatedDateTime
$SigninAudience=$_.SignInAudience
$Owners=(Get-MgBetaApplicationOwner -ApplicationId $AppId).AdditionalProperties.userPrincipalName
$Owners=$Owners -join ","
if($owners -eq "")
{
$Owners="-"
}
#Process through Secret keys
if(!($CertificatesOnly.IsPresent) -or ($SwitchPresent -eq $false))
{
foreach($Secret in $Secrets )
{
$CredentialType="Client Secret"
$Print=1
$DisplayName=$Secret.DisplayName
$Id=$Secret.KeyId
$CreatedTime=$Secret.StartDateTime
$ExpiryDate=$Secret.EndDateTime
$ExpiryStatusCalculation=(New-TimeSpan -Start (Get-Date).Date -End $ExpiryDate).Days
if($ExpiryStatusCalculation -lt 0)
{
$ExpiryStatus="Expired"
$Print=0
}
else
{
$ExpiryStatus="Active"
$FriendlyExpiryTime="Expires in $ExpiryStatusCalculation days"
}
#Filter for soon-to-expire client secrets
if(($SoonToExpireInDays -ne "") -and (($SoonToExpireInDays -lt $ExpiryStatusCalculation)))
{
$Print=0
}
if($Print -eq 1)
{
$PrintedCount++
$ExportResult=[PSCustomObject]@{'App Name'=$AppName;'App Owners'=$Owners;'App Creation Time'=$AppCreationDate;'Credential Type'=$CredentialType;'Name'=$DisplayName;'Id'=$Id;'Creation Time'=$CreatedTime;'Expiry Date'=$ExpiryDate;'Days to Expiry'=$ExpiryStatusCalculation;'Friendly Expiry Date'=$FriendlyExpiryTime;'App Id'=$AppId}
$ExportResult | Export-Csv -Path $ExportCSV -Notype -Append
}
}
}
if(!($ClientSecretsOnly.IsPresent) -or ($SwitchPresent -eq $false))
{
foreach ($Certificate in $Certificates)
{
$CredentialType="Certificate"
$Print=1
$DisplayName=$Certificate.DisplayName
$Id=$Certificate.KeyId
$CreatedTime=$Certificate.StartDateTime
$ExpiryDate=$Certificate.EndDateTime
$ExpiryStatusCalculation=(New-TimeSpan -Start (Get-Date).Date -End $ExpiryDate).Days
if($ExpiryStatusCalculation -lt 0)
{
$ExpiryStatus="Expired"
$Print=0
}
else
{
$ExpiryStatus="Active"
$FriendlyExpiryTime="Expires in $ExpiryStatusCalculation days"
}
#Filter for soon-to-expire certificates
if(($SoonToExpireInDays -ne "") -and (($SoonToExpireInDays -lt $ExpiryStatusCalculation)))
{
$Print=0
}
if($Print -eq 1)
{
$PrintedCount++
$ExportResult=[PSCustomObject]@{'App Name'=$AppName;'App Owners'=$Owners;'App Creation Time'=$AppCreationDate;'Credential Type'=$CredentialType;'Name'=$DisplayName;'Id'=$Id;'Creation Time'=$CreatedTime;'Expiry Date'=$ExpiryDate;'Days to Expiry'=$ExpiryStatusCalculation;'Friendly Expiry Date'=$FriendlyExpiryTime;'App Id'=$AppId}
$ExportResult | Export-Csv -Path $ExportCSV -Notype -Append
}
}
}
}
#Open output file after execution
If($PrintedCount -eq 0)
{
Write-Host No data found for the given criteria
Write-Host `n~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n
}
else
{
Write-Host `nThe script processed $AppCount app registrations and the output file contains $PrintedCount records.
if((Test-Path -Path $ExportCSV) -eq "True")
{
Write-Host `n The Output file available in: -NoNewline -ForegroundColor Yellow
Write-Host $ExportCSV
Write-Host `n~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n
$Prompt = New-Object -ComObject wscript.shell
$UserInput = $Prompt.popup("Do you want to open output file?",`
0,"Open Output File",4)
If ($UserInput -eq 6)
{
Invoke-Item "$ExportCSV"
}
}
}