Explorar o código

Add files via upload

Sourabh Agarwal %!s(int64=8) %!d(string=hai) anos
pai
achega
f31c03e82f

+ 43 - 0
SQLConfigurations/ConfigureSQLAccounts.ps1

@@ -0,0 +1,43 @@
+#  Copyright (c) Microsoft Corporation.  All rights reserved.
+#  
+# THIS SAMPLE CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
+# WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
+# IF THIS CODE AND INFORMATION IS MODIFIED, THE ENTIRE RISK OF USE OR RESULTS IN
+# CONNECTION WITH THE USE OF THIS CODE AND INFORMATION REMAINS WITH THE USER.
+
+# The purpose of this script is to configure the SQL Server startup account and also enable 
+# LPIM and IFI for the account.
+# We would create a local user on the machine and use that as the startup account for SQL.
+# For simplicity the account would be added to the builtin Administrator group.
+Try
+{
+    $connect = [ADSI]"WinNT://localhost"
+    $user = $connect.Create("User","SQLServiceAccount")
+    $user.SetPassword("LS1setup!")
+    $user.setinfo()
+    $user.description = "SQL Server Startup Account"
+    $user.SetInfo()
+    #Add Account to the Admin Group
+    $Admingroup = [ADSI]("WinNT://"+$env:COMPUTERNAME +"/administrators,group")
+    $AdminGroup.Add("WinNT://"+$env:ComputerName +"/SQLServiceAccount,user")
+
+    #Now Change SQL Server Startup Account and Restart the services.
+    Import-Module sqlps -DisableNameChecking
+    Start-Sleep -Seconds 10
+    $SMOWmiserver = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $env:COMPUTERNAME
+    $ChangeService=$SMOWmiserver.Services | where {$_.name -eq "MSSQLSERVER"}
+    $UName=$env:COMPUTERNAME + "\SQLServiceAccount"
+    $PWord="LS1setup!"            
+    $ChangeService.SetServiceAccount($UName, $PWord)
+}
+Catch
+{ 
+  Write-Host "***Erorr Configuring the SQL Startup Account****" -ForegroundColor Red
+}
+
+
+
+
+
+

+ 113 - 0
SQLConfigurations/ConfigureSQLDataLocations.ps1

@@ -0,0 +1,113 @@
+#  Copyright (c) Microsoft Corporation.  All rights reserved.
+#  
+# THIS SAMPLE CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
+# WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
+# IF THIS CODE AND INFORMATION IS MODIFIED, THE ENTIRE RISK OF USE OR RESULTS IN
+# CONNECTION WITH THE USE OF THIS CODE AND INFORMATION REMAINS WITH THE USER.
+
+# The purpose of this script is 
+# 1. Move all Data Files to E Drive of the Server 
+# 2. Move all Log Files to F Drive of the Server 
+# 3. Move Error Log and TraceFiles to E Drive of the Server 
+# 4. Configure TempDB on the D Drive of the Server 
+# 5. Configure backup on the F Drive of the Server
+Try
+{
+    Import-Module sqlps -DisableNameChecking
+    $SMOWmiserver = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $env:COMPUTERNAME         
+    $ChangeService=$SMOWmiserver.Services | where {$_.name -eq "MSSQLSERVER"} 
+    If($ChangeService.ServiceState -eq "Stopped")
+    {
+        $ChangeService.Start()
+    }
+    #Give Time for the Service to Start.
+    start-sleep -Seconds 5
+
+    Write-Host "****************Configuring the TempDB location and Files *********************"
+    $TSQLScript = ""
+    $TSQLScript = " ALTER DATABASE tempdb MODIFY FILE ( NAME = N'tempdev',FILENAME = N'D:\tempdev.mdf' , SIZE = 500MB , FILEGROWTH = 50MB) 
+                    GO
+                    ALTER DATABASE tempdb MODIFY FILE (NAME = N'templog', FILENAME = N'D:\templog.ldf', SIZE = 500MB, FILEGROWTH = 10MB)
+                    Go
+                    ALTER DATABASE tempdb Add FILE ( NAME = N'tempdev2',FILENAME = N'D:\tempdev2.ndf' , SIZE = 500MB , FILEGROWTH = 50MB)
+                    GO
+                    "
+    # Execute the T-SQL script against the SQL Server instance
+    Invoke-SqlCmd -ServerInstance . -Query $TSQLScript -Database "master" -verbose -QueryTimeout 0 | Out-File -filePath "C:\logs.txt"
+
+    #Create Folders for the Data, Logs and Trace Files
+    $datapath = "F:\Data\"
+    $logpath = "G:\Logs\"
+    $ErrorLog = "F:\ErrorLogs\"
+    $BackupPath = "G:\Backups"
+
+    [IO.Directory]::CreateDirectory($datapath)
+    [IO.Directory]::CreateDirectory($logpath)
+    [IO.Directory]::CreateDirectory($ErrorLog)
+    [IO.Directory]::CreateDirectory($BackupPath)
+
+    Write-Host "**************** Configuring Default Locations for the Server ************"
+    $ChangeService.Refresh()
+    $StartupPram = $ChangeService.StartupParameters.Split(';')
+    $MasterDataFile = $StartupPram[0].Substring(2)
+    $MasterLogFile = $StartupPram[2].Substring(2)
+    # Stop SQL and Move the Master MDF/LDF Files
+    $ChangeService.Stop()
+    Start-Sleep 5
+    [IO.File]::Copy($MasterDataFile, "F:\Data\Master.mdf")
+    [IO.File]::Copy($MasterLogFile, "G:\Logs\Mastlog.ldf")
+    #$ChangeService.StartupParameters = "-dE:\Data\Master.mdf;-eE:\ErrorLogs\Errorlog;-lF:\Logs\Mastlog.ldf"
+    $ChangeService.Refresh()
+    $ChangeService.Start()
+    While ($ChangeService.ServiceState -ne "Running")
+    {
+        $ChangeService.Refresh()
+    }
+    #Change the Startup parameters and Instance Properties to reflect new locations
+    $SQLObject = New-Object Microsoft.SqlServer.Management.Smo.Server($env:COMPUTERNAME)
+    $SQLObject.Settings.BackupDirectory = $BackupPath
+    $SQLObject.Settings.DefaultFile = $datapath
+    $SQLObject.Settings.DefaultLog = $logpath
+    $SQLObject.Alter()
+    $SQLObject.Settings.Alter()
+    
+    #Change the SQL Server Startup Parameters -
+    
+
+    Write-Host "********* Moving the System DB's to the Data Drives **********"
+    $CurrentFileLocations = $MasterDataFile.Substring(0,$MasterDataFile.Length-10)
+    $SQLQuery = "ALTER DATABASE Model MODIFY FILE ( NAME = modeldev , FILENAME = 'F:\Data\model.mdf')
+                Go
+                ALTER DATABASE Model MODIFY FILE ( NAME = modellog , FILENAME = 'G:\Logs\modellog.ldf')
+                Go
+                ALTER DATABASE MSDB MODIFY FILE ( NAME = MSDBData , FILENAME = 'F:\Data\MSDBData.mdf')
+                Go
+                ALTER DATABASE MSDB MODIFY FILE ( NAME = MSDBLog , FILENAME = 'G:\Logs\MSDBLog.ldf')
+                Go
+                "
+    Invoke-SqlCmd -ServerInstance . -Query $SQLQuery -Database "master" -verbose -QueryTimeout 0 | Out-File -filePath "C:\logs.txt"
+
+    #Stop and Restart SQL for the values to take effect
+    $ChangeService.Stop()
+    Start-Sleep 30
+    
+    [IO.File]::Copy($CurrentFileLocations+"model.mdf", "F:\Data\model.mdf")
+    [IO.File]::Copy($CurrentFileLocations+"modellog.ldf", "G:\Logs\modellog.ldf")
+    [IO.File]::Copy($CurrentFileLocations+"MSDBData.mdf", "F:\Data\MSDBData.mdf")
+    [IO.File]::Copy($CurrentFileLocations+"MSDBLog.ldf", "G:\Logs\MSDBLog.ldf")
+
+    $ChangeService.Refresh()
+    $ChangeService.Start()
+    Start-Sleep 5
+    While ($ChangeService.ServiceState -ne "Running")
+    {
+        $ChangeService.Refresh()
+        $ChangeService.ServiceState
+    }
+    #Closing Try Block
+}
+Catch
+{
+    Write-Host "********** Erorr in configuration of the SQL Data Locations ************" -ForegroundColor Red
+}

+ 71 - 0
SQLConfigurations/ConfigureTCP_Firewall_Exception.ps1

@@ -0,0 +1,71 @@
+#  Copyright (c) Microsoft Corporation.  All rights reserved.
+#  
+# THIS SAMPLE CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
+# WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
+# IF THIS CODE AND INFORMATION IS MODIFIED, THE ENTIRE RISK OF USE OR RESULTS IN
+# CONNECTION WITH THE USE OF THIS CODE AND INFORMATION REMAINS WITH THE USER.
+
+# The purpose of this script is 
+# 1. Configure Firewall Exceptions for SQL
+# 2. Configure SQL to Listen on TCP post 1500 and 1501 (DAC)
+# 3. Change SQL to Use Mixed Mode Authentication 
+# 4. Rename SA Account
+Try
+{
+    Import-Module sqlps -DisableNameChecking
+    # Setup the SQL Server Connectivity
+    $TCPPort = "1500" 
+    $DACPort = "1501"
+
+    #This Section of the Code is to add a firewall exception for Ports 1500 and 1501
+    Write-Host "************************  Configure Firewall Exceptions ********************* " -ForegroundColor DarkYellow 
+    # Prepare the arguments for the NETSH command
+    $Arguments = "advfirewall firewall add rule name = SQLPort dir = in protocol = tcp action = allow localport = " + $TCPPort + " remoteip = ANY profile = PUBLIC"
+    # Execute the command silently
+    $p = Start-Process netsh -ArgumentList $Arguments -wait -NoNewWindow -PassThru
+    $p.HasExited
+    $p.ExitCode
+    # Prepare the arguments for the NETSH command
+    $Arguments = "advfirewall firewall add rule name = SQLDACPort dir = in protocol = tcp action = allow localport = " + $DACPort + " remoteip = ANY profile = PUBLIC"
+    # Execute the command silently
+    $p = Start-Process netsh -ArgumentList $Arguments -wait -NoNewWindow -PassThru
+    $p.HasExited
+    $p.ExitCode
+
+    Write-Host "************************  Configure TCP Ports ********************* " -ForegroundColor DarkYellow 
+    # Create a SMO object
+    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
+    $MachineObject = New-Object ('Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer') $env:COMPUTERNAME
+    $tcp = $MachineObject.GetSMOObject("ManagedComputer[@Name='" + (Get-Item env:\computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']")
+    if ($tcp.IsEnabled -ne "True")
+    {
+        $tcp.IsEnabled = $true
+        $tcp.alter
+        $MachineObject.GetSMOObject($tcp.urn.Value + "/IPAddress[@Name='IPAll']").IPAddressProperties[1].Value = $TCPPort
+        $tcp.alter()
+    }
+    else
+    {
+        $MachineObject.GetSMOObject($tcp.urn.Value + "/IPAddress[@Name='IPAll']").IPAddressProperties[1].Value = $TCPPort
+        $tcp.alter()
+    }
+
+    Write-Host "************************  Configure Mixed Mode Authentication ********************* " -ForegroundColor DarkYellow
+    $SQLObject = New-Object Microsoft.SqlServer.Management.Smo.Server($env:COMPUTERNAME)
+    $SQLObject.Settings.LoginMode = [Microsoft.SqlServer.Management.SMO.ServerLoginMode]::Mixed
+    $SQLObject.Alter()
+    $SQLObject.Settings.Alter()
+
+    Write-Host "************************  Rename SA Login ********************* " -ForegroundColor DarkYellow
+    $SQL = "ALTER LOGIN sa WITH NAME = OptimusPrime"
+    Invoke-SqlCmd -ServerInstance . -Query $SQL -Database "master"
+    $SQL = "ALTER LOGIN OptimusPrime WITH Password = 'LS1setup!'"
+    Invoke-SqlCmd -ServerInstance . -Query $SQL -Database "master"
+    $SQL = "ALTER LOGIN OptimusPrime Enable"
+    Invoke-SqlCmd -ServerInstance . -Query $SQL -Database "master"
+}
+Catch
+{
+      Write-Host "********** Erorr Configuring TCP/Firewall Options ************" -ForegroundColor Red
+}

+ 46 - 0
SQLConfigurations/Configure_SQLConfig.ps1

@@ -0,0 +1,46 @@
+#  Copyright (c) Microsoft Corporation.  All rights reserved.
+#  
+# THIS SAMPLE CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
+# WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
+# IF THIS CODE AND INFORMATION IS MODIFIED, THE ENTIRE RISK OF USE OR RESULTS IN
+# CONNECTION WITH THE USE OF THIS CODE AND INFORMATION REMAINS WITH THE USER.
+
+# The purpose of this script is 
+# 1. Configure the SQL Configuration Options - Max Server Memory, MAXDOP, Remote Admin Connection
+# 2. Enable/Disable the following on the Model Database
+#   2a. AutoShrink, AutoClose, Limit Autogrow
+Try
+{
+        Write-Host "*************************  SQL Configuration Options *************************"
+        $TSQLScript1 = "
+        exec sp_configure 'show advanced options',1;
+        reconfigure with override"
+        $TSQLScript2 = "
+        exec sp_configure 'remote admin connections',1;
+        reconfigure with override"
+        $TSQLScript3 = "
+        exec sp_configure 'max server memory (MB)',5000;
+        reconfigure with override"
+        $TSQLScript4 = "
+        exec sp_configure 'max degree of parallelism',1;
+        reconfigure with override"
+
+        # Execute the T-SQL script against the SQL Server instance
+        Invoke-SqlCmd -ServerInstance . -Query $TSQLScript1 -Database "master"
+        Invoke-SqlCmd -ServerInstance . -Query $TSQLScript2 -Database "master" 
+        Invoke-SqlCmd -ServerInstance . -Query $TSQLScript3 -Database "master" 
+        Invoke-SqlCmd -ServerInstance . -Query $TSQLScript4 -Database "master" 
+
+        Write-Host "*************************  Model Database Options *************************"
+        
+        Invoke-SQLcmd -Query "ALTER DATABASE MODEL SET AUTO_CREATE_STATISTICS ON (INCREMENTAL = ON );" -ServerInstance . -Database "master" 
+        Invoke-SQLcmd -Query "ALTER DATABASE MODEL SET AUTO_UPDATE_STATISTICS ON;" -ServerInstance . -Database "master" 
+        Invoke-SQLcmd -Query "ALTER DATABASE MODEL SET AUTO_UPDATE_STATISTICS_ASYNC OFF;" -ServerInstance . -Database "master" 
+        Invoke-SQLcmd -Query "ALTER DATABASE MODEL SET AUTO_SHRINK OFF;" -ServerInstance . -Database "master" 
+        Invoke-SQLcmd -Query "ALTER DATABASE MODEL SET AUTO_CLOSE OFF;" -ServerInstance . -Database "master" 
+}
+Catch
+{
+      Write-Host "********** Erorr Configuring SQL Config Options ************" -ForegroundColor Red
+}