Advertisement
SQLSoldier

Untitled

Mar 14th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # SCRIPT MUST BE RUN AS ADMINISTRATOR
  2. param([string]$SQLInstance)
  3.  
  4.  
  5. $SQLInstance = $SQLInstance.ToUpper()
  6.  
  7. if ($SQLInstance -ilike "*\*")
  8.     {
  9.     $string = $SQLInstance.Split("\")
  10.     $SQLName = $string[0]
  11.     $Instance = $string[1]
  12.     }
  13. else
  14.     {
  15.     $SQLName = $SQLInstance
  16.     $Instance = "MSSQLSERVER"
  17.     }
  18.  
  19.  
  20. $SQLName
  21. $Instance
  22.  
  23. # Load SMO Wmi.ManagedComputer assembly
  24. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
  25.  
  26. Trap {
  27.   $err = $_.Exception
  28.   while ( $err.InnerException )
  29.     {
  30.     $err = $err.InnerException
  31.     write-output $err.Message
  32.     };
  33.     continue
  34.   }
  35.  
  36. # Connect to the instance using SMO
  37. $m = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $SQLName
  38.  
  39.  
  40. $uri = "ManagedComputer[@Name='$SQLName']/ServerInstance[@Name='$Instance']/ServerProtocol[@Name='Tcp']"
  41. $Tcp = $m.GetSmoObject($uri)
  42. $Enabled = $Tcp.IsEnabled
  43.  
  44. #Enable TCP/IP if not enabled
  45. IF (!$Enabled)
  46.     {$Tcp.IsEnabled = $true }
  47.  
  48. #Set to listen on 1433
  49. $m.GetSmoObject($uri + "/IPAddress[@Name='IPAll']").IPAddressProperties[1].Value = "1433"
  50.  
  51. $TCP.alter()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement