|
| 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 | +} |
0 commit comments