@@ -16,20 +16,24 @@ function Backup-DbaDbMasterKey {
16
16
17
17
For MFA support, please use Connect-DbaInstance.
18
18
19
+ . PARAMETER Credential
20
+ Pass a credential object for the password
21
+
19
22
. PARAMETER Database
20
23
Backup master key from specific database(s).
21
24
22
25
. PARAMETER ExcludeDatabase
23
26
The database(s) to exclude - this list is auto-populated from the server.
24
27
28
+ . PARAMETER SecurePassword
29
+ The password to encrypt the exported key. This must be a SecureString.
30
+
25
31
. PARAMETER Path
26
32
The directory to export the key. If no path is specified, the default backup directory for the instance will be used.
27
33
28
- . PARAMETER Credential
29
- Pass a credential object for the password
30
-
31
- . PARAMETER SecurePassword
32
- The password to encrypt the exported key. This must be a SecureString.
34
+ . PARAMETER FileBaseName
35
+ Override the default naming convention with a fixed name for the database master key, useful when exporting a single one.
36
+ ".key" will be appended to the filename.
33
37
34
38
. PARAMETER InputObject
35
39
Database object piped in from Get-DbaDatabase
@@ -82,6 +86,7 @@ function Backup-DbaDbMasterKey {
82
86
[Alias (" Password" )]
83
87
[Security.SecureString ]$SecurePassword ,
84
88
[string ]$Path ,
89
+ [string ]$FileBaseName ,
85
90
[parameter (ValueFromPipeline )]
86
91
[Microsoft.SqlServer.Management.Smo.Database []]$InputObject ,
87
92
[switch ]$EnableException
@@ -90,7 +95,6 @@ function Backup-DbaDbMasterKey {
90
95
if ($Credential ) {
91
96
$SecurePassword = $Credential.Password
92
97
}
93
- $time = Get-Date - Format yyyMMddHHmmss
94
98
}
95
99
process {
96
100
foreach ($instance in $SqlInstance ) {
@@ -144,31 +148,36 @@ function Backup-DbaDbMasterKey {
144
148
}
145
149
146
150
$fileinstance = $instance.ToString ().Replace(' \' , ' $' )
147
- $filename = Join-DbaPath - SqlInstance $server - Path $actualPath - ChildPath " $fileinstance -$dbname -masterkey.key"
151
+ $targetBaseName = " $fileinstance -$dbname -masterkey"
152
+ if ($FileBaseName ) {
153
+ $targetBaseName = $FileBaseName
154
+ }
155
+
156
+ $exportFileName = Join-DbaPath - SqlInstance $server - Path $actualPath - ChildPath " $targetBaseName .key"
148
157
149
158
# if the base file name exists, then default to old style of appending a timestamp
150
- if (Test-DbaPath - SqlInstance $server - Path $filename ) {
151
- $filename = Join-DbaPath - SqlInstance $server - Path $actualPath - ChildPath " $fileinstance -$dbname -masterkey-$time .key"
159
+ if (Test-DbaPath - SqlInstance $server - Path $exportFileName ) {
160
+ $time = Get-Date - Format yyyMMddHHmmss
161
+ $exportFileName = Join-DbaPath - SqlInstance $server - Path $actualPath - ChildPath " $targetBaseName -$time .key"
162
+ # Sleep for a second to avoid another export in the same second
163
+ Start-Sleep - Seconds 1
152
164
}
153
165
154
- if ($Pscmdlet.ShouldProcess ($instance , " Backing up master key to $filename " )) {
166
+ if ($Pscmdlet.ShouldProcess ($instance , " Backing up master key to $exportFileName " )) {
155
167
try {
156
- $masterkey.Export ($filename , ($SecurePassword | ConvertFrom-SecurePass ))
168
+ $masterkey.Export ($exportFileName , ($SecurePassword | ConvertFrom-SecurePass ))
157
169
$status = " Success"
158
170
} catch {
159
171
$status = " Failure"
160
172
Write-Message - Level Warning - Message " Backup failure: $ ( $_.Exception.InnerException ) "
161
173
}
162
174
163
- # Sleep for a second to avoid another export in the same second
164
- Start-Sleep - Seconds 1
165
-
166
175
Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name ComputerName - value $server.ComputerName
167
176
Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name InstanceName - value $server.ServiceName
168
177
Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name SqlInstance - value $server.DomainInstanceName
169
178
Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name Database - value $dbName
170
179
Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name DatabaseID - value $db.ID
171
- Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name Filename - value $filename
180
+ Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name Filename - value $exportFileName
172
181
Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name Status - value $status
173
182
174
183
Select-DefaultView - InputObject $masterkey - Property ComputerName, InstanceName, SqlInstance, Database, ' Filename as Path' , Status
0 commit comments