-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzureAd-Entries.ps1
99 lines (76 loc) · 3.81 KB
/
AzureAd-Entries.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
# Install-module -Name AzureAD
# Imports the AzureAD Module
Import-Module AzureAD
# Establishes a connection to AzureAD
Connect-AzureAD
# Placeholder for certificate based authentication
#$tenantId = "***"
#$applicationId = "***"
#$thumb = "***"
#Connect-AzureAD -TenantId $tenantId -ApplicationId $applicationId -CertificateThumbprint $thumb
# Sets the number of days before expiration to send the reminder message
$reminderDays = 60
# Gets the current date
$now = Get-Date
# Initializes the list for expiring entries
$expiringEntries = @()
# Function to send the summary of reminders
function Send-Notification {
param (
[string[]]$Messages
)
# SMTP server details. Adjust these settings to match your environment
$smtpServer = "mail.example.com"
$smtpFrom = "[email protected]"
$smtpTo = "[email protected]" # You can add more recipients by separating them with commas
$smtpSubject = "Reminder: AzureAD Entries expiring soon"
$smtpBody = $Messages -join "`n`n"
$useSmtpAuth = $true # Set to $false if SMTP authentication is not required
$smtpUsername = ""
$smtpPassword = ""
if ($useSmtpAuth) {
# Converts the password into a SecureString object
$securePassword = ConvertTo-SecureString $smtpPassword -AsPlainText -Force
# Sets the credentials
$smtpCredentials = New-Object System.Management.Automation.PSCredential ($smtpUsername, $securePassword)
# Ignores certificate validation
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
# Sends the email with authentication
Send-MailMessage -SmtpServer $smtpServer -From $smtpFrom -To $smtpTo -Subject $smtpSubject -Body $smtpBody -Credential $smtpCredentials -UseSsl
# Resets the callback function for certificate validation
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
}
else {
# Ignores certificate validation
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
# Sends the email without authentication
Send-MailMessage -SmtpServer $smtpServer -From $smtpFrom -To $smtpTo -Subject $smtpSubject -Body $smtpBody -UseSsl
# Resets the callback function for certificate validation
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
}
}
# Expiration finding for SAML and AzureAD applications
$SAMLApps = Get-AzureADServicePrincipal -All $true | Where-Object { ($_.Tags -contains "WindowsAzureActiveDirectoryGalleryApplicationNonPrimaryV1") -or ($_.Tags -contains "WindowsAzureActiveDirectoryCustomSingleSignOnApplication") -or ($_.Tags -contains "WindowsAzureActiveDirectoryIntegratedApp") }
foreach ($App in $SAMLApps) {
foreach ($KeyCredential in $App.KeyCredentials) {
if ($KeyCredential.EndDate -lt $now.AddDays($reminderDays)) {
$expiringEntries += "SAML Application Name: $($App.DisplayName)`nKey ID: $($KeyCredential.KeyId)`nStart Date: $($KeyCredential.StartDate.ToString("yyyy-MM-dd HH:mm:ss"))`nExpiration Date: $($KeyCredential.EndDate.ToString("yyyy-MM-dd HH:mm:ss"))"
}
}
}
$applications = Get-AzureADApplication -All $true
foreach ($application in $applications) {
$secrets = Get-AzureADApplicationPasswordCredential -ObjectId $application.ObjectId
foreach ($secret in $secrets) {
if ($null -ne $secret.EndDate -and ($secret.EndDate - $now).TotalDays -lt $reminderDays) {
$expiringEntries += "AzureAD Application Name: $($application.DisplayName)`nKey ID: $($secret.KeyId)`nStart Date: $($secret.StartDate.ToString("yyyy-MM-dd HH:mm:ss"))`nExpiration Date: $($secret.EndDate.ToString("yyyy-MM-dd HH:mm:ss"))"
}
}
}
# Checks if expiring entries exist and then sends an email
if ($expiringEntries.Count -gt 0) {
Send-Notification -Messages $expiringEntries
}
else {
Write-Host "No entries expiring soon found." -ForegroundColor Green
}