Skip to content

Instantly share code, notes, and snippets.

@watahani
Created June 27, 2020 17:19
Show Gist options
  • Save watahani/a8348833c3c834a8132ae58ffcee51e2 to your computer and use it in GitHub Desktop.
Save watahani/a8348833c3c834a8132ae58ffcee51e2 to your computer and use it in GitHub Desktop.
if ($(Get-WindowsFeature -Name Web-Application-Proxy).Installed) {
$hostName = (New-Object System.UriBuilder -ArgumentList (Get-WebApplicationProxyConfiguration).AdfsUrl).Host
$currentCertHash = (Get-WebApplicationProxySslCertificate | Where-Object { $_.HostName -eq $hostName } | Select-Object -First 1 ).CertificateHash
$currentCert = Get-ChildItem cert:\localmachine\my | Where-Object { $_.Thumbprint -eq $currentCertHash }
if ($currentCert.NotAfter -lt (Get-Date).AddDays(30)) {
try {
$cert = Get-ChildItem cert:\LocalMachine\My\ | Where-Object { $_.Subject -eq "CN=$hostName" } | Sort-Object -Property NotAfter -Descending | Select-Object -First 1 ;
$certThumbprint = $cert.Thumbprint
Set-WebApplicationProxySslCertificate -Thumbprint $certThumbprint
Write-Host "Updated Web Application Proxy Certificate with Thumbprint: $certThumbprint"
}
catch {
throw ( New-Object System.Exception( "Failed update Certificate", $_.Exception ))
}
}
else {
return "no need to update Certificate `n`r $currentCert"
}
}
elseif ($(Get-WindowsFeature -Name ADFS-Federation).Installed) {
$hostName = (Get-AdfsProperties).HostName
$currentCertHash = (Get-AdfsSslCertificate | Where-Object { $_.HostName -eq $hostName } | Select-Object -First 1 ).CertificateHash
$currentCert = Get-ChildItem cert:\localmachine\my | Where-Object { $_.Thumbprint -eq $currentCertHash }
if ($currentCert.NotAfter -lt (Get-Date).AddDays(30)) {
try {
$cert = Get-ChildItem cert:\LocalMachine\My\ | Where-Object { $_.Subject -eq "CN=$hostName" } | Sort-Object -Property NotAfter -Descending | Select-Object -First 1 ;
$certThumbprint = $cert.Thumbprint
Set-AdfsSslCertificate -Thumbprint $certThumbprint
Write-Host "Updated AD FS SSL Certificate with Thumbprint: $certThumbprint"
}
catch {
throw ( New-Object System.Exception( "Failed update Certificate", $_.Exception ))
}
}
else {
return "no need to update Certificate `n`r $currentCert"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment