Skip to content

Commit dcf00d7

Browse files
authored
Backup-DbaDbCertificate, new FileBaseName parameter (#9597)
1 parent 00aaf8d commit dcf00d7

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

public/Backup-DbaDbCertificate.ps1

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ function Backup-DbaDbCertificate {
3737
.PARAMETER Suffix
3838
The suffix of the filename of the exported certificate.
3939
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+
4044
.PARAMETER InputObject
4145
Enables piping from Get-DbaDbCertificate
4246
@@ -131,6 +135,7 @@ function Backup-DbaDbCertificate {
131135
[Security.SecureString]$DecryptionPassword,
132136
[System.IO.FileInfo]$Path,
133137
[string]$Suffix,
138+
[string]$FileBaseName,
134139
[parameter(ValueFromPipeline, ParameterSetName = "collection")]
135140
[Microsoft.SqlServer.Management.Smo.Certificate[]]$InputObject,
136141
[switch]$EnableException
@@ -140,7 +145,6 @@ function Backup-DbaDbCertificate {
140145
if (-not $EncryptionPassword -and $DecryptionPassword) {
141146
Stop-Function -Message "If you specify a decryption password, you must also specify an encryption password" -Target $DecryptionPassword
142147
}
143-
$time = Get-Date -Format yyyMMddHHmmss
144148

145149
function export-cert ($cert) {
146150
$certName = $cert.Name
@@ -164,14 +168,21 @@ function Backup-DbaDbCertificate {
164168
}
165169

166170
$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
168176

169177
# if the base file name exists, then default to old style of appending a timestamp
170178
if (Test-DbaPath -SqlInstance $server -Path "$fullCertName.cer") {
171179
if ($Suffix) {
172180
Stop-Function -Message "$fullCertName.cer already exists on $($server.Name)" -Target $actualPath -Continue
173181
} else {
182+
$time = Get-Date -Format yyyyMMddHHmmss
174183
$fullCertName = "$fullCertName-$time"
184+
# Sleep for a second to avoid another export in the same second
185+
Start-Sleep -Seconds 1
175186
}
176187
}
177188

@@ -180,7 +191,6 @@ function Backup-DbaDbCertificate {
180191
if ($Pscmdlet.ShouldProcess($instance, "Exporting certificate $certName from $db on $instance to $actualPath")) {
181192
Write-Message -Level Verbose -Message "Exporting Certificate: $certName to $fullCertName"
182193
try {
183-
184194
$exportPathCert = "$fullCertName.cer"
185195

186196
# because the password shouldn't go to memory...
@@ -208,9 +218,6 @@ function Backup-DbaDbCertificate {
208218
$cert.export($exportPathCert)
209219
}
210220

211-
# Sleep for a second to avoid another export in the same second
212-
Start-Sleep -Seconds 1
213-
214221
[PSCustomObject]@{
215222
ComputerName = $server.ComputerName
216223
InstanceName = $server.ServiceName

tests/Backup-DbaDbCertificate.Tests.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Describe "Backup-DbaDbCertificate" -Tag "UnitTests" {
1919
"DecryptionPassword",
2020
"Path",
2121
"Suffix",
22+
"FileBaseName",
2223
"InputObject",
2324
"EnableException",
2425
"Confirm",
@@ -63,6 +64,15 @@ Describe "Backup-DbaDbCertificate" -Tag "IntegrationTests" {
6364
$results.DatabaseID | Should -Be (Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $db1Name).ID
6465
}
6566

67+
It "backs up the db cert with a filename (see #9485)" {
68+
$results = Backup-DbaDbCertificate -SqlInstance $TestConfig.instance1 -Certificate $cert.Name -Database $db1Name -EncryptionPassword $pw -DecryptionPassword $pw -FileBaseName "dbatoolscli_cert1_$random"
69+
$results.Certificate | Should -Be $cert.Name
70+
$results.Status | Should -Match "Success"
71+
$results.DatabaseID | Should -Be (Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $db1Name).ID
72+
[IO.Path]::GetFileNameWithoutExtension($results.Path) | Should -Be "dbatoolscli_cert1_$random"
73+
$null = Get-ChildItem -Path $results.Path -ErrorAction Ignore | Remove-Item -Confirm:$false -ErrorAction Ignore
74+
}
75+
6676
It "warns the caller if the cert cannot be found" {
6777
$invalidDBCertName = "dbatoolscli_invalidCertName"
6878
$invalidDBCertName2 = "dbatoolscli_invalidCertName2"
@@ -82,5 +92,6 @@ Describe "Backup-DbaDbCertificate" -Tag "IntegrationTests" {
8292
$results = Backup-DbaDbCertificate -SqlInstance $TestConfig.instance1 -EncryptionPassword $pw
8393
$null = Get-ChildItem -Path $results.Path -ErrorAction Ignore | Remove-Item -Confirm:$false -ErrorAction Ignore
8494
}
95+
8596
}
8697
}

0 commit comments

Comments
 (0)