Hi All,
I am using a script to check all certificate on Azure App Gateway that are going to expire in next 30 days. And somehow I managed to find information but the most important part Name of cert is missing in output.
Please help me to get this populated, since without it is too tough to find cert.
function Test-CertExpiresSoon($cert) {
$span = [TimeSpan]::FromDays(30)
$today = [DateTime]::Today
return ($cert.NotAfter - $today) -lt $span
}
function Decode-Certificate($certBytes) {
$p7b = New-Object System.Security.Cryptography.Pkcs.SignedCms
$p7b.Decode($certBytes)
return $p7b.Certificates[0]
}
$gateways = Get-AzureRmApplicationGateway -Name “appgw-test” -ResourceGroupName “appgw-rg”
foreach ($gw in $gateways) {
foreach ($cert in $gw.SslCertificates) {
$certBytes = [Convert]::FromBase64String($cert.PublicCertData)
$x509 = Decode-Certificate $certBytes
if (Test-CertExpiresSoon $x509) {
[PSCustomObject] @{
ResourceGroup = $gw.ResourceGroupName;
AppGateway = $gw.Name;
CertSubject = $x509.Subject;
CertThumbprint = $x509.Thumbprint;
CertExpiration = $x509.NotAfter;
CertName = $x509.SubjectName;
}
}
}
}
The CertName is coming like this, except this everything is fine:-
CertName : System.Security.Cryptography.X509Certificates.X500DistinguishedName