ConfigureTCP_Firewall_Exception.ps1 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. #
  3. # THIS SAMPLE CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  4. # WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  5. # WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  6. # IF THIS CODE AND INFORMATION IS MODIFIED, THE ENTIRE RISK OF USE OR RESULTS IN
  7. # CONNECTION WITH THE USE OF THIS CODE AND INFORMATION REMAINS WITH THE USER.
  8. # The purpose of this script is
  9. # 1. Configure Firewall Exceptions for SQL
  10. # 2. Configure SQL to Listen on TCP post 1500 and 1501 (DAC)
  11. # 3. Change SQL to Use Mixed Mode Authentication
  12. # 4. Rename SA Account
  13. Try
  14. {
  15. Import-Module sqlps -DisableNameChecking
  16. # Setup the SQL Server Connectivity
  17. $TCPPort = "1500"
  18. $DACPort = "1501"
  19. #This Section of the Code is to add a firewall exception for Ports 1500 and 1501
  20. Write-Host "************************ Configure Firewall Exceptions ********************* " -ForegroundColor DarkYellow
  21. # Prepare the arguments for the NETSH command
  22. $Arguments = "advfirewall firewall add rule name = SQLPort dir = in protocol = tcp action = allow localport = " + $TCPPort + " remoteip = ANY profile = PUBLIC"
  23. # Execute the command silently
  24. $p = Start-Process netsh -ArgumentList $Arguments -wait -NoNewWindow -PassThru
  25. $p.HasExited
  26. $p.ExitCode
  27. # Prepare the arguments for the NETSH command
  28. $Arguments = "advfirewall firewall add rule name = SQLDACPort dir = in protocol = tcp action = allow localport = " + $DACPort + " remoteip = ANY profile = PUBLIC"
  29. # Execute the command silently
  30. $p = Start-Process netsh -ArgumentList $Arguments -wait -NoNewWindow -PassThru
  31. $p.HasExited
  32. $p.ExitCode
  33. Write-Host "************************ Configure TCP Ports ********************* " -ForegroundColor DarkYellow
  34. # Create a SMO object
  35. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
  36. $MachineObject = New-Object ('Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer') $env:COMPUTERNAME
  37. $tcp = $MachineObject.GetSMOObject("ManagedComputer[@Name='" + (Get-Item env:\computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']")
  38. if ($tcp.IsEnabled -ne "True")
  39. {
  40. $tcp.IsEnabled = $true
  41. $tcp.alter
  42. $MachineObject.GetSMOObject($tcp.urn.Value + "/IPAddress[@Name='IPAll']").IPAddressProperties[1].Value = $TCPPort
  43. $tcp.alter()
  44. }
  45. else
  46. {
  47. $MachineObject.GetSMOObject($tcp.urn.Value + "/IPAddress[@Name='IPAll']").IPAddressProperties[1].Value = $TCPPort
  48. $tcp.alter()
  49. }
  50. Write-Host "************************ Configure Mixed Mode Authentication ********************* " -ForegroundColor DarkYellow
  51. $SQLObject = New-Object Microsoft.SqlServer.Management.Smo.Server($env:COMPUTERNAME)
  52. $SQLObject.Settings.LoginMode = [Microsoft.SqlServer.Management.SMO.ServerLoginMode]::Mixed
  53. $SQLObject.Alter()
  54. $SQLObject.Settings.Alter()
  55. Write-Host "************************ Rename SA Login ********************* " -ForegroundColor DarkYellow
  56. $SQL = "ALTER LOGIN sa WITH NAME = OptimusPrime"
  57. Invoke-SqlCmd -ServerInstance . -Query $SQL -Database "master"
  58. $SQL = "ALTER LOGIN OptimusPrime WITH Password = 'LS1setup!'"
  59. Invoke-SqlCmd -ServerInstance . -Query $SQL -Database "master"
  60. $SQL = "ALTER LOGIN OptimusPrime Enable"
  61. Invoke-SqlCmd -ServerInstance . -Query $SQL -Database "master"
  62. }
  63. Catch
  64. {
  65. Write-Host "********** Erorr Configuring TCP/Firewall Options ************" -ForegroundColor Red
  66. }