@@ -16,11 +16,15 @@ function Backup-DbaServiceMasterKey {
16
16
17
17
For MFA support, please use Connect-DbaInstance.
18
18
19
+ . PARAMETER KeyCredential
20
+ Pass a credential object for the password
21
+
19
22
. PARAMETER Path
20
23
The directory to export the key. If no path is specified, the default backup directory for the instance will be used.
21
24
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.
24
28
25
29
. PARAMETER SecurePassword
26
30
The password to encrypt the exported key. This must be a SecureString.
@@ -72,13 +76,13 @@ function Backup-DbaServiceMasterKey {
72
76
[Alias (" Password" )]
73
77
[Security.SecureString ]$SecurePassword ,
74
78
[string ]$Path ,
79
+ [string ]$FileBaseName ,
75
80
[switch ]$EnableException
76
81
)
77
82
begin {
78
83
if ($KeyCredential ) {
79
84
$SecurePassword = $KeyCredential.Password
80
85
}
81
- $time = Get-Date - Format yyyMMddHHmmss
82
86
}
83
87
process {
84
88
foreach ($instance in $SqlInstance ) {
@@ -116,16 +120,24 @@ function Backup-DbaServiceMasterKey {
116
120
$Path = $Path.TrimEnd (" \" )
117
121
$Path = $Path.TrimEnd (" /" )
118
122
$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"
120
129
121
130
# 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
124
136
}
125
137
126
- if ($Pscmdlet.ShouldProcess ($instance , " Backing up service master key to $filename " )) {
138
+ if ($Pscmdlet.ShouldProcess ($instance , " Backing up service master key to $exportFileName " )) {
127
139
try {
128
- $masterkey.Export ($filename , ($SecurePassword | ConvertFrom-SecurePass ))
140
+ $masterkey.Export ($exportFileName , ($SecurePassword | ConvertFrom-SecurePass ))
129
141
$status = " Success"
130
142
} catch {
131
143
$status = " Failure"
@@ -135,7 +147,7 @@ function Backup-DbaServiceMasterKey {
135
147
Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name ComputerName - value $server.ComputerName
136
148
Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name InstanceName - value $server.ServiceName
137
149
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
139
151
Add-Member - Force - InputObject $masterkey - MemberType NoteProperty - Name Status - value $status
140
152
141
153
Select-DefaultView - InputObject $masterkey - Property ComputerName, InstanceName, SqlInstance, ' Filename as Path' , Status
0 commit comments