Set SQL Server instance port programmatically

code snippet on how to set SQL instance port from powershell script rather then SQL Server Configuration manager


function SetPort($instance, $port)

{

$filterString="InstanceName='$instance' AND IpAddressName='IPAll' AND ProtocolName='Tcp' AND PropertyName='TcpPort'"

$InstanceList=get-wmiobject ServerNetworkProtocolProperty -namespace "root\Microsoft\SqlServer\ComputerManagement10"-filter $filterString

foreach ($sqlInstancein$InstanceList)

{

if ($sqlInstance-ne$null)

{

$sqlInstance.SetStringValue($port)

}

}

}

functionSetArgumentVariables ($argList)

{

foreach ($argin$argList)

{

$argPair=$arg.Split("=")

if ($argPair.Count-eq2)

{

$key=$argPair[0]

$value=$argPair[1]

switch ($key)

{

"port" {

$result= [System.Int32]::TryParse($value, [ref] $global:Port);

if (!$result)

{

$global:Port=""

}

}

"instance" {$global:Instance=$value}

default {}

}

}

}

}

#SetArgumentVariables $args

SetPort "MyInstance""14330"

Author: G L

Greg's a Senior Database Administrator in Austria with over 15 years of experience in IT (servicedesk/helpdesk, software engineer, database administrator) Likes SQL Server administration, powershell and testing new features

Leave a Reply

Your email address will not be published. Required fields are marked *