-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKVSecretExpiryEmailNotifications.ps1
More file actions
157 lines (136 loc) · 5.15 KB
/
KVSecretExpiryEmailNotifications.ps1
File metadata and controls
157 lines (136 loc) · 5.15 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
# Sent Email for KeyVault Secrets expiry from Azure Hybrid Worker
[CmdletBinding()]
Param()
Begin {
$dateformat = Get-Date -format 'MM.dd.yyyy.HH.mm.ss'
$LoggingDirectory = "<Folder path>"
$Logpath = "$($LoggingDirectory)\SecretExpiry_$($dateformat).log"
Start-Transcript -Path $Logpath -Force
$verbosepreference = "continue"
$style = @'
<style>
table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%
}
td, th {
border: 1px solid #ddd;
padding: 8px;
}
tr:nth-child(even){background-color:oldlace}
tr:hover {background-color: #ddd}
th{
padding-top: 7px;
padding-bottom: 7px;
text-align: left;
background-color: #08CD11;
color: white
}
</style>
'@
$style1 = @'
<style>
table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%
}
td, th {
border: 1px solid #ddd;
padding: 8px;
}
tr:nth-child(even){background-color:oldlace}
tr:hover {background-color: #ddd}
th{
padding-top: 7px;
padding-bottom: 7px;
text-align: left;
background-color: #CD2F08;
color: white
}
</style>
'@
$ThumbPrint = (Get-ChildItem "Cert:\LocalMachine\my" | Where-Object { $_.Subject -eq "<Subject>" }).Thumbprint
$ApplicationID = "<APPID>"
$TenantID = "<TenantID>"
}
process {
Connect-AzAccount -CertificateThumbprint $thumbprint -ApplicationId $ApplicationID -Tenant $TenantID -ServicePrincipal | Out-Null
$VaultName = "KV_Name"
$DaysNearExpiration = 15
# Set the subscription context
Set-AzContext -SubscriptionId "SUBID" | Out-Null
#Get-AzKeyVault -VaultName $VaultName
$ExpiredSecrets = @()
$NearExpirationSecrets = @()
#gather all key vaults from subscription
if ($VaultName) {
$KeyVaults = Get-AzKeyVault -VaultName $VaultName
}
else {
$KeyVaults = Get-AzKeyVault
}
#check date which will notify about expiration
$ExpirationDate = (Get-Date (Get-Date).AddDays($DaysNearExpiration) -Format yyyyMMdd)
$CurrentDate = (Get-Date -Format yyyyMMdd)
$today = Get-Date
# iterate across all key vaults in subscription
foreach ($KeyVault in $KeyVaults) {
# gather all secrets in each key vault
$SecretsArray = Get-AzKeyVaultSecret -VaultName $KeyVault.VaultName
foreach ($secret in $SecretsArray) {
# check if expiration date is set
if ($secret.Expires) {
$secretExpiration = Get-date $secret.Expires -Format yyyyMMdd
# check if expiration date set on secret is before notify expiration date
if ($ExpirationDate -gt $secretExpiration) {
# check if secret did not expire yet but will expire soon
if ($CurrentDate -lt $secretExpiration) {
$NearExpirationSecrets += [pscustomobject]@{
Name = $secret.Name;
Category = 'SecretNearExpiration';
#KeyVaultName = $KeyVault.VaultName;
ExpirationDate = $secret.Expires;
DaysUntillExpiry = New-TimeSpan -Start $today.ToString('yyyy-MM-dd') -End $($secret.Expires).ToString('yyyy-MM-dd')
}
}
# secret is already expired
else {
$ExpiredSecrets += [pscustomobject]@{
Name = $secret.Name;
Category = 'SecretExpired';
#KeyVaultName = $KeyVault.VaultName;
ExpirationDate = $secret.Expires;
}
}
}
}
}
}
Write-Verbose "Total number of expired secrets: $($ExpiredSecrets.Count)"
#Write-Verbose "$ExpiredSecrets" -Verbose
Write-Verbose "Total number of secrets near expiration: $($NearExpirationSecrets.Count)"
#Write-Output $NearExpirationSecrets
if ($ExpiredSecrets -or $NearExpirationSecrets) {
$Html = $NearExpirationSecrets | ConvertTo-Html -Fragment -PreContent "<h3><center>Weekly Report Created on $(Get-Date) | <font color = #161CE9> Total number of secrets near expiration: $($NearExpirationSecrets.Count)</font></Center></h3>"
[string]$htmlMail = ConvertTo-HTML -Head $style -Body $Html
$Html1 = $ExpiredSecrets | ConvertTo-Html -Fragment -PreContent "<h3><center>Weekly Report Created on $(Get-Date) | <font color = #161CE9> Total number of expired secrets: $($ExpiredSecrets.Count)</font></Center></h3>"
[string]$htmlMail1 = ConvertTo-HTML -Head $style1 -Body $Html1
$parameters = @{
From = '<EMAIL>'
To = @("<EMAIL>")
Subject = "$VaultName | Secret Expiry Notification"
Body = "$htmlMail1 $htmlMail"
BodyAsHTML = $true
SmtpServer = '<SMTP>'
}
Send-MailMessage @parameters
}
else {
Write-Output "There is no secret near to expiry"
}
}
end {
Stop-Transcript
}