In a previous article hatte ich bereits beschrieben, wie die kostenlosen Let’s Encrypt Zertifikate mittels PowerShell angefordert und den Exchange Diensten zugewiesen werden können.
Derzeit arbeite ich daran, den kompletten Prozess vom Anfordern des Let’s Encrypt Zertifikats bis zum automatischen Erneuern mit der PowerShell zu automatisieren. Ziel ist ein Fire-and-Forget Script für die Exchange Zertifikate, mal sehen ob es klappt.
In dem Anfangs verlinkten Artikel klappte bereits das Anfordern eines Zertifikats via Let’s Encrypt. Die Zertifikate sind allerdings nur 3 Monate gültig und müssen daher regelmäßig ausgetauscht werden.
I have now tested renewing the certificate using the following script:
Import-Module ACMESharp Import-Module Webadministration Add-PSSnapin *exchange* $PFXPasswort = Get-Random -Minimum 1000000 -Maximum 9999999 $CurrentCertThumbprint = (Get-ChildItem -Path IIS:SSLBindings | where {$_.port -match "443" -and $_.IPAddress -match "0.0.0.0" } | select Thumbprint).Thumbprint $ExchangeCertificate = Get-ExchangeCertificate -Thumbprint $CurrentCertThumbprint $ExchangeSANs = ($ExchangeCertificate.CertificateDomains).Address $ExchangeSubject = $ExchangeCertificate.Subject.Replace("CN=","") if ($ExchangeSANs -notcontains $ExchangeSubject) {$ExchangeSANs += $ExchangeSubject} $ExchangeSANs $ExchangeSANID = 1 foreach ($ExchangeSAN in $ExchangeSANs) { $CurrentDate = get-date -format ddMMyyyy $ACMEAlias = "Cert" + "$CurrentDate" + "-" + "$ExchangeSANID" $ExchangeSANID++ write-host "New Identifier:" New-ACMEIdentifier -Dns $ExchangeSAN -Alias $ACMEAlias write-host "Complete Challenge:" Complete-ACMEChallenge $ACMEAlias -ChallengeType http-01 -Handler iis -HandlerParameters @{ WebSiteRef = 'Default Web Site' } [Array]$ACMEAliasArray += $ACMEAlias if ($ExchangeSAN -match $ExchangeSubject) {$ExchangeSubjectAlias = $ACMEAlias} } foreach ($ACMEAlias in $ACMEAliasArray) { write-host "Submit Challange:" Submit-ACMEChallenge $ACMEAlias -ChallengeType http-01 } sleep -seconds 30 foreach ($ACMEAlias in $ACMEAliasArray) { write-host "Update:" Update-ACMEIdentifier $ACMEAlias } $SANAlias = "SAN" + "$CurrentDate" New-ACMECertificate $ExchangeSubjectAlias -Generate -AlternativeIdentifierRefs $ACMEAliasArray -Alias $SANAlias Submit-ACMECertificate $SANAlias sleep -seconds 30 Update-ACMECertificate $SANAlias $CertPath = "$env:temp" + "\" + "$SANAlias" + ".pfx" Get-ACMECertificate $SANAlias -ExportPkcs12 $CertPath -CertificatePassword $PFXPasswort $ImportPassword = ConvertTo-SecureString -String $PFXPasswort -Force –AsPlainText Import-ExchangeCertificate -FileName $CertPath -FriendlyName $ExchangeSubject -Password $ImportPassword -PrivateKeyExportable:$true | Enable-ExchangeCertificate -Services "SMTP, IMAP, POP, IIS" –force
Dieses Script diente nur zum Testen, es enthält noch keine Fehlerbehandlung, Doku oder ähnliches. Die gute Nachricht: Der Austausch des vorhandenen Let’s Encrypt Zertifikats hat funktioniert. Hier zwei Screenshots des Scripts:
I ran the PowerShell as administrator and used the test environment with the original certificate to test the renewal process.
So two important building blocks for the fire-and-forget script are already basically working. But it will still take some time before the tool is ready...