|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Returns whether the server login is enabled or disabled. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Tests the state of a SQL Server login and returns a Boolean result. |
| 7 | + When a Server object is provided, the login is resolved using |
| 8 | + Get-SqlDscLogin (optionally refreshing the server logins first). |
| 9 | + When a Login object is provided, its current state is evaluated directly. |
| 10 | + .PARAMETER ServerObject |
| 11 | + Specifies current server connection object. |
| 12 | +
|
| 13 | + .PARAMETER LoginObject |
| 14 | + Specifies a login object to test. |
| 15 | +
|
| 16 | + .PARAMETER Name |
| 17 | + Specifies the name of the server login to test. |
| 18 | +
|
| 19 | + .PARAMETER Refresh |
| 20 | + Specifies that the **ServerObject**'s logins should be refreshed before |
| 21 | + trying to test the login object. This is helpful when logins could have |
| 22 | + been modified outside of the **ServerObject**, for example through T-SQL. |
| 23 | + But on instances with a large amount of logins it might be better to make |
| 24 | + sure the **ServerObject** is recent enough, or pass in **LoginObject**. |
| 25 | +
|
| 26 | + .INPUTS |
| 27 | + [Microsoft.SqlServer.Management.Smo.Server] |
| 28 | +
|
| 29 | + Server object accepted from the pipeline. |
| 30 | +
|
| 31 | + [Microsoft.SqlServer.Management.Smo.Login] |
| 32 | +
|
| 33 | + Login object accepted from the pipeline. |
| 34 | +
|
| 35 | + .OUTPUTS |
| 36 | + [System.Boolean] |
| 37 | +
|
| 38 | + Returns $true if the login is enabled, $false if the login is disabled. |
| 39 | +
|
| 40 | + .EXAMPLE |
| 41 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 42 | + Test-SqlDscIsLoginEnabled -ServerObject $serverObject -Name 'MyLogin' |
| 43 | +
|
| 44 | + Returns $true if the login is enabled, if not $false is returned. |
| 45 | +
|
| 46 | + .EXAMPLE |
| 47 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 48 | + $loginObject = $serverObject | Get-SqlDscLogin -Name 'MyLogin' |
| 49 | + Test-SqlDscIsLoginEnabled -LoginObject $loginObject |
| 50 | +
|
| 51 | + Returns $true if the login is enabled, if not $false is returned. |
| 52 | +
|
| 53 | + .EXAMPLE |
| 54 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 55 | + $result = $serverObject | Test-SqlDscIsLoginEnabled -Name 'MyLogin' |
| 56 | +
|
| 57 | + Demonstrates pipeline usage with ServerObject. Returns $true if the login is enabled, if not $false is returned. |
| 58 | +
|
| 59 | + .EXAMPLE |
| 60 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 61 | + $loginObject = $serverObject | Get-SqlDscLogin -Name 'MyLogin' |
| 62 | + $result = $loginObject | Test-SqlDscIsLoginEnabled |
| 63 | +
|
| 64 | + Demonstrates pipeline usage with LoginObject. Returns $true if the login is enabled, if not $false is returned. |
| 65 | +#> |
| 66 | +function Test-SqlDscIsLoginEnabled |
| 67 | +{ |
| 68 | + [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.')] |
| 69 | + [CmdletBinding()] |
| 70 | + [OutputType([System.Boolean])] |
| 71 | + param |
| 72 | + ( |
| 73 | + [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)] |
| 74 | + [Microsoft.SqlServer.Management.Smo.Server] |
| 75 | + $ServerObject, |
| 76 | + |
| 77 | + [Parameter(ParameterSetName = 'LoginObject', Mandatory = $true, ValueFromPipeline = $true)] |
| 78 | + [Microsoft.SqlServer.Management.Smo.Login] |
| 79 | + $LoginObject, |
| 80 | + |
| 81 | + [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true)] |
| 82 | + [System.String] |
| 83 | + $Name, |
| 84 | + |
| 85 | + [Parameter(ParameterSetName = 'ServerObject')] |
| 86 | + [System.Management.Automation.SwitchParameter] |
| 87 | + $Refresh |
| 88 | + ) |
| 89 | + |
| 90 | + process |
| 91 | + { |
| 92 | + if ($PSCmdlet.ParameterSetName -eq 'ServerObject') |
| 93 | + { |
| 94 | + $getSqlDscLoginParameters = @{ |
| 95 | + ServerObject = $ServerObject |
| 96 | + Name = $Name |
| 97 | + Refresh = $Refresh |
| 98 | + ErrorAction = 'Stop' |
| 99 | + } |
| 100 | + |
| 101 | + # If this command does not find the login it will throw an exception. |
| 102 | + $loginObjectArray = Get-SqlDscLogin @getSqlDscLoginParameters |
| 103 | + |
| 104 | + # Pick the only object in the array. |
| 105 | + $LoginObject = $loginObjectArray |
| 106 | + } |
| 107 | + |
| 108 | + $loginEnabled = -not $LoginObject.IsDisabled |
| 109 | + |
| 110 | + return $loginEnabled |
| 111 | + } |
| 112 | +} |
0 commit comments