Free ACME scripts / Windows
Exchange
Install / enable an ACME certificate on Microsoft Exchange from simple-acme, automatically and on any Exchange server (Edge, Mailbox, CAS, MultiRole; 2013, 2016, 2019, Subscription Edition; domain-joined or workgroup).
- Version
- 3.2
- Language
- PowerShell
- SHA-256
- c569a18f208c9e9014ad2824e1e3175a9892931d07ecdfa4a06f244b3bbf6a44
- Licence
- Apache-2.0, copyright FairSSL A/S
Download it on the server, check the SHA-256 and keep it as C:\simple-acme\Scripts\ImportExchange.v3.ps1, so a newer version later replaces it without a change to your renewal:
# Windows PowerShell 5.1 needs TLS 1.2 switched on for this session
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -UseBasicParsing -Uri 'https://sslbrain.com/scripts/files/exchange-3.2.ps1' -OutFile 'C:\simple-acme\Scripts\ImportExchange.v3.ps1'
(Get-FileHash 'C:\simple-acme\Scripts\ImportExchange.v3.ps1' -Algorithm SHA256).Hash -eq 'C569A18F208C9E9014AD2824E1E3175A9892931D07ECDFA4A06F244B3BBF6A44'
Unblock-File 'C:\simple-acme\Scripts\ImportExchange.v3.ps1' What it does
FairSSL replacement for the stock simple-acme ImportExchange.v2.ps1 and ImportExchangeHybrid.ps1. simple-acme runs this after it has obtained and stored the certificate; the script's job is to enable that certificate on Microsoft Exchange.
Why a replacement: simple-acme runs the install script in a NEW powershell.exe (not the Exchange Management Shell you launched wacs.exe from), so the script has to load the Exchange cmdlets itself. The stock v2 script does that with WinRM remoting (a remote PowerShell session to http://<fqdn>/PowerShell/), which fails on an Edge Transport server because Edge is in a workgroup and Kerberos to itself is unavailable ("ServerNotTrusted").
What it does, in order: 1. Loads the Exchange management cmdlets LOCALLY, never the WinRM call that broke v2 on Edge: a. reuse them if already present (e.g. launched from the Exchange Management Shell); b. Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn (2013+), then ...E2010 (2010) - in-process, and what makes the script work on a non-domain-joined / workgroup Edge server; c. last resort on a domain-joined server where the snap-in is not registered: dot-source <ExchangeInstallPath>\bin\RemoteExchange.ps1 and Connect-ExchangeServer -Auto. This uses implicit remoting to the local server, so it does NOT rescue a workgroup Edge box - step b does. 2. Finds the certificate by thumbprint in LocalMachine\My or LocalMachine\WebHosting (some simple-acme configurations use WebHosting). If it is missing and a CacheFile was supplied, it imports the PFX as a fallback. 3. Detects the server role (Edge, Mailbox, Client Access, MultiRole) and the Exchange version (2013, 2016, 2019, Subscription Edition). 4. Enables the certificate only for the services valid on that server: the requested list intersected with what the role/version supports, so "IIS,SMTP,IMAP,POP" collapses to "SMTP" on Edge instead of failing. Federation and UM are never enabled automatically. 5. -Hybrid (optional): also points the Office 365 hybrid surfaces at the new certificate - the "Outbound to Office 365" send connector, the local "Default Frontend" receive connector, and Set-HybridConfiguration. 6. Cleanup (only when LeaveOldExchangeCerts is 0): removes older Exchange certificates whose common name matches the new one, while protecting the new certificate, self-signed certificates (Edge subscription / internal "Microsoft Exchange"), the Auth certificate and the Federation certificate.
Enabling a certificate for SMTP with -Force also makes it Exchange's internal transport certificate (on Edge, the one the EdgeSync subscription uses). The script reads that certificate first and sets it back right after the enable, also when the enable fails halfway. It is not set back when it is expired, gone, or an earlier certificate for this same name: earlier versions of this script moved the role to every renewed certificate, and putting last renewal's certificate back would leave internal transport on a certificate that expires within weeks. Then the new certificate keeps the role, and the log says so. The Auth and Federation certificates are never touched.
The exit code is 0 only when the certificate is enabled, the internal transport certificate is kept and, with -Hybrid, every hybrid step worked. Anything else exits 1 with the reason, so simple-acme and FairSSL report it. A step that fails does not stop the steps after it: simple-acme removes the previous certificate after the run, also after a failed one, so every service that can be put on the new certificate is, and the run fails at the end. A Client Access server (2010, 2013) has no internal transport certificate, so there is nothing to keep. Each Exchange server enables the certificate independently, so in a multi-server environment just run the command on each server.
Positional parameters are kept compatible with simple-acme's v2 script, so the only guide change is the script name.
Requirements
- Windows Server with Windows PowerShell 5.1, run as Administrator
- simple-acme, which runs the script after each issuance and renewal
Parameters
| -NewCertThumbprint | Thumbprint of the freshly issued certificate ({CertThumbprint}). simple-acme has already placed it in the LocalMachine certificate store (My or WebHosting). |
|---|---|
| -ExchangeServices | Comma separated desired services (no spaces), e.g. "IIS,SMTP,IMAP,POP". The literal "auto" (or an empty value) means "IIS,SMTP,IMAP,POP". The final set is the intersection of the desired list and the services supported by this server's role/version. Federation and UM are never added automatically; request them explicitly if you really need them. |
| -LeaveOldExchangeCerts | 1 (default) leaves existing certificates alone. 0 removes older Exchange certificates whose common name matches the new certificate, while protecting the new certificate, self-signed certificates (Edge subscription / internal "Microsoft Exchange"), the Auth Certificate and the Federation certificate. |
| -CacheFile | Optional PFX path ({CacheFile}). Only used as a fallback to import the certificate if it is not already present in the store. |
| -PfxPassword | Password for CacheFile ({CachePassword}). |
| -FriendlyName | Friendly name ({CertFriendlyName}). Accepted for compatibility. |
| -Hybrid | Opt-in. Additionally points the Office 365 hybrid surfaces at the new certificate: the "Outbound to Office 365" send connector, the local "Default Frontend" receive connector, and Set-HybridConfiguration -TlsCertificateName. A connector is changed only when its value differs, and MSExchangeTransport is restarted once when one changed. Skipped with a note if no HybridConfiguration exists. A hybrid step that fails, or no send connector with the new TLS name, makes the run exit 1. |
| -DebugOn | Verbose tracing. |
Get a certificate and run the script
Run simple-acme's
wacs.exe from its folder (here C:\simple-acme) as Administrator. It orders the
certificate, saves it in the Windows certificate store and as PEM files, runs the script, and creates the scheduled task that
renews the certificate and runs the script again. Replace www.example.com, the e-mail address and
the script's parameters (see Parameters; simple-acme fills in tokens such as
{CertThumbprint}, {StorePath} and {RenewalId}).
--validation selfhosting answers the CA's HTTP challenge on port 80; on a web server use
--validation filesystem --webroot <site folder> instead.
Let's Encrypt
Directory: https://acme-v02.api.letsencrypt.org/directory. No account to create: the client registers one with your e-mail address. Let's Encrypt documentation
.\wacs.exe `
--baseuri "https://acme-v02.api.letsencrypt.org/directory" `
--accepttos --emailaddress "you@example.com" `
--source manual --host "www.example.com" `
--validation selfhosting `
--store certificatestore,pemfiles --pemfilespath "C:\simple-acme\Certificates" `
--installation script `
--script ".\Scripts\ImportExchange.v3.ps1" `
--scriptparameters "<parameters>" Google Trust Services
Directory: https://dv.acme-v02.api.pki.goog/directory. Create an EAB key in a Google Cloud project (gcloud publicca external-account-keys create). Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. Google Public CA tutorial
.\wacs.exe `
--baseuri "https://dv.acme-v02.api.pki.goog/directory" `
--eab-key-identifier "EAB_KID" --eab-key "EAB_HMAC" `
--accepttos --emailaddress "you@example.com" `
--source manual --host "www.example.com" `
--validation selfhosting `
--store certificatestore,pemfiles --pemfilespath "C:\simple-acme\Certificates" `
--installation script `
--script ".\Scripts\ImportExchange.v3.ps1" `
--scriptparameters "<parameters>" ZeroSSL
Directory: https://acme.zerossl.com/v2/DV90. Copy the EAB credentials from the Developer section of your ZeroSSL account. Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. ZeroSSL ACME documentation
.\wacs.exe `
--baseuri "https://acme.zerossl.com/v2/DV90" `
--eab-key-identifier "EAB_KID" --eab-key "EAB_HMAC" `
--accepttos --emailaddress "you@example.com" `
--source manual --host "www.example.com" `
--validation selfhosting `
--store certificatestore,pemfiles --pemfilespath "C:\simple-acme\Certificates" `
--installation script `
--script ".\Scripts\ImportExchange.v3.ps1" `
--scriptparameters "<parameters>" acme.sslbrain.com
Directory: https://acme.sslbrain.com/acme/directory. Create an EAB key in sslbrain Cloud (Basic or higher) under ACME clients, Connect a client. sslbrain Cloud validates the domain through a CNAME redirect, a DNS API or pre-validation, not your ACME client. Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. sslbrain plans
.\wacs.exe `
--baseuri "https://acme.sslbrain.com/acme/directory" `
--eab-key-identifier "EAB_KID" --eab-key "EAB_HMAC" `
--accepttos --emailaddress "you@example.com" `
--source manual --host "www.example.com" `
--validation none `
--store certificatestore,pemfiles --pemfilespath "C:\simple-acme\Certificates" `
--installation script `
--script ".\Scripts\ImportExchange.v3.ps1" `
--scriptparameters "<parameters>" Licence and credits
Copyright FairSSL A/S, licensed under the Apache License 2.0: use it, change it and share it, also commercially.
Runs in simple-acme (formerly win-acme), Apache-2.0.
One script per server, or sslbrain does it for all your servers without scripts.
Feedback and improvements are welcome: info@fairssl.dk