Skip to content

Commit d77fabf

Browse files
committed
Remove-SqlDscLogin: Command to delete SQL Server logins
1 parent 3a792fc commit d77fabf

File tree

8 files changed

+584
-2
lines changed

8 files changed

+584
-2
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"label": "build",
4242
"type": "shell",
4343
"command": "&${cwd}/build.ps1",
44-
"args": [],
44+
"args": ["-AutoRestore", "-UseModuleFast", "-Tasks", "build"],
4545
"presentation": {
4646
"echo": true,
4747
"reveal": "always",

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- Make sure tests forcibly imports the module being tested to avoid AI failing
1111
when testing changes.
12+
- VS Code tasks configuration was improved to support AI.
1213

1314
### Added
1415

@@ -27,9 +28,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2728
- Supports getting a specific login by name or all logins if no name is specified.
2829
- Includes a `-Refresh` parameter to refresh the server's login collection
2930
before retrieval.
31+
- `Remove-SqlDscLogin`
32+
- Added new public command to remove a SQL Server login from a Database
33+
Engine instance.
34+
- Supports removing a login by specifying a `ServerObject` and `Name`, or by
35+
passing a `LoginObject` through the pipeline.
36+
- Includes confirmation prompts with `-Force` parameter to bypass confirmation.
37+
- Includes a `-Refresh` parameter to refresh the server's login collection
38+
before attempting removal.
39+
- Provides detailed error messages with localization support.
3040

3141
### Changed
3242

43+
- Module no longer outputs a warning message during import if SMO dependency
44+
module does not exist, it now outputs a verbose message which is normally
45+
silent by default. This change was made to workaround an issue with DSC v3.
3346
- `azure-pipelines.yml`
3447
- Remove `windows-2019` images fixes [#2106](https://github.com/dsccommunity/SqlServerDsc/issues/2106).
3548
- Move individual tasks to `windows-latest`.

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ stages:
250250
# Group 2
251251
'tests/Integration/Commands/Assert-SqlDscLogin.Integration.Tests.ps1'
252252
'tests/Integration/Commands/Get-SqlDscLogin.Integration.Tests.ps1'
253+
'tests/Integration/Commands/Remove-SqlDscLogin.Integration.Tests.ps1'
253254
# Group 9
254255
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
255256
)
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<#
2+
.SYNOPSIS
3+
Removes a SQL Server login.
4+
5+
.DESCRIPTION
6+
This command removes a SQL Server login from a SQL Server Database Engine instance.
7+
8+
.PARAMETER ServerObject
9+
Specifies current server connection object.
10+
11+
.PARAMETER LoginObject
12+
Specifies a login object to remove.
13+
14+
.PARAMETER Name
15+
Specifies the name of the server login to be removed.
16+
17+
.PARAMETER Force
18+
Specifies that the login should be removed without any confirmation.
19+
20+
.PARAMETER Refresh
21+
Specifies that the **ServerObject**'s logins should be refreshed before
22+
trying removing the login object. This is helpful when logins could have
23+
been modified outside of the **ServerObject**, for example through T-SQL.
24+
But on instances with a large amount of logins it might be better to make
25+
sure the **ServerObject** is recent enough, or pass in **LoginObject**.
26+
27+
.EXAMPLE
28+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
29+
$loginObject = $serverObject | Get-SqlDscLogin -Name 'MyLogin'
30+
$loginObject | Remove-SqlDscLogin
31+
32+
Removes the login named **MyLogin**.
33+
34+
.EXAMPLE
35+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
36+
$serverObject | Remove-SqlDscLogin -Name 'MyLogin'
37+
38+
Removes the login named **MyLogin**.
39+
40+
.INPUTS
41+
Microsoft.SqlServer.Management.Smo.Server
42+
43+
Specifies a server connection object.
44+
45+
.INPUTS
46+
Microsoft.SqlServer.Management.Smo.Login
47+
48+
Specifies a login object.
49+
50+
.OUTPUTS
51+
None.
52+
#>
53+
function Remove-SqlDscLogin
54+
{
55+
[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.')]
56+
[OutputType()]
57+
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
58+
param
59+
(
60+
[Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)]
61+
[Microsoft.SqlServer.Management.Smo.Server]
62+
$ServerObject,
63+
64+
[Parameter(ParameterSetName = 'LoginObject', Mandatory = $true, ValueFromPipeline = $true)]
65+
[Microsoft.SqlServer.Management.Smo.Login]
66+
$LoginObject,
67+
68+
[Parameter(ParameterSetName = 'ServerObject', Mandatory = $true)]
69+
[System.String]
70+
$Name,
71+
72+
[Parameter()]
73+
[System.Management.Automation.SwitchParameter]
74+
$Force,
75+
76+
[Parameter(ParameterSetName = 'ServerObject')]
77+
[System.Management.Automation.SwitchParameter]
78+
$Refresh
79+
)
80+
81+
process
82+
{
83+
if ($Force.IsPresent -and -not $Confirm)
84+
{
85+
$ConfirmPreference = 'None'
86+
}
87+
88+
if ($PSCmdlet.ParameterSetName -eq 'ServerObject')
89+
{
90+
$getSqlDscLoginParameters = @{
91+
ServerObject = $ServerObject
92+
Name = $Name
93+
Refresh = $Refresh
94+
ErrorAction = 'Stop'
95+
}
96+
97+
# If this command does not find the login it will throw an exception.
98+
$loginObjectArray = Get-SqlDscLogin @getSqlDscLoginParameters
99+
100+
# Pick the only object in the array.
101+
$LoginObject = $loginObjectArray | Select-Object -First 1
102+
}
103+
104+
$verboseDescriptionMessage = $script:localizedData.Login_Remove_ShouldProcessVerboseDescription -f $LoginObject.Name, $LoginObject.Parent.InstanceName
105+
$verboseWarningMessage = $script:localizedData.Login_Remove_ShouldProcessVerboseWarning -f $LoginObject.Name
106+
$captionMessage = $script:localizedData.Login_Remove_ShouldProcessCaption
107+
108+
if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
109+
{
110+
try
111+
{
112+
$originalErrorActionPreference = $ErrorActionPreference
113+
114+
$ErrorActionPreference = 'Stop'
115+
116+
$LoginObject.Drop()
117+
}
118+
catch
119+
{
120+
$errorMessage = $script:localizedData.Login_Remove_Failed -f $LoginObject.Name
121+
122+
$PSCmdlet.ThrowTerminatingError(
123+
[System.Management.Automation.ErrorRecord]::new(
124+
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
125+
'RSDL0001', # cspell: disable-line
126+
[System.Management.Automation.ErrorCategory]::InvalidOperation,
127+
$LoginObject.Name
128+
)
129+
)
130+
}
131+
finally
132+
{
133+
$ErrorActionPreference = $originalErrorActionPreference
134+
}
135+
}
136+
}
137+
}

source/en-US/SqlServerDsc.strings.psd1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ ConvertFrom-StringData @'
6767
Login_Get_RetrievingByName = Retrieving login by name '{0}' from server '{1}'.
6868
Login_Get_ReturningAllLogins = Returning all logins from server '{0}'.
6969
70+
## Remove-SqlDscLogin
71+
Login_Remove_ShouldProcessVerboseDescription = Removing the login '{0}' on the instance '{1}'.
72+
Login_Remove_ShouldProcessVerboseWarning = Are you sure you want to remove the login '{0}'?
73+
# This string shall not end with full stop (.) since it is used as a title of ShouldProcess messages.
74+
Login_Remove_ShouldProcessCaption = Remove login on instance
75+
Login_Remove_Failed = Removal of the login '{0}' failed. (RSDL0001)
76+
7077
## Remove-SqlDscAudit
7178
Audit_Remove_ShouldProcessVerboseDescription = Removing the audit '{0}' on the instance '{1}'.
7279
Audit_Remove_ShouldProcessVerboseWarning = Are you sure you want to remove the audit '{0}'?

source/suffix.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ if (-not $env:SqlServerDscCI)
2121
It is not possible to throw the error from Import-SqlDscPreferredModule
2222
since it will just fail the command Import-Module with an obscure error.
2323
#>
24-
Write-Warning -Message $_.Exception.Message
24+
Write-Verbose -Message $_.Exception.Message
2525
}
2626
}

0 commit comments

Comments
 (0)