Home
%3CLINGO-SUB%20id%3D%22lingo-sub-383474%22%20slang%3D%22en-US%22%3EConfiguring%20SQL%20Protocols%20through%20Windows%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383474%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Aug%2028%2C%202008%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3ESometimes%20we%20are%20asked%20about%20the%20possibility%20of%20configuring%20SQL%20Server%20protocols%20through%20PowerShell.%26nbsp%3B%20In%20SQL%20Server%202008%2C%20the%20sqlps%20tool%20incorporates%20WMI%20and%20SMO%20into%20this%20powerful%20Windows%20administrator%20tool%2C%20making%20it%20easy%20to%20manage%20SQL%20Server%20protocols%20through%20PowerShell.%3C%2FP%3E%0A%20%20%3CP%3ETo%20get%20started%2C%20run%20(elevated%2C%20if%20on%20Windows%20Vista%20or%20Windows%20Server%202008)%20sqlps.exe%2C%20which%20by%20default%20is%20located%20at%20the%20%25ProgramFiles%25Microsoft%20SQL%20Server100Toolsbinnsqlps.exe%3B%20or%2C%20if%20your%20architecture%20is%20x64%2C%20it%20is%20in%20the%20same%20path%20as%20above%2C%20under%20your%20Program%20Files%20(x86)%20directory.%3C%2FP%3E%0A%20%20%3CP%3ENow%20that%20you%20have%20an%20Admin%20SQL%20PowerShell%20window%2C%20here%20are%20some%20example%20scripts%20that%20you%20can%20run%20to%20configure%20your%20SQL%20Server%20instance%3A%3C%2FP%3E%0A%20%20%3CP%3E%3CB%3E%20Get%20the%20TCP%2C%20NP%2C%20and%20SM%20objects%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EThis%20script%20is%20the%20starting%20point%20for%20manipulating%20the%20protocols%20properties%20on%20a%20local%20default%20instance.%20%26nbsp%3BTo%20modify%20this%20for%20a%20named%20instance%2C%20replace%20%E2%80%9CMSSQLSERVER%E2%80%9D%20with%20the%20name%20of%20your%20instance.%3C%2FP%3E%0A%20%20%3CP%3E%24MachineObject%20%3D%20new-object%20('Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer')%20.%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%24ProtocolUri%20%3D%20%22ManagedComputer%5B%40Name%3D'%22%20%2B%20(get-item%20env%3Acomputername).Value%20%2B%20%22'%5D%2FServerInstance%5B%40Name%3D'MSSQLSERVER'%5D%2FServerProtocol%22%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%24tcp%20%3D%20%24MachineObject.getsmoobject(%24ProtocolUri%20%2B%20%22%5B%40Name%3D'Tcp'%5D%22)%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%24np%20%3D%20%24MachineObject.getsmoobject(%24ProtocolUri%20%2B%20%22%5B%40Name%3D'Np'%5D%22)%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%24sm%20%3D%20%24MachineObject.getsmoobject(%24ProtocolUri%20%2B%20%22%5B%40Name%3D'Sm'%5D%22)%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CB%3E%20Enable%20remote%20connection%20protocols%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EOnce%20you%20have%20the%20base%20protocol%20objects%2C%20enabling%20remote%20connections%20is%20trivial%3A%3C%2FP%3E%0A%20%20%3CP%3E%24np.IsEnabled%20%3D%20%24true%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%24np.alter()%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%24tcp.IsEnabled%20%3D%20%24true%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%24tcp.alter()%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3ECalling%20the%20.alter()%20method%20commits%20changes%20you%20make%20to%20the%20registry%2C%20and%20you%20will%20need%20to%20restart%20the%20SQL%20Server%20instance%20for%20it%20to%20pick%20up%20these%20changes.%3C%2FP%3E%0A%20%20%3CP%3E%3CB%3E%20More%20elaborate%20example%3A%20Modifying%20an%20instance%E2%80%99s%20TCP%20Port%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EOnce%20you%20have%20the%20TCP%20object%2C%20you%20can%20view%20the%20properties%20of%20the%20TCP%20Ports%20on%20the%20various%20IP%20Addresses%20your%20SQL%20Server%20instance%20is%20listening%20on.%26nbsp%3B%20For%20instance%2C%20this%20command%20will%20show%20the%20properties%20of%20the%20%E2%80%9CIPAll%E2%80%9D%20IP%20Address%3A%3C%2FP%3E%0A%20%20%3CP%3E%24MachineObject.getsmoobject(%24tcp.urn.value%20%2B%20%22%2FIPAddress%5B%40Name%3D'IPAll'%5D%22)%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EThe%20following%20commands%20will%20make%20your%20server%20listen%20on%20the%20TCP%20port%203344%2C%20by%20modifying%20the%20TcpPort%20property%20of%20the%20IPAll%20entry%20and%20then%20committing%20those%20changes%3A%3C%2FP%3E%0A%20%20%3CP%3E%24MachineObject.getsmoobject(%24tcp.urn.Value%20%2B%20%22%2FIPAddress%5B%40Name%3D'IPAll'%5D%22).IPAddressProperties%5B1%5D.Value%20%3D%20%223344%22%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%24tcp.alter()%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3EYou%20can%20now%20verify%20in%20the%20SQL%20Server%20Configuration%20Manager%20that%20your%20IPAll%20setting%20is%20now%20set%20to%20listen%20on%20TCP%20Port%203344%2C%20and%20restarting%20the%20SQL%20Server%20service%20will%20result%20in%20it%20now%20listening%20on%20the%20newly-specified%20port.%3C%2FP%3E%0A%20%20%3CP%3EDan%20Benediktson%20%3CBR%20%2F%3E%20SQL%20Server%20Protocols%20%3CBR%20%2F%3E%20Disclaimer%3A%20This%20posting%20is%20provided%20%22AS%20IS%22%20with%20no%20warranties%2C%20and%20confers%20no%20rights%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383474%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Aug%2028%2C%202008%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Sometimes%20we%20are%20asked%20about%20the%20possibility%20of%20configuring%20SQL%20Server%20protocols%20through%20PowerShell.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383474%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerProtocols%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Aug 28, 2008

Sometimes we are asked about the possibility of configuring SQL Server protocols through PowerShell.  In SQL Server 2008, the sqlps tool incorporates WMI and SMO into this powerful Windows administrator tool, making it easy to manage SQL Server protocols through PowerShell.

To get started, run (elevated, if on Windows Vista or Windows Server 2008) sqlps.exe, which by default is located at the %ProgramFiles%Microsoft SQL Server100Toolsbinnsqlps.exe; or, if your architecture is x64, it is in the same path as above, under your Program Files (x86) directory.

Now that you have an Admin SQL PowerShell window, here are some example scripts that you can run to configure your SQL Server instance:

Get the TCP, NP, and SM objects

This script is the starting point for manipulating the protocols properties on a local default instance.  To modify this for a named instance, replace “MSSQLSERVER” with the name of your instance.

$MachineObject = new-object ('Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer') .

$ProtocolUri = "ManagedComputer[@Name='" + (get-item env:computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol"

$tcp = $MachineObject.getsmoobject($ProtocolUri + "[@Name='Tcp']")

$np = $MachineObject.getsmoobject($ProtocolUri + "[@Name='Np']")

$sm = $MachineObject.getsmoobject($ProtocolUri + "[@Name='Sm']")

Enable remote connection protocols

Once you have the base protocol objects, enabling remote connections is trivial:

$np.IsEnabled = $true

$np.alter()

$tcp.IsEnabled = $true

$tcp.alter()

Calling the .alter() method commits changes you make to the registry, and you will need to restart the SQL Server instance for it to pick up these changes.

More elaborate example: Modifying an instance’s TCP Port

Once you have the TCP object, you can view the properties of the TCP Ports on the various IP Addresses your SQL Server instance is listening on.  For instance, this command will show the properties of the “IPAll” IP Address:

$MachineObject.getsmoobject($tcp.urn.value + "/IPAddress[@Name='IPAll']")

The following commands will make your server listen on the TCP port 3344, by modifying the TcpPort property of the IPAll entry and then committing those changes:

$MachineObject.getsmoobject($tcp.urn.Value + "/IPAddress[@Name='IPAll']").IPAddressProperties[1].Value = "3344"

$tcp.alter()

You can now verify in the SQL Server Configuration Manager that your IPAll setting is now set to listen on TCP Port 3344, and restarting the SQL Server service will result in it now listening on the newly-specified port.

Dan Benediktson
SQL Server Protocols
Disclaimer: This posting is provided "AS IS" with no warranties, and confers no rights