Skip to content

Set-NetworkConfiguration

DBSystems: Configures network protocols for a SQL Server instance

#Requires -Version 5.0
#Requires -Modules SQLServer

[CmdLetBinding()]
Param(
    [Parameter(Mandatory = $true)]   
    [string]$ServerInstance,    
    [Parameter(Mandatory = $true)]   
    [pscredential]$ServerCredential,
    [ValidateSet('TCP')]
    [string]$Protocol = "TCP",
    [switch]$AutomaticallyAcceptUntrustedCertificates,
    [switch]$Disable,
    [switch]$ForceServiceRestart,
    [int]$ManagementPublicPort,
    [switch]$NoServiceRestart, 
    [int]$Port = 0,   
    [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]$setArgs = @{
        'ErrorAction' = 'Stop'
        'InputObject'  = $instance
        'AutomaticallyAcceptUntrustedCertificates' = $AutomaticallyAcceptUntrustedCertificates.ToBool()
        'Protocol'     = $Protocol
        'Disable'      = $Disable.ToBool()
        'ForceServiceRestart' = $ForceServiceRestart.ToBool()
        'NoServiceRestart' = $NoServiceRestart.ToBool()
        'Port'         = $Port
        'Credential'   = $ServerCredential
        'Confirm'      = $false
    }                               
    if ($ManagementPublicPort -gt 0) {
        $setArgs.Add('ManagementPublicPort', $ManagementPublicPort)
    }
    if ($RetryTimeout -gt 0) {
        $setArgs.Add('RetryTimeout', $RetryTimeout)
    }    

    $result = Set-SqlNetworkConfiguration @setArgs | Select-Object *    
    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 network protocol to configure (Currently only 'TCP' is supported by this script)

Off

Indicates that this cmdlet automatically accepts untrusted certificates

Off

Indicates that this cmdlet disables the specified network protocol

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 port to accept TCP connections. To configure dynamic ports, set this parameter to 0.

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.