Skip to content

Commit cba192e

Browse files
authored
Add new SQL Server role management commands (#2137)
1 parent 8a69d3b commit cba192e

13 files changed

+1622
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
exists as a login, throwing a terminating error if it doesn't exist.
2828
- Supports pipeline input and provides detailed error messages with localization.
2929
- Uses `Test-SqlDscIsLogin` command for login validation following module patterns.
30-
- `Get-SqlDscLogin`
31-
- Added new public command to get a SQL Server login from a Database Engine instance.
32-
- Returns a `Microsoft.SqlServer.Management.Smo.Login` object that represents
33-
the login.
34-
- Supports getting a specific login by name or all logins if no name is specified.
35-
- Includes a `-Refresh` parameter to refresh the server's login collection
36-
before retrieval.
37-
- `Remove-SqlDscLogin`
38-
- Added new public command to remove a SQL Server login from a Database
39-
Engine instance.
40-
- Supports removing a login by specifying a `ServerObject` and `Name`, or by
41-
passing a `LoginObject` through the pipeline.
42-
- Includes confirmation prompts with `-Force` parameter to bypass confirmation.
43-
- Includes a `-Refresh` parameter to refresh the server's login collection
44-
before attempting removal.
45-
- Provides detailed error messages with localization support.
46-
- `New-SqlDscLogin`
47-
- Added new public command to create a new login on a SQL Server Database
48-
Engine instance.
49-
- Supports creating SQL Server logins, Windows user logins, Windows group
50-
logins, certificate-based logins, and asymmetric key-based logins.
51-
- Implements proper parameter sets to prevent combining hashed passwords
52-
with password policy options, following SQL Server restrictions.
30+
- Added `Get-SqlDscLogin`, `Get-SqlDscRole`, `New-SqlDscLogin`, `New-SqlDscRole`, `Remove-SqlDscRole`, and `Remove-SqlDscLogin` commands for retrieving and managing SQL Server logins and roles with support for refresh, pipeline input, and ShouldProcess.
5331

5432
### Changed
5533

azure-pipelines.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,11 @@ stages:
294294
'tests/Integration/Commands/Assert-SqlDscLogin.Integration.Tests.ps1'
295295
'tests/Integration/Commands/New-SqlDscLogin.Integration.Tests.ps1'
296296
'tests/Integration/Commands/Get-SqlDscLogin.Integration.Tests.ps1'
297+
'tests/Integration/Commands/New-SqlDscRole.Integration.Tests.ps1'
298+
'tests/Integration/Commands/Get-SqlDscRole.Integration.Tests.ps1'
299+
'tests/Integration/Commands/Remove-SqlDscRole.Integration.Tests.ps1'
297300
'tests/Integration/Commands/Remove-SqlDscLogin.Integration.Tests.ps1'
301+
298302
# Group 9
299303
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
300304
)

source/Public/Get-SqlDscRole.ps1

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<#
2+
.SYNOPSIS
3+
Get server roles from a SQL Server Database Engine instance.
4+
5+
.DESCRIPTION
6+
This command gets one or more server roles from a SQL Server Database Engine instance.
7+
If no name is specified, all server roles are returned.
8+
9+
.PARAMETER ServerObject
10+
Specifies current server connection object.
11+
12+
.PARAMETER Name
13+
Specifies the name of the server role to get. If not specified, all
14+
server roles are returned.
15+
16+
.PARAMETER Refresh
17+
Specifies that the **ServerObject**'s roles should be refreshed before
18+
trying to get the role object. This is helpful when roles could have been
19+
modified outside of the **ServerObject**, for example through T-SQL. But
20+
on instances with a large amount of roles it might be better to make
21+
sure the **ServerObject** is recent enough.
22+
23+
.EXAMPLE
24+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
25+
$serverObject | Get-SqlDscRole
26+
27+
Get all server roles from the instance.
28+
29+
.EXAMPLE
30+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
31+
$serverObject | Get-SqlDscRole -Name 'MyCustomRole'
32+
33+
Get the server role named **MyCustomRole**.
34+
35+
.OUTPUTS
36+
`[Microsoft.SqlServer.Management.Smo.ServerRole[]]`
37+
#>
38+
function Get-SqlDscRole
39+
{
40+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
41+
[OutputType([Microsoft.SqlServer.Management.Smo.ServerRole[]])]
42+
[CmdletBinding()]
43+
param
44+
(
45+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
46+
[Microsoft.SqlServer.Management.Smo.Server]
47+
$ServerObject,
48+
49+
[Parameter()]
50+
[System.String]
51+
$Name,
52+
53+
[Parameter()]
54+
[System.Management.Automation.SwitchParameter]
55+
$Refresh
56+
)
57+
58+
process
59+
{
60+
if ($Refresh.IsPresent)
61+
{
62+
# Refresh the server object's roles collection
63+
$ServerObject.Roles.Refresh()
64+
}
65+
66+
Write-Verbose -Message ($script:localizedData.Role_Get -f $ServerObject.InstanceName)
67+
68+
$roleObject = @()
69+
70+
if ($PSBoundParameters.ContainsKey('Name'))
71+
{
72+
$roleObject = $ServerObject.Roles[$Name]
73+
74+
if (-not $roleObject)
75+
{
76+
Write-Verbose -Message ($script:localizedData.Role_NotFound -f $Name)
77+
78+
$missingRoleMessage = $script:localizedData.Role_NotFound -f $Name
79+
80+
$writeErrorParameters = @{
81+
Message = $missingRoleMessage
82+
Category = 'ObjectNotFound'
83+
ErrorId = 'GSDR0001' # cspell: disable-line
84+
TargetObject = $Name
85+
}
86+
87+
Write-Error @writeErrorParameters
88+
}
89+
else
90+
{
91+
Write-Verbose -Message ($script:localizedData.Role_Found -f $Name)
92+
}
93+
}
94+
else
95+
{
96+
Write-Verbose -Message ($script:localizedData.Role_GetAll)
97+
98+
$roleObject = $ServerObject.Roles
99+
}
100+
101+
return [Microsoft.SqlServer.Management.Smo.ServerRole[]] $roleObject
102+
}
103+
}

source/Public/New-SqlDscRole.ps1

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<#
2+
.SYNOPSIS
3+
Creates a new server role in a SQL Server Database Engine instance.
4+
5+
.DESCRIPTION
6+
This command creates a new server role in a SQL Server Database Engine instance.
7+
8+
.PARAMETER ServerObject
9+
Specifies current server connection object.
10+
11+
.PARAMETER Name
12+
Specifies the name of the server role to be created.
13+
14+
.PARAMETER Owner
15+
Specifies the owner of the server role. If not specified, the role
16+
will be owned by the login that creates it.
17+
18+
.PARAMETER Force
19+
Specifies that the role should be created without any confirmation.
20+
21+
.PARAMETER Refresh
22+
Specifies that the **ServerObject**'s roles should be refreshed before
23+
creating the role object. This is helpful when roles could have been
24+
modified outside of the **ServerObject**, for example through T-SQL. But
25+
on instances with a large amount of roles it might be better to make
26+
sure the **ServerObject** is recent enough.
27+
28+
.EXAMPLE
29+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
30+
$serverObject | New-SqlDscRole -Name 'MyCustomRole'
31+
32+
Creates a new server role named **MyCustomRole**.
33+
34+
.EXAMPLE
35+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
36+
$serverObject | New-SqlDscRole -Name 'MyCustomRole' -Owner 'MyOwner' -Force
37+
38+
Creates a new server role named **MyCustomRole** with the specified owner
39+
without prompting for confirmation.
40+
41+
.OUTPUTS
42+
`[Microsoft.SqlServer.Management.Smo.ServerRole]`
43+
#>
44+
function New-SqlDscRole
45+
{
46+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
47+
[OutputType([Microsoft.SqlServer.Management.Smo.ServerRole])]
48+
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
49+
param
50+
(
51+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
52+
[Microsoft.SqlServer.Management.Smo.Server]
53+
$ServerObject,
54+
55+
[Parameter(Mandatory = $true)]
56+
[ValidateNotNullOrEmpty()]
57+
[System.String]
58+
$Name,
59+
60+
[Parameter()]
61+
[System.String]
62+
$Owner,
63+
64+
[Parameter()]
65+
[System.Management.Automation.SwitchParameter]
66+
$Force,
67+
68+
[Parameter()]
69+
[System.Management.Automation.SwitchParameter]
70+
$Refresh
71+
)
72+
73+
process
74+
{
75+
if ($Refresh.IsPresent)
76+
{
77+
# Refresh the server object's roles collection
78+
$ServerObject.Roles.Refresh()
79+
}
80+
81+
Write-Verbose -Message ($script:localizedData.Role_Create -f $Name, $ServerObject.InstanceName)
82+
83+
# Check if the role already exists
84+
if ($ServerObject.Roles[$Name])
85+
{
86+
$errorMessage = $script:localizedData.Role_AlreadyExists -f $Name, $ServerObject.InstanceName
87+
New-InvalidOperationException -Message $errorMessage
88+
}
89+
90+
$verboseDescriptionMessage = $script:localizedData.Role_Create_ShouldProcessVerboseDescription -f $Name, $ServerObject.InstanceName
91+
$verboseWarningMessage = $script:localizedData.Role_Create_ShouldProcessVerboseWarning -f $Name
92+
$captionMessage = $script:localizedData.Role_Create_ShouldProcessCaption
93+
94+
if ($Force.IsPresent -or $PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
95+
{
96+
try
97+
{
98+
$serverRole = New-Object -TypeName Microsoft.SqlServer.Management.Smo.ServerRole -ArgumentList $ServerObject, $Name
99+
100+
if ($PSBoundParameters.ContainsKey('Owner'))
101+
{
102+
$serverRole.Owner = $Owner
103+
}
104+
105+
Write-Verbose -Message ($script:localizedData.Role_Creating -f $Name)
106+
107+
$serverRole.Create()
108+
109+
Write-Verbose -Message ($script:localizedData.Role_Created -f $Name)
110+
111+
return $serverRole
112+
}
113+
catch
114+
{
115+
$errorMessage = $script:localizedData.Role_CreateFailed -f $Name, $ServerObject.InstanceName
116+
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
117+
}
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)