@@ -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
0 commit comments