Advertisement
mike_fal

Configure-SQLMemory

Mar 3rd, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.    Configures a SQL Server instance per the Jonathan Kehayias' guidelines.
  4. .DESCRIPTION
  5.    This script will configure your SQL Server instance per the guidelines
  6.      found in Jonathan Kehayias' blog post: http://www.sqlskills.com/blogs/jonathan/how-much-memory-does-my-sql-server-actually-need/
  7.      The rules are:
  8.         - 1GB for initial OS reserve
  9.         - +1GB per 4GB server RAM up to 16GB
  10.         - +1GB per 8GB server RAM above 16
  11. .PARAMETER
  12.    -instance SQL instance name, i.e. localhost\SQL2012, DBASERVER01
  13.      -apply Defaults to $false to report on changes, but not make them.  Set to $true to apply
  14. .EXAMPLE
  15.    Configure-SQLMemory -instance DBASERVER01 -apply $true
  16. #>
  17.  
  18. param([parameter(Mandatory=$true)][string] $instance
  19.     , [boolean] $apply = $false
  20.     )
  21.    
  22. #load SMO
  23. [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
  24.  
  25. if($instance.Contains("\")){
  26.     $sqlhost = $instance.Split("\") | Select -First 1
  27.     }
  28. else{
  29.     $sqlhost = $instance
  30.     }
  31.  
  32. #set memory variables
  33. $totalmem = (gwmi Win32_ComputerSystem -computername $sqlhost).TotalPhysicalMemory/1GB
  34. $sqlmem = 0
  35.  
  36. #calculate memory
  37. while($totalmem -gt 0){
  38.     if($totalmem -gt 16){
  39.         $sqlmem += [math]::floor(($totalmem-16)/8)
  40.         $totalmem=16
  41.     }
  42.     elseif($totalmem -gt 4){
  43.         $sqlmem += [math]::floor(($totalmem)/4)
  44.         $totalmem = 4
  45.     }
  46.     else{
  47.         $sqlmem += 1
  48.         $totalmem = 0
  49.     }
  50. }
  51.  
  52. #if not in debug mode, alter config.  Otherwise report current and new values.
  53. $srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server $instance
  54.     "Instance:" + $instance
  55.     "Max Memory:" + $srv.Configuration.MaxServerMemory.ConfigValue/1024 + " -> " + $sqlmem
  56.     "Min Memory:" + $srv.Configuration.MinServerMemory.ConfigValue/1024 + " -> " + $sqlmem/2
  57. if($apply){
  58.     $srv.Configuration.MaxServerMemory.ConfigValue = $sqlmem * 1024
  59.     $srv.Configuration.MinServerMemory.ConfigValue = $sqlmem/2 * 1024
  60.     $srv.Configuration.Alter()
  61.     "Configuration Complete!"
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement