Skip to content

Commit 00aaf8d

Browse files
authored
Backup-DbaServiceMasterKey, add FileBaseName parameter (#9598)
1 parent 978b97c commit 00aaf8d

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

public/Backup-DbaServiceMasterKey.ps1

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ function Backup-DbaServiceMasterKey {
1616
1717
For MFA support, please use Connect-DbaInstance.
1818
19+
.PARAMETER KeyCredential
20+
Pass a credential object for the password
21+
1922
.PARAMETER Path
2023
The directory to export the key. If no path is specified, the default backup directory for the instance will be used.
2124
22-
.PARAMETER KeyCredential
23-
Pass a credential object for the password
25+
.PARAMETER FileBaseName
26+
Override the default naming convention with a fixed name for the service master key, useful when exporting a single one.
27+
".key" will be appended to the filename.
2428
2529
.PARAMETER SecurePassword
2630
The password to encrypt the exported key. This must be a SecureString.
@@ -72,13 +76,13 @@ function Backup-DbaServiceMasterKey {
7276
[Alias("Password")]
7377
[Security.SecureString]$SecurePassword,
7478
[string]$Path,
79+
[string]$FileBaseName,
7580
[switch]$EnableException
7681
)
7782
begin {
7883
if ($KeyCredential) {
7984
$SecurePassword = $KeyCredential.Password
8085
}
81-
$time = Get-Date -Format yyyMMddHHmmss
8286
}
8387
process {
8488
foreach ($instance in $SqlInstance) {
@@ -116,16 +120,24 @@ function Backup-DbaServiceMasterKey {
116120
$Path = $Path.TrimEnd("\")
117121
$Path = $Path.TrimEnd("/")
118122
$fileinstance = $instance.ToString().Replace('\', '$')
119-
$filename = Join-DbaPath -SqlInstance $server -Path $Path -ChildPath "$fileinstance-servicemasterkey.key"
123+
$targetBaseName = "$fileinstance-servicemasterkey"
124+
if ($FileBaseName) {
125+
$targetBaseName = $FileBaseName
126+
}
127+
128+
$exportFileName = Join-DbaPath -SqlInstance $server -Path $Path -ChildPath "$targetBaseName.key"
120129

121130
# if the base file name exists, then default to old style of appending a timestamp
122-
if (Test-DbaPath -SqlInstance $server -Path $filename) {
123-
$filename = Join-DbaPath -SqlInstance $server -Path $Path -ChildPath "$fileinstance-servicemasterkey-$time.key"
131+
if (Test-DbaPath -SqlInstance $server -Path $exportFileName) {
132+
$time = Get-Date -Format yyyyMMddHHmmss
133+
$exportFileName = Join-DbaPath -SqlInstance $server -Path $Path -ChildPath "$targetBaseName-$time.key"
134+
# Sleep for a second to avoid another export in the same second
135+
Start-Sleep -Seconds 1
124136
}
125137

126-
if ($Pscmdlet.ShouldProcess($instance, "Backing up service master key to $filename")) {
138+
if ($Pscmdlet.ShouldProcess($instance, "Backing up service master key to $exportFileName")) {
127139
try {
128-
$masterkey.Export($filename, ($SecurePassword | ConvertFrom-SecurePass))
140+
$masterkey.Export($exportFileName, ($SecurePassword | ConvertFrom-SecurePass))
129141
$status = "Success"
130142
} catch {
131143
$status = "Failure"
@@ -135,7 +147,7 @@ function Backup-DbaServiceMasterKey {
135147
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name ComputerName -value $server.ComputerName
136148
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
137149
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
138-
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name Filename -value $filename
150+
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name Filename -value $exportFileName
139151
Add-Member -Force -InputObject $masterkey -MemberType NoteProperty -Name Status -value $status
140152

141153
Select-DefaultView -InputObject $masterkey -Property ComputerName, InstanceName, SqlInstance, 'Filename as Path', Status

tests/Backup-DbaServiceMasterKey.Tests.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Describe "Backup-DbaServiceMasterKey" -Tag "UnitTests" {
1515
"KeyCredential",
1616
"SecurePassword",
1717
"Path",
18+
"FileBaseName",
1819
"EnableException",
1920
"Confirm",
2021
"WhatIf"
@@ -36,14 +37,21 @@ Describe "Backup-DbaServiceMasterKey" -Tag "IntegrationTests" {
3637
Context "Can backup a service master key" {
3738
BeforeAll {
3839
$securePassword = ConvertTo-SecureString -String "GoodPass1234!" -AsPlainText -Force
39-
$results = Backup-DbaServiceMasterKey -SqlInstance $TestConfig.instance1 -SecurePassword $securePassword -Confirm:$false
4040
}
4141

4242
AfterAll {
4343
$null = Remove-Item -Path $results.Path -ErrorAction SilentlyContinue -Confirm:$false
4444
}
4545

4646
It "backs up the SMK" {
47+
$results = Backup-DbaServiceMasterKey -SqlInstance $TestConfig.instance1 -SecurePassword $securePassword -Confirm:$false
48+
$results.Status | Should -Be "Success"
49+
}
50+
51+
It "backs up the SMK with a specific filename (see #9483)" {
52+
$random = Get-Random
53+
$results = Backup-DbaServiceMasterKey -SqlInstance $TestConfig.instance1 -SecurePassword $securePassword -FileBaseName "smk($random)" -Confirm:$false
54+
[IO.Path]::GetFileNameWithoutExtension($results.Path) | Should -Be "smk($random)"
4755
$results.Status | Should -Be "Success"
4856
}
4957
}

0 commit comments

Comments
 (0)