Get-MSSQL-MaxMemory


posted by Richard Giles
10-06-2008

Downloads: 467
File size: 2.4kB
Views: 1,431

Embed
Get-MSSQL-MaxMemory
  1. ## ===================================================================== 
  2. ## Title       : Get-MSSQL-MaxMemory 
  3. ## Description : Get max memory property from SQL Server 
  4. ## Author      : Idera 
  5. ## Date        : 9/1/2008 
  6. ## Input       : -serverInstance    <server\instance> 
  7. ##                   -verbose  
  8. ##                   -debug     
  9. ## Output      : Integer in MB,  
  10. ##                     -1 if memory is unlimited, 
  11. ##                      -2 if no connection to the desired DB can be obtained 
  12. ## Usage            : PS> . Get-MSSQL-MaxMemory -serverInstance MyServer -v -d 
  13. ## Notes            : Adapted from Jakob Bindslet script 
  14. ## Tag            : SQL Server, ADO, Configuration, Memory 
  15. ## Change log  : 
  16. ## ===================================================================== 
  17.  
  18. param 
  19.       [string]$serverInstance = "local"
  20.     [switch]$verbose
  21.     [switch]$debug 
  22.  
  23. function main() 
  24.     if ($verbose) {$VerbosePreference = "Continue"
  25.     if ($debug) {$DebugPreference = "Continue"
  26.     Get-MSSQL-MaxMemory $serverInstance 
  27.  
  28. function Get-MSSQL-MaxMemory($serverInstance
  29.     trap [Exception]  
  30.     
  31.         write-error $("TRAPPED: " + $_.Exception.Message); 
  32.         continue
  33.     
  34.  
  35.     #initialize ADO connection parameters 
  36.     Write-Debug "Initializing connection parameters..." 
  37.     $adoOpenStatic =
  38.     $adoLockOptimistic =
  39.     $timeout =
  40.  
  41.     #get memory configuration setting from sysconfigures system table 
  42.     # TIP: using ADO to run a query 
  43.     Write-Debug "Connecting to server: $serverInstance" 
  44.     $adoConnection = New-Object -comobject ADODB.Connection 
  45.     $adoRecordset = New-Object -comobject ADODB.Recordset 
  46.     $query = "SELECT value FROM [master].[dbo].[sysconfigures] WHERE config = 1544" 
  47.     $adoConnection.Set_ConnectionString("Provider=SQLOLEDB; Data Source=" + $srv + "; Initial Catalog=master; Integrated Security=SSPI"
  48.     $adoConnection.Set_ConnectionTimeout($timeout
  49.     $adoConnection.Open() 
  50.  
  51.     if ($adoConnection.State -eq "1")  
  52.     
  53.         Write-Debug "Connection succeeded..." 
  54.        $adoRecordset.Open($query, $adoConnection, $adoOpenStatic, $adoLockOptimistic
  55.        $adoRecordset.MoveFirst() 
  56.        $maxMemory = ($adoRecordset.Fields.Item("value").Value
  57.        if ($maxMemory -eq 2147483647)  
  58.        
  59.             Write-Verbose "Max memory is set to unlimited" 
  60.           $maxMemory = -
  61.        }  
  62.        $adoRecordset.Close() 
  63.        $adoConnection.Close() 
  64.     }  
  65.     else  
  66.     
  67.         Write-Debug "Connection failed..." 
  68.        $maxMemory = -
  69.     
  70.     Write-Debug "Max memory is set to $maxMemory" 
  71.     Write-Output $maxMemory 
  72.  
  73. main 
Filed under: , ,

Get max memory property from SQL Server

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.