I'm try to send List of secrets that are expired and about to expired to email ID . But i cant able to see Secrets that are near about to expire. is anybody help how to get near about to expiry secrets of azure keyVault?
i'm using below code :
if ($secret.Expires) {
$secretExpiration = Get-date $secret.Expires -Format yyyyMMdd
if ($ExpirationDate -gt $secretExpiration) {
if ($CurrentDate -lt $secretExpiration) {
$NearExpirationSecrets += New-Object PSObject -Property @{
Name = $secret.Name;
Category = 'SecretNearExpiration';
KeyVaultName = $KeyVault.VaultName;
ExpirationDate = $secret.Expires;
}
}
You could try my script below, in my sample, A secret that will expire in 7 days
means secret is about to expired
, the script get all the secrets that are about to expired in a keyvault.
$KeyVault = Get-AzKeyVault -ResourceGroupName <group-name> -VaultName joykeyvault
$secrets = Get-AzKeyVaultSecret -VaultName joykeyvault
$Date = Get-Date (Get-Date).AddDays(7) -Format yyyyMMdd
$CurrentDate = Get-Date -Format yyyyMMdd
$NearExpirationSecrets = @()
foreach($secret in $secrets){
if($secret.Expires) {
$secretExpiration = Get-Date $secret.Expires -Format yyyyMMdd
if($secretExpiration -lt $Date -and $secretExpiration -gt $CurrentDate){
$NearExpirationSecrets += New-Object PSObject -Property @{
Name = $secret.Name;
Category = 'SecretNearExpiration';
KeyVaultName = $KeyVault.VaultName;
ExpirationDate = $secret.Expires;
}
}
}
}