Execution Flow
Synopsis
Reads all relevant properties of the existing certificate (subject, endpoint binding, TDE binding), backs it up as .cer + .pvk, creates a new self-signed certificate with CREATE CERTIFICATE, and optionally auto-binds it to AlwaysOn/Broker endpoints or TDE-encrypted databases. Writes a detailed renewal protocol TXT file.
ConfirmImpact = High. Use
-WhatIf to preview without making changes. For AlwaysOn, the new certificate must be distributed to all replica instances separately.Syntax
New-sqmSqlCertificate [-SqlInstance <String>] [-SqlCredential <PSCredential>] -CertificateName <String> # required [-Database <String>] # default: master [-NewCertificateName <String>] [-ValidityYears <Int>] [-BackupPath <String>] [-BackupEncryptionPassword <SecureString>] [-RenameOldCertificate <Bool>] [-BindEndpoint] [-BindTde] [-EnableException] [-WhatIf] [-Confirm]
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| -SqlInstance | String | $env:COMPUTERNAME | SQL Server instance to connect to. |
| -SqlCredential | PSCredential | — | Credential for the SQL connection. |
| -CertificateName | String | Required | Exact name of the certificate to renew (from sys.certificates). |
| -Database | String | master | Database containing the certificate. |
| -NewCertificateName | String | OldName_YEAR | Name for the new certificate. Auto-generated as <OldName>_<ExpiryYear> if omitted. |
| -ValidityYears | Int | 5 | Validity period for the new certificate in years (1–20). |
| -BackupPath | String | Config OutputPath | Directory for .cer and .pvk backup files. |
| -BackupEncryptionPassword | SecureString | — | Password to encrypt the exported private key (.pvk). Required when the certificate has a private key. |
| -RenameOldCertificate | Bool | $true | Log a reminder to rename/drop the old certificate after verification (SQL Server has no RENAME CERTIFICATE). |
| -BindEndpoint | Switch | false | Automatically rebind AlwaysOn / Service Broker endpoints to the new certificate. |
| -BindTde | Switch | false | Automatically rotate the TDE encryption key to the new certificate (online, no downtime). |
| -EnableException | Switch | false | Throw terminating exceptions instead of writing errors. |
Return Value
| Property | Type | Description |
|---|---|---|
| Success | Bool | True on success, False on error. |
| OldCertificateName | String | Name of the original certificate. |
| NewCertificateName | String | Name of the newly created certificate. |
| NewExpiryDate | DateTime | Calculated expiry date of the new certificate. |
| NewThumbprint | String | Hex thumbprint of the new certificate. |
| EndpointBound | Bool | Whether the endpoint was automatically rebound. |
| TdeBound | Bool | Whether TDE was automatically rotated. |
| BackupDirectory | String | Path to the backup directory containing .cer, .pvk, and protocol. |
| ProtocolFile | String | Full path to the renewal protocol TXT file. |
Examples
# Simple renewal — no automatic binding New-sqmSqlCertificate -SqlInstance "SQL01" -CertificateName "AG_CERT" ` -BackupEncryptionPassword (Read-Host -AsSecureString) # Renewal with automatic endpoint binding and 10-year validity New-sqmSqlCertificate -SqlInstance "SQL01" -CertificateName "AG_CERT" ` -ValidityYears 10 -BindEndpoint ` -BackupEncryptionPassword (Read-Host -AsSecureString "Backup password") # Renew TDE certificate with auto rotation New-sqmSqlCertificate -SqlInstance "SQL01" -CertificateName "TDE_PROD" ` -BindTde -BackupEncryptionPassword (Read-Host -AsSecureString "Backup password")
Notes
Requires sysadmin on the instance. dbatools, Invoke-sqmLogging, Get-sqmDefaultOutputPath, and Copy-sqmToCentralPath must be available.
AlwaysOn: After renewal, the new certificate must be distributed to every replica instance and installed there with
Install-sqmCertificate -ForAlwaysOn. SQL Server has no RENAME CERTIFICATE statement — the old cert must be dropped manually after verification.TDE: The new TDE certificate must be backed up including its private key immediately after creation. Without a backup, a database recovery is impossible if the master database is lost.