Skip to content

Commit c0d43ba

Browse files
committed
Add Force parameter to New-SqlDscDataFile and New-SqlDscFileGroup for confirmation bypass; update tests to validate new functionality
1 parent ba30af5 commit c0d43ba

File tree

7 files changed

+209
-26
lines changed

7 files changed

+209
-26
lines changed

source/Public/New-SqlDscDataFile.ps1

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
.ss extension). For regular databases, this should be the data file
2121
path (typically with .mdf or .ndf extension).
2222
23+
.PARAMETER Force
24+
Specifies that the DataFile object should be created without prompting for
25+
confirmation. By default, the command prompts for confirmation when the FileGroup
26+
parameter is provided.
27+
2328
.EXAMPLE
2429
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
2530
$database = $serverObject.Databases['MyDatabase']
@@ -47,10 +52,10 @@
4752
function New-SqlDscDataFile
4853
{
4954
[OutputType([Microsoft.SqlServer.Management.Smo.DataFile])]
50-
[CmdletBinding(DefaultParameterSetName = 'WithFileGroup')]
55+
[CmdletBinding(DefaultParameterSetName = 'Standalone', SupportsShouldProcess = $true, ConfirmImpact = 'High')]
5156
param
5257
(
53-
[Parameter(ParameterSetName = 'WithFileGroup', ValueFromPipeline = $true)]
58+
[Parameter(Mandatory = $true, ParameterSetName = 'WithFileGroup', ValueFromPipeline = $true)]
5459
[Microsoft.SqlServer.Management.Smo.FileGroup]
5560
$FileGroup,
5661

@@ -64,14 +69,32 @@ function New-SqlDscDataFile
6469
[Parameter(Mandatory = $true, ParameterSetName = 'Standalone')]
6570
[ValidateNotNullOrEmpty()]
6671
[System.String]
67-
$FileName
72+
$FileName,
73+
74+
[Parameter(ParameterSetName = 'WithFileGroup')]
75+
[System.Management.Automation.SwitchParameter]
76+
$Force
6877
)
6978

7079
process
7180
{
81+
if ($Force.IsPresent -and -not $Confirm)
82+
{
83+
$ConfirmPreference = 'None'
84+
}
85+
86+
$dataFileObject = $null
87+
7288
if ($PSCmdlet.ParameterSetName -eq 'WithFileGroup')
7389
{
74-
$dataFileObject = [Microsoft.SqlServer.Management.Smo.DataFile]::new($FileGroup, $Name, $FileName)
90+
$descriptionMessage = $script:localizedData.DataFile_Create_ShouldProcessDescription -f $Name, $FileGroup.Name
91+
$confirmationMessage = $script:localizedData.DataFile_Create_ShouldProcessConfirmation -f $Name
92+
$captionMessage = $script:localizedData.DataFile_Create_ShouldProcessCaption
93+
94+
if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage))
95+
{
96+
$dataFileObject = [Microsoft.SqlServer.Management.Smo.DataFile]::new($FileGroup, $Name, $FileName)
97+
}
7598
}
7699
else
77100
{

source/Public/New-SqlDscFileGroup.ps1

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
.PARAMETER Name
1717
Specifies the name of the FileGroup to create.
1818
19+
.PARAMETER Force
20+
Specifies that the FileGroup object should be created without prompting for
21+
confirmation. By default, the command prompts for confirmation when the Database
22+
parameter is provided.
23+
1924
.EXAMPLE
2025
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
2126
$database = $serverObject.Databases['MyDatabase']
@@ -43,7 +48,7 @@
4348
function New-SqlDscFileGroup
4449
{
4550
[OutputType([Microsoft.SqlServer.Management.Smo.FileGroup])]
46-
[CmdletBinding(DefaultParameterSetName = 'Standalone')]
51+
[CmdletBinding(DefaultParameterSetName = 'Standalone', SupportsShouldProcess = $true, ConfirmImpact = 'High')]
4752
param
4853
(
4954
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'WithDatabase')]
@@ -54,14 +59,34 @@ function New-SqlDscFileGroup
5459
[Parameter(Mandatory = $true, ParameterSetName = 'Standalone')]
5560
[ValidateNotNullOrEmpty()]
5661
[System.String]
57-
$Name
62+
$Name,
63+
64+
[Parameter(ParameterSetName = 'WithDatabase')]
65+
[System.Management.Automation.SwitchParameter]
66+
$Force
5867
)
5968

6069
process
6170
{
71+
if ($Force.IsPresent -and -not $Confirm)
72+
{
73+
$ConfirmPreference = 'None'
74+
}
75+
76+
$fileGroupObject = $null
77+
6278
if ($PSCmdlet.ParameterSetName -eq 'WithDatabase')
6379
{
64-
$fileGroupObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.FileGroup' -ArgumentList $Database, $Name
80+
$serverObject = $Database.Parent
81+
82+
$descriptionMessage = $script:localizedData.FileGroup_Create_ShouldProcessDescription -f $Name, $Database.Name, $serverObject.InstanceName
83+
$confirmationMessage = $script:localizedData.FileGroup_Create_ShouldProcessConfirmation -f $Name
84+
$captionMessage = $script:localizedData.FileGroup_Create_ShouldProcessCaption
85+
86+
if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage))
87+
{
88+
$fileGroupObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.FileGroup' -ArgumentList $Database, $Name
89+
}
6590
}
6691
else
6792
{

source/en-US/SqlServerDsc.strings.psd1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ ConvertFrom-StringData @'
369369
DatabaseSnapshot_Create = Creating database snapshot '{0}' from source database '{1}' on instance '{2}'. (NSDS0002)
370370
DatabaseSnapshot_EditionNotSupported = Database snapshots are not supported on SQL Server instance '{0}' with edition '{1}'. Snapshots are only supported in Enterprise, Developer, and Evaluation editions. (NSDS0001)
371371
372+
## New-SqlDscFileGroup
373+
FileGroup_Create_ShouldProcessDescription = Creating the filegroup '{0}' for database '{1}' on instance '{2}'. (NSDFG0001)
374+
FileGroup_Create_ShouldProcessConfirmation = Are you sure you want to create the filegroup '{0}'? (NSDFG0002)
375+
# This string shall not end with full stop (.) since it is used as a title of ShouldProcess messages.
376+
FileGroup_Create_ShouldProcessCaption = Create filegroup for database
377+
378+
## New-SqlDscDataFile
379+
DataFile_Create_ShouldProcessDescription = Creating the data file '{0}' for filegroup '{1}'. (NSDDF0001)
380+
DataFile_Create_ShouldProcessConfirmation = Are you sure you want to create the data file '{0}'? (NSDDF0002)
381+
# This string shall not end with full stop (.) since it is used as a title of ShouldProcess messages.
382+
DataFile_Create_ShouldProcessCaption = Create data file for filegroup
383+
372384
## Set-SqlDscDatabaseProperty
373385
Database_Set = Setting properties of database '{0}' on instance '{1}'. (SSDDP0001)
374386
Database_Updating = Updating database '{0}'. (SSDDP0002)

tests/Integration/Commands/New-SqlDscDataFile.Integration.Tests.ps1

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ BeforeAll {
3535
Describe 'New-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
3636
Context 'When creating a standalone DataFile with real SMO types' {
3737
It 'Should create a standalone DataFile successfully' {
38-
$result = New-SqlDscDataFile -Name 'TestDataFile'
38+
$result = New-SqlDscDataFile -Name 'TestDataFile' -FileName 'C:\Data\TestDataFile.mdf'
3939

4040
$result | Should -Not -BeNullOrEmpty
4141
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.DataFile'
4242
$result.Name | Should -Be 'TestDataFile'
43+
$result.FileName | Should -Be 'C:\Data\TestDataFile.mdf'
4344
$result.Parent | Should -BeNullOrEmpty
4445
}
4546

@@ -62,7 +63,7 @@ Describe 'New-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019
6263
}
6364

6465
It 'Should create a DataFile with FileGroup successfully' {
65-
$result = New-SqlDscDataFile -FileGroup $script:mockFileGroup -Name 'TestDataFile'
66+
$result = New-SqlDscDataFile -FileGroup $script:mockFileGroup -Name 'TestDataFile' -FileName 'C:\Data\TestDataFile.ndf' -Confirm:$false
6667

6768
$result | Should -Not -BeNullOrEmpty
6869
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.DataFile'
@@ -71,7 +72,7 @@ Describe 'New-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019
7172
}
7273

7374
It 'Should create a DataFile with FileGroup and FileName' {
74-
$result = New-SqlDscDataFile -FileGroup $script:mockFileGroup -Name 'TestDataFile2' -FileName 'C:\Data\TestDataFile2.ndf'
75+
$result = New-SqlDscDataFile -FileGroup $script:mockFileGroup -Name 'TestDataFile2' -FileName 'C:\Data\TestDataFile2.ndf' -Confirm:$false
7576

7677
$result | Should -Not -BeNullOrEmpty
7778
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.DataFile'
@@ -81,32 +82,47 @@ Describe 'New-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019
8182
}
8283

8384
It 'Should accept FileGroup parameter from pipeline' {
84-
$result = $script:mockFileGroup | New-SqlDscDataFile -Name 'PipelineDataFile'
85+
$result = $script:mockFileGroup | New-SqlDscDataFile -Name 'PipelineDataFile' -FileName 'C:\Data\PipelineDataFile.ndf' -Confirm:$false
8586

8687
$result | Should -Not -BeNullOrEmpty
8788
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.DataFile'
8889
$result.Name | Should -Be 'PipelineDataFile'
8990
$result.Parent | Should -Be $script:mockFileGroup
9091
}
92+
93+
It 'Should support Force parameter to bypass confirmation' {
94+
$result = New-SqlDscDataFile -FileGroup $script:mockFileGroup -Name 'ForcedDataFile' -FileName 'C:\Data\ForcedDataFile.ndf' -Force
95+
96+
$result | Should -Not -BeNullOrEmpty
97+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.DataFile'
98+
$result.Name | Should -Be 'ForcedDataFile'
99+
$result.Parent | Should -Be $script:mockFileGroup
100+
}
101+
102+
It 'Should return null when user declines confirmation' {
103+
$result = New-SqlDscDataFile -FileGroup $script:mockFileGroup -Name 'DeclinedDataFile' -FileName 'C:\Data\DeclinedDataFile.ndf' -Confirm:$false -WhatIf
104+
105+
$result | Should -BeNullOrEmpty
106+
}
91107
}
92108

93109
Context 'When verifying DataFile properties' {
94110
It 'Should allow setting Size property' {
95-
$result = New-SqlDscDataFile -Name 'TestDataFile'
111+
$result = New-SqlDscDataFile -Name 'TestDataFile' -FileName 'C:\Data\TestDataFile.ndf'
96112
$result.Size = 1024.0
97113

98114
$result.Size | Should -Be 1024.0
99115
}
100116

101117
It 'Should allow setting Growth property' {
102-
$result = New-SqlDscDataFile -Name 'TestDataFile'
118+
$result = New-SqlDscDataFile -Name 'TestDataFile' -FileName 'C:\Data\TestDataFile.ndf'
103119
$result.Growth = 64.0
104120

105121
$result.Growth | Should -Be 64.0
106122
}
107123

108124
It 'Should allow setting GrowthType property' {
109-
$result = New-SqlDscDataFile -Name 'TestDataFile'
125+
$result = New-SqlDscDataFile -Name 'TestDataFile' -FileName 'C:\Data\TestDataFile.ndf'
110126
$result.GrowthType = [Microsoft.SqlServer.Management.Smo.FileGrowthType]::Percent
111127

112128
$result.GrowthType | Should -Be 'Percent'

tests/Integration/Commands/New-SqlDscFileGroup.Integration.Tests.ps1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Describe 'New-SqlDscFileGroup' -Tag @('Integration_SQL2017', 'Integration_SQL201
6161
}
6262

6363
It 'Should create a FileGroup with Database successfully' {
64-
$result = New-SqlDscFileGroup -Database $script:mockDatabase -Name 'TestFileGroup'
64+
$result = New-SqlDscFileGroup -Database $script:mockDatabase -Name 'TestFileGroup' -Confirm:$false
6565

6666
$result | Should -Not -BeNullOrEmpty
6767
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.FileGroup'
@@ -70,13 +70,28 @@ Describe 'New-SqlDscFileGroup' -Tag @('Integration_SQL2017', 'Integration_SQL201
7070
}
7171

7272
It 'Should accept Database parameter from pipeline' {
73-
$result = $script:mockDatabase | New-SqlDscFileGroup -Name 'PipelineFileGroup'
73+
$result = $script:mockDatabase | New-SqlDscFileGroup -Name 'PipelineFileGroup' -Confirm:$false
7474

7575
$result | Should -Not -BeNullOrEmpty
7676
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.FileGroup'
7777
$result.Name | Should -Be 'PipelineFileGroup'
7878
$result.Parent | Should -Be $script:mockDatabase
7979
}
80+
81+
It 'Should support Force parameter to bypass confirmation' {
82+
$result = New-SqlDscFileGroup -Database $script:mockDatabase -Name 'ForcedFileGroup' -Force
83+
84+
$result | Should -Not -BeNullOrEmpty
85+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.FileGroup'
86+
$result.Name | Should -Be 'ForcedFileGroup'
87+
$result.Parent | Should -Be $script:mockDatabase
88+
}
89+
90+
It 'Should return null when user declines confirmation' {
91+
$result = New-SqlDscFileGroup -Database $script:mockDatabase -Name 'DeclinedFileGroup' -Confirm:$false -WhatIf
92+
93+
$result | Should -BeNullOrEmpty
94+
}
8095
}
8196

8297
Context 'When verifying FileGroup properties' {
@@ -89,3 +104,4 @@ Describe 'New-SqlDscFileGroup' -Tag @('Integration_SQL2017', 'Integration_SQL201
89104
}
90105
}
91106
}
107+

tests/Unit/Public/New-SqlDscDataFile.Tests.ps1

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Describe 'New-SqlDscDataFile' -Tag 'Public' {
6464
} -ScriptBlock {
6565
param ($mockFileGroupObject)
6666

67-
$result = New-SqlDscDataFile -FileGroup $mockFileGroupObject -Name 'MyDataFile' -FileName 'C:\Data\MyDataFile.mdf'
67+
$result = New-SqlDscDataFile -FileGroup $mockFileGroupObject -Name 'MyDataFile' -FileName 'C:\Data\MyDataFile.mdf' -Confirm:$false
6868

6969
$result | Should -Not -BeNullOrEmpty
7070
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.DataFile'
@@ -80,7 +80,7 @@ Describe 'New-SqlDscDataFile' -Tag 'Public' {
8080
} -ScriptBlock {
8181
param ($mockFileGroupObject)
8282

83-
$result = New-SqlDscDataFile -FileGroup $mockFileGroupObject -Name 'MySnapshot_Data' -FileName 'C:\Snapshots\MySnapshot_Data.ss'
83+
$result = New-SqlDscDataFile -FileGroup $mockFileGroupObject -Name 'MySnapshot_Data' -FileName 'C:\Snapshots\MySnapshot_Data.ss' -Confirm:$false
8484

8585
$result | Should -Not -BeNullOrEmpty
8686
$result.Name | Should -Be 'MySnapshot_Data'
@@ -95,7 +95,7 @@ Describe 'New-SqlDscDataFile' -Tag 'Public' {
9595
} -ScriptBlock {
9696
param ($mockFileGroupObject)
9797

98-
$result = $mockFileGroupObject | New-SqlDscDataFile -Name 'PipelineDataFile' -FileName 'C:\Data\PipelineDataFile.ndf'
98+
$result = $mockFileGroupObject | New-SqlDscDataFile -Name 'PipelineDataFile' -FileName 'C:\Data\PipelineDataFile.ndf' -Confirm:$false
9999

100100
$result | Should -Not -BeNullOrEmpty
101101
$result.Name | Should -Be 'PipelineDataFile'
@@ -104,6 +104,33 @@ Describe 'New-SqlDscDataFile' -Tag 'Public' {
104104
}
105105
}
106106

107+
It 'Should support Force parameter to bypass confirmation' {
108+
InModuleScope -Parameters @{
109+
mockFileGroupObject = $mockFileGroupObject
110+
} -ScriptBlock {
111+
param ($mockFileGroupObject)
112+
113+
$result = New-SqlDscDataFile -FileGroup $mockFileGroupObject -Name 'ForcedDataFile' -FileName 'C:\Data\ForcedDataFile.mdf' -Force
114+
115+
$result | Should -Not -BeNullOrEmpty
116+
$result.Name | Should -Be 'ForcedDataFile'
117+
$result.FileName | Should -Be 'C:\Data\ForcedDataFile.mdf'
118+
$result.Parent | Should -Be $mockFileGroupObject
119+
}
120+
}
121+
122+
It 'Should return null when WhatIf is specified' {
123+
InModuleScope -Parameters @{
124+
mockFileGroupObject = $mockFileGroupObject
125+
} -ScriptBlock {
126+
param ($mockFileGroupObject)
127+
128+
$result = New-SqlDscDataFile -FileGroup $mockFileGroupObject -Name 'WhatIfDataFile' -FileName 'C:\Data\WhatIfDataFile.mdf' -WhatIf
129+
130+
$result | Should -BeNullOrEmpty
131+
}
132+
}
133+
107134
Context 'When creating a standalone DataFile' {
108135
It 'Should create a DataFile without FileGroup successfully' {
109136
$result = New-SqlDscDataFile -Name 'StandaloneDataFile' -FileName 'C:\Data\StandaloneDataFile.mdf'
@@ -134,15 +161,15 @@ Describe 'New-SqlDscDataFile' -Tag 'Public' {
134161
$commandInfo.ParameterSets.Name | Should -Contain 'Standalone'
135162
}
136163

137-
It 'Should have WithFileGroup as the default parameter set' {
164+
It 'Should have Standalone as the default parameter set' {
138165
$defaultParameterSet = $commandInfo.ParameterSets | Where-Object { $_.IsDefault }
139-
$defaultParameterSet.Name | Should -Be 'WithFileGroup'
166+
$defaultParameterSet.Name | Should -Be 'Standalone'
140167
}
141168

142-
It 'Should have FileGroup as an optional parameter in WithFileGroup set' {
169+
It 'Should have FileGroup as a mandatory parameter in WithFileGroup set' {
143170
$parameterInfo = $commandInfo.Parameters['FileGroup']
144171
$withFileGroupAttribute = $parameterInfo.Attributes | Where-Object { $_ -is [Parameter] -and $_.ParameterSetName -eq 'WithFileGroup' }
145-
$withFileGroupAttribute.Mandatory | Should -BeFalse
172+
$withFileGroupAttribute.Mandatory | Should -BeTrue
146173
}
147174

148175
It 'Should not have FileGroup parameter in Standalone set' {
@@ -165,5 +192,23 @@ Describe 'New-SqlDscDataFile' -Tag 'Public' {
165192
$parameterInfo = $commandInfo.Parameters['FileGroup']
166193
$parameterInfo.Attributes.ValueFromPipeline | Should -Contain $true
167194
}
195+
196+
It 'Should support ShouldProcess' {
197+
$commandInfo.Parameters.ContainsKey('WhatIf') | Should -BeTrue
198+
$commandInfo.Parameters.ContainsKey('Confirm') | Should -BeTrue
199+
}
200+
201+
It 'Should have Force parameter only in WithFileGroup parameter set' {
202+
$parameterInfo = $commandInfo.Parameters['Force']
203+
$parameterInfo | Should -Not -BeNullOrEmpty
204+
$parameterInfo.ParameterSets.Keys | Should -Contain 'WithFileGroup'
205+
$parameterInfo.ParameterSets.Keys | Should -Not -Contain 'Standalone'
206+
}
207+
208+
It 'Should have ConfirmImpact set to High' {
209+
$commandInfo.ScriptBlock.Attributes | Where-Object { $_.TypeId.Name -eq 'CmdletBindingAttribute' } |
210+
ForEach-Object { $_.ConfirmImpact } | Should -Be 'High'
211+
}
168212
}
169213
}
214+

0 commit comments

Comments
 (0)