Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to set the instance authentication mode #9349

Closed
bilodeauj opened this issue May 13, 2024 · 0 comments
Closed

Add command to set the instance authentication mode #9349

bilodeauj opened this issue May 13, 2024 · 0 comments
Labels
feature new command Feature request that would need a new command triage required New issue that has not been reviewed by maintainers

Comments

@bilodeauj
Copy link

Summarize Command's Functionality

Sometimes after having installed SQL Server there may be a need to change the authentication mode, let's say from Windows to Mixed Mode. I was unable to find an existing command in dbatools that allowed me change the Authentication Mode in SQL Server. The cmdlet should have the ability to provide a list list of sql instance, along with the desired Authentication mode and specify if the services should be restarted following the change in order for it to take effect.

Is there a command that is similiar or close to what you are looking for?

No

Technical Details

Here is some code i came up with and figured i'd share it back in the hopes that it might be useful to others, and might be able to be integrated into dbatools

function Set-DbaInstanceAuthenticationMode
{
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Medium')]
    Param
    (
        [Parameter(ValueFromPipeline)]
        [DbaInstanceParameter[]]$SqlInstance,

        [PSCredential]$SqlCredential,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Integrated','Mixed','Normal')]
        [string]$AuthenticationMode,

        [switch]$Restart,

        [switch]$EnableException
    )

    Begin
    {
    }
    Process
    {

        foreach ($instance in $SqlInstance) {

            try {
                $server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential
            } catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }
            if($server.Settings.LoginMode -ne $AuthenticationMode){
                if ($pscmdlet.ShouldProcess($server, "Change Authentication Mode from $($server.Settings.LoginMode) to $AuthenticationMode."))
                {
                    try {
                        $server.Settings.LoginMode = [Microsoft.SqlServer.Management.SMO.ServerLoginMode] $AuthenticationMode
                        $server.Settings.Alter()

                        if($Restart){
                            Restart-DbaService -SqlInstance $instance
                        }

                    } catch {
                        Stop-Function -Message "Could not modify Authentication Mode for $server" -ErrorRecord $_ -Target $server -Continue
                    }
                }
            } else {
                Write-Message -Level Verbose -Message "Skipping : Athentication Mode is already set to $AuthenticationMode "
            }
        }
    }
}

Set-DbaInstanceAuthenticationMode -SqlInstance "server01.domain.local" -AuthenticationMode Mixed -Restart
@bilodeauj bilodeauj added feature new command Feature request that would need a new command triage required New issue that has not been reviewed by maintainers labels May 13, 2024
@dataplat dataplat locked and limited conversation to collaborators May 30, 2024
@wsmelton wsmelton converted this issue into discussion #9374 May 30, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
feature new command Feature request that would need a new command triage required New issue that has not been reviewed by maintainers
Projects
None yet
Development

No branches or pull requests

1 participant