Skip to content

Set-AuthenticationMode

DBSystems: Configures the authentication mode of a SQL Server instance

#Requires -Version 5.0
#Requires -Modules SQLServer

[CmdLetBinding()]
Param(
    [Parameter(Mandatory = $true)]   
    [string]$ServerInstance,    
    [Parameter(Mandatory = $true)]   
    [pscredential]$ServerCredential,
    [ValidateSet('Normal', 'Integrated', 'Mixed')]
    [string]$Mode = 'Normal',
    [switch]$AutomaticallyAcceptUntrustedCertificates,
    [switch]$ForceServiceRestart,
    [int]$ManagementPublicPort,
    [switch]$NoServiceRestart,
    [int]$RetryTimeout,
    [int]$ConnectionTimeout = 30
)

function Get-SqlServerInstanceInternal {
    [CmdLetBinding()]
    Param(
        [Parameter(Mandatory = $true)]   
        [string]$ServerInstance,    
        [pscredential]$ServerCredential,
        [int]$ConnectionTimeout = 30
    )
    try {
        [hashtable]$cmdArgs = @{
            'ErrorAction' = 'Stop'
            'Confirm' = $false
            'ServerInstance' = $ServerInstance
            'ConnectionTimeout' = $ConnectionTimeout
        }
        if ($null -ne $ServerCredential) {
            $cmdArgs.Add('Credential', $ServerCredential)
        }
        return Get-SqlInstance @cmdArgs
    } catch {
        throw
    }
}

Import-Module SQLServer

try {
    $instance = Get-SqlServerInstanceInternal -ServerInstance $ServerInstance -ServerCredential $ServerCredential -ConnectionTimeout $ConnectionTimeout

    [hashtable]$cmdArgs = @{
        'ErrorAction' = 'Stop'
        'InputObject' = $instance
        'Credential' = $ServerCredential
        'Mode' = $Mode
        'Confirm' = $false
        'NoServiceRestart' = $NoServiceRestart.ToBool()
        'ForceServiceRestart' = $ForceServiceRestart
        'AutomaticallyAcceptUntrustedCertificates' = $AutomaticallyAcceptUntrustedCertificates.ToBool()
    }
    
    if ($ManagementPublicPort -gt 0) {
        $cmdArgs.Add("ManagementPublicPort", $ManagementPublicPort)
    }
    if ($RetryTimeout -gt 0) {
        $cmdArgs.Add("RetryTimeout", $RetryTimeout)
    }
    
    $result = Set-SqlAuthenticationMode @cmdArgs
    Write-Output $result
} catch {
    throw
}

Specifies the name of the target computer including the instance name, e.g. MyServer\Instance

Specifies a PSCredential object for the connection to the SQL Server. ServerCredential is ONLY used for SQL Logins. When you are using Windows Authentication you don't specify -Credential. It is picked up from your current login.

Specifies the authentication mode that will be configured: Normal, Integrated, or Mixed

Off

Indicates that this cmdlet automatically accepts untrusted certificates

Off

Indicates that this cmdlet forces the SQL Server service to restart, if necessary, without prompting the user

Specifies the public management port on the target computer

Off

Indicates that this cmdlet prevents a restart of the SQL Server service without prompting the user

Specifies the time period to retry the command on the target sever

Specifies the time period to retry the command on the target server

An interactive directory of PowerShell scripts.