Use powershell to set the SQL Server Protocol

Source: Internet
Author: User
Tags mssqlserver
Using ing SQL protocols through windows powershell

Sometimes we are asked about the possibility of processing ing 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 Server \ 100 \ tools \ binn \ sqlps.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 varous 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.