Get-SQLPerfCounters.ps1 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <#
  2. .SYNOPSIS
  3. Collect performance counter statistics for SQL Server
  4. .VERSION
  5. 1.0
  6. .DESCRIPTION
  7. Collects SQL performance data and stores metrics in local instance database for later extraction
  8. .NOTES
  9. Requires : PowerShell V2+,.NET 2+, dependent on scripts Out-DataTable.ps1 and Write-DataTable.ps1
  10. This script uses the current user's credentials for authentication to the local SQL Server instance, so please ensure your Windows
  11. login has permissions to both the SQL instance and the Windows OS prior to script execution.
  12. .EXAMPLE
  13. Simple usage, using default mandatory parameters
  14. PS C:\foo> .\Get-SQLPerfCounters.ps1 -S 'SQLInstance' -D 'dba_local' -T 'PerformanceCounter'
  15. ----
  16. server : SQLInstanceName
  17. destDatabase : dba_local
  18. destDatabaseTable : PerformanceCounter
  19. ----
  20. .PARAMETER server
  21. Use physical name for standalone installs or virtual name for cluster installations. If this is a named instance, add
  22. the instance name to this string (e.g. parikslaptop\test ).
  23. .PARAMETER destDatabase
  24. Database name where PerformanceCounter table is located
  25. .PARAMETER destDatabaseTable
  26. Table name where performance counter data is stored
  27. #>
  28. param (
  29. [Parameter(Position=0, Mandatory=$true)]
  30. [Alias('S')]
  31. [ValidateLength(1, 50)]
  32. [string] $server,
  33. [Parameter(Position=1, Mandatory=$false)]
  34. [Alias('D')]
  35. [ValidateLength(1, 50)]
  36. [string]$destDatabase = "dba_local",
  37. [Parameter(Position=2, Mandatory=$false)]
  38. [Alias('T')]
  39. [ValidateLength(1, 50)]
  40. [string]$destDatabaseTable = "PerformanceCounter"
  41. )
  42. try{
  43. <#
  44. if(![System.Diagnostics.EventLog]::SourceExists("SQLPerfCounters"))
  45. {[System.Diagnostics.EventLog]::CreateEventSource("SQLPerfCounters","Application")}
  46. #>
  47. $scriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
  48. $dt = $null
  49. .$scriptDir\Out-DataTable.ps1
  50. .$scriptDir\Write-DataTable.ps1
  51. $dbConn = New-Object Data.SqlClient.SqlConnection;
  52. $dbConn.ConnectionString = "Data Source=$server;Initial Catalog=dba_local;Integrated Security=True;"
  53. $dbConn.Open()
  54. $dbCmd = New-Object Data.SqlClient.SqlCommand "SELECT counter_name FROM PerformanceCounterList WHERE is_captured_ind = 1", $dbConn
  55. $dr = $dbCmd.ExecuteReader()
  56. [string[]]$perfList = @()
  57. if($dr.HasRows){
  58. $server2 = $server -replace "\\.*$", ""
  59. $instance = $server.split('\')[1]
  60. if($instance -eq $null){$instance = 'SQLServer'}
  61. while ($dr.Read()){$perfList += $dr["counter_name"]}
  62. $gc = (get-counter -counter $perfList)
  63. foreach($g in $gc.CounterSamples){
  64. $hProp = @{"CounterName" = $g.Path -replace ".*$instance\:|^`\`\`\`\$server2`\`\","";"CounterValue" = $g.CookedValue;"TimeStamp" = $g.TimeStamp}
  65. $ctr = New-Object -TypeName PSObject -Property $hProp | Select-Object CounterName, CounterValue, TimeStamp
  66. $dt += $ctr | Out-DataTable
  67. }
  68. Write-DataTable -ServerInstance $server -Database $destDatabase -TableName $destDatabaseTable -Data $dt -ErrorAction "Stop"
  69. }
  70. }
  71. catch{
  72. [string]$LogName = "Application"
  73. [string]$src = "SQLPerfCounters"
  74. $evnID = 10
  75. $errMssg = $_.Exception.GetBaseException().Message
  76. [string]$entryType = "Error"
  77. $errParms = @{'LogName'=$LogName;'Source'=$src;'EventID'=$evnID;'EntryType'=$entryType;'Message'=$errMssg}
  78. Write-Output @errParms
  79. $errMssg
  80. exit 1
  81. }
  82. finally{
  83. if($dr){$dr.Close()}
  84. if($dbCmd){$dbCmd.Dispose()}
  85. if($dbConn){$dbConn.Close();$dbConn.Dispose()}
  86. }