Enabling SQL Server 2008 TCP/IP connections via script

Mar 10 2010

As part of allowing users to connect to SQL Server remotely we often need to enable TCP/IP connections which are off by default in Express and other scenarios. While it’s simple to use SQL Server’s “Configuration Manager” to enable or disable certain protocols, sometimes we need to do things in a script, such as for an installer.

This solution uses the SQL PowerShell which is installed automatically with SQL Server 2008. I show how to do it directly or from a wrapper script such as VBScript or DOS.

Enabling TCP/IP through PowerShell

The basic PowerShell (“PS”) commands for doing this are:

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

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

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

$tcp.IsEnabled = $true

$tcp.alter()

Where “instance-name” is the name of the SQL Server instance to make this adjustment for.

This can be used to set up other protocols too, like Named Pipes by making the appropriate changes to the $ProtocolUri variable. Google around for these commands for the proper syntax.

Scripting the Change

The real challenge I ran into using this was that it needed to be run from a DOS batch file, not directly from within PowerShell.

Step #1 – invoking a PowerShell script from the command line.

Turns out powershell.exe expects the next text to be actual PS commands, not filenames such as for scripts. So to invoke the executable and have it execute a saved script, you need to use syntax like this:

powershell.exe -noexit &'c:\scripts\my powershell script.ps1'

  • “&” tells it the filename may contain spaces.
  • -noexit tells PowerShell not to exit without running the commands in the next file. (Intuitive, I know.)

Of course you can do this through normal VBScript shell commands and other techniques as well:


Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit &'c:\scripts\my powershell script.ps1'")

If you want commands like this to invoke with annoying users, check out Scott Hanselman’s blog entry on using a third-party utility library for running scripts in hidden windows.

Step #2 – running from DOS.

What I really wanted was a single line that could be run from within a DOS style batch file without referencing separate scripts. Turns out, we can.

  • Multiple PS commands can be combined in one line, separated with semi-colons (;).
  • We use single quote marks (') instead of doubles because the whole command needs to be a single argument to the PowerShell.exe invocation. In the case of nested quotes, escape the singles with 2-single quotes (''). Yeah, this can get a bit messy.

Here’s the first 5-line PowerShell script invoked by a single command from DOS. (This is all one line.)


"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLPS.exe" "$MachineObject = new-object ('Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer') .;$ProtocolUri = 'ManagedComputer[@Name=''' + (get-item env:\computername).Value + ''']/ServerInstance[@Name=''instance-name'']/ServerProtocol';$tcp = $MachineObject.getsmoobject($ProtocolUri + '[@Name=''Tcp'']');$tcp.IsEnabled = $true;$tcp.alter();"

 

Voila.

MS SQL Server ,

Reading Recommendations to Accompany “Relational Principles” Training

Feb 5 2008

This is an ordered list (not a relation!) of my reading recommendations that emphasize relational fundamentals and building upon that to create great database models and implementations. Many other specific recommendations are scattered throughout the Training Slides (in the notes), for those who have copies of them.

1. C.J. Date, Database In Depth: Relational Theory for Practitioners. O'Reilly Media: 2005. ISBN: 0-596-10012-4. (Amazon link.)

This should be required reading for anyone who is a database professional. This really is a "greatest hits" of the core concepts Date has been espousing for years and which too few people really understand. If you read only one "theory" book, make it this one!

2. C.J. Date, "What First Normal Form Really Means." (Article)
You can get this directly (for a fee) via www.dbdebunk.com, or as Chapter 8 in the recently released compilation of Chris's writings (see below, #11).

This has given me more comprehension, per page of reading, about the relational model than any other thing I have read. It is so much easier to understand this one concept deeply, once, than to read and re-read the pithy two-paragraph spouting found in most modeling books about "eliminating repeating groups."

3. Graeme C. Simsion and Graham C. Witt, Data Modeling Essentials (3rd ed.). Morgan Kaufmann: 2004. ISBN: 0-12-644551-6. (Amazon link.)

  This is my favorite data modeling book and has a very pragmatic balance over theory for theory's sake. It also does an excellent job explaining the more advanced normalization issues. (Note, it does not include anything on 6NF.)
  If you have more a coding bias in your database work, I'd put Code Complete (next) higher on the list than this.

4. Steve McConnell, Code Complete (2nd ed.). Microsoft Press: 2004. ISBN: 0-7356-19670. (Amazon link.)

  Simply one of the best software engineering books ever published. This will help you become a better programmer no matter what language you use.
  If you have more a modeling/design bias in your database work, I'd put Data Modeling Essentials (above) higher on your list.

5. Joe Celko, SQL For Smarties: Advanced SQL Programming (3rd ed.). Morgan-Kaufmann: 2005. ISBN: 0123693799. (Amazon link.)

  Celko is the main guy for coding great SQL and solving problems the "SQL way" that might not be otherwise intuitive. I recommend having and referencing 2 or 3 of his books as needed.
  Note, however, he is no expert at pure relational ideas and often contradicts what I think is a mor academically-correct approach. Read Date for "what's right" and read Celko for "how to get the job done."

6. Stuart R. Faulk, "Software Requirements: A Tutorial." In Software Requirements Engineering, 2nd ed., 1997. (Amazon link for the whole book, or just the article can be found here and other places (just do a search).)

Not only an excellent summary of the Requirements problem as a whole, but an excellent article on how to design work-products (like requirements or models) to be useful to the audience.

7. Robin Williams, The Non-Designer's Design Book, 3rd ed. Peachpit Press: 2008. ISBN: 0-321-19385-7. (Not that Robin Williams!) (Amazon link.)

Your models are only as good as they communicate. This book is a great primer for generally making designed things (documents in particular) good without going through college as a graphic designer. I think anyone who produces materials for others to consume should read this. It's pretty short and fun.

8. Hugh Darwin, "The Askew Wall" (presentation). (Available here.)

Darwin walks through just some of the most egregious problems with the SQL language. Covers both non-relational aspects of SQL as well as just language design issues.


The rest of the list . . .

. . . are better for a team reference library and shared as needed. Most are a read-once, or an occasional reference, but very useful.

9. C.J. Date, The Database Relational Model: A Retrospective Review and Analysis. Addison Wesley: 2001. ISBN: 0-201-61294-1. (Amazon link.)

Reading this short book is probably more useful than reading Codd's original papers. Chris explains those two seminal papers and comments on areas that have changed (few!) and how they stand up to today's criticisms.

10. C.J. Date, An Introduction to Database Systems, 8th ed. Addison-Wesley: 2004. ISBN: 0321197844. (Amazon link.)

This is a de-facto text book for advanced undergraduate or graduate level database courses and is really what put Chris Date "on the map" for his expertise and clear writing. The 8th edition is worth having if for no other reason than Appendix A which shows by example some of why the "Transrelational Model" could be so revolutionary in implementing truly relational products.

11. C.J. Date, Date on Database: Writings 2000-2006. Apress: 2006. ISBN: 1-59059-746-X. (Amazon link.)

  Chris's shorter writings (articles, whitepapers) become available occasionally in collections like this one. Buying this one book gives you "What First Normal Form Really Means" (#2 above) in Chapter 8, and some great retrospective on Ted Codd in Chapter 1, among many other great topics.
  After purchase, you can spend an extra $10 to get the downloadable PDF version of the book from Apress.

12. Len Silverston, The Data Model Resource Book. (2 volumes.) Wiley: 2001. ISBN (vol. 1): 0-471-38023-7, (vol. 2): 0-471-35378-5. (Amazon links to Volume 1 and Volume 2.)

Len's is the best compilation of ready-made database designs covering various common business scenarios (Volume 1) and some specific industries (Volume 2). I don't advocate using the designs verbatim, but they are a great reference and idea source. It's useful to cross-check your own designs with ones this veteran of the field has assembled as a sanity check.

Database, Database Design, Software Engineering , , ,