@@ -37,6 +37,10 @@ function Backup-DbaDbCertificate {
37
37
. PARAMETER Suffix
38
38
The suffix of the filename of the exported certificate.
39
39
40
+ . PARAMETER FileBaseName
41
+ Override the default naming convention with a fixed name for the certificate and private key file name, useful when exporting a single certificate.
42
+ ".cer" will be appended to the certificate file name and ".pvk" will be appended to the private key file name.
43
+
40
44
. PARAMETER InputObject
41
45
Enables piping from Get-DbaDbCertificate
42
46
@@ -131,6 +135,7 @@ function Backup-DbaDbCertificate {
131
135
[Security.SecureString ]$DecryptionPassword ,
132
136
[System.IO.FileInfo ]$Path ,
133
137
[string ]$Suffix ,
138
+ [string ]$FileBaseName ,
134
139
[parameter (ValueFromPipeline , ParameterSetName = " collection" )]
135
140
[Microsoft.SqlServer.Management.Smo.Certificate []]$InputObject ,
136
141
[switch ]$EnableException
@@ -140,7 +145,6 @@ function Backup-DbaDbCertificate {
140
145
if (-not $EncryptionPassword -and $DecryptionPassword ) {
141
146
Stop-Function - Message " If you specify a decryption password, you must also specify an encryption password" - Target $DecryptionPassword
142
147
}
143
- $time = Get-Date - Format yyyMMddHHmmss
144
148
145
149
function export-cert ($cert ) {
146
150
$certName = $cert.Name
@@ -164,14 +168,21 @@ function Backup-DbaDbCertificate {
164
168
}
165
169
166
170
$fileinstance = $instance.ToString ().Replace(' \' , ' $' )
167
- $fullCertName = Join-DbaPath - SqlInstance $server - Path $actualPath - ChildPath " $fileinstance -$dbname -$certName$Suffix "
171
+ $targetBaseName = " $fileinstance -$dbname -$certName$Suffix "
172
+ if ($FileBaseName ) {
173
+ $targetBaseName = $FileBaseName
174
+ }
175
+ $fullCertName = Join-DbaPath - SqlInstance $server - Path $actualPath - ChildPath $targetBaseName
168
176
169
177
# if the base file name exists, then default to old style of appending a timestamp
170
178
if (Test-DbaPath - SqlInstance $server - Path " $fullCertName .cer" ) {
171
179
if ($Suffix ) {
172
180
Stop-Function - Message " $fullCertName .cer already exists on $ ( $server.Name ) " - Target $actualPath - Continue
173
181
} else {
182
+ $time = Get-Date - Format yyyyMMddHHmmss
174
183
$fullCertName = " $fullCertName -$time "
184
+ # Sleep for a second to avoid another export in the same second
185
+ Start-Sleep - Seconds 1
175
186
}
176
187
}
177
188
@@ -180,7 +191,6 @@ function Backup-DbaDbCertificate {
180
191
if ($Pscmdlet.ShouldProcess ($instance , " Exporting certificate $certName from $db on $instance to $actualPath " )) {
181
192
Write-Message - Level Verbose - Message " Exporting Certificate: $certName to $fullCertName "
182
193
try {
183
-
184
194
$exportPathCert = " $fullCertName .cer"
185
195
186
196
# because the password shouldn't go to memory...
@@ -208,9 +218,6 @@ function Backup-DbaDbCertificate {
208
218
$cert.export ($exportPathCert )
209
219
}
210
220
211
- # Sleep for a second to avoid another export in the same second
212
- Start-Sleep - Seconds 1
213
-
214
221
[PSCustomObject ]@ {
215
222
ComputerName = $server.ComputerName
216
223
InstanceName = $server.ServiceName
0 commit comments