Advertisement
Lars-UT

Using PowerShell and a stored proc to populate a Perfmon Cou

Sep 26th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.    This script returns perfmon counters from a SQL Server and writes them to a table
  4. .DESCRIPTION
  5.    script is dependent upon the existence of the destination table and database
  6. #.EXAMPLE
  7. #   ExtractPerfmonData -server "YourRemoteServerName" -instance "MSSQL`$SQLTest" -PerfMonRepoSQLServerInstance "YourMonitorServerName\Instance" -PerfMonRepoDB "YourMonitorDatabaseName"
  8. .EXAMPLE
  9.    ExtractPerfmonData -server "YourRemoteServerName" -PerfMonRepoSQLServerInstance "YourMonitorServerName\Instance" -PerfMonRepoDB "YourMonitorDatabaseName"
  10. .NOTES
  11.    http://www.travisgan.com/2013/03/powershell-and-performance-monitor.html
  12.    
  13. #>
  14. Import-Module "sqlps" -DisableNameChecking
  15.  
  16. function ExtractPerfmonData
  17. {
  18.     param(
  19.         [Parameter(Mandatory=$true,
  20.                    ValueFromPipeline=$true,
  21.                    ValueFromPipelineByPropertyName=$true)]
  22.                    [ValidateNotNull()]
  23.                    [ValidateNotNullOrEmpty()]
  24.                    [string]$server,
  25.                    
  26. #        [Parameter(Mandatory=$true,
  27. #                   ValueFromPipeline=$true,
  28. #                   ValueFromPipelineByPropertyName=$true)]
  29. #                  [ValidateNotNull()]
  30. #                  [ValidateNotNullOrEmpty()]
  31. #                  [string]$instance,
  32.  
  33.         [Parameter(Mandatory=$true,
  34.                    ValueFromPipeline=$true,
  35.                    ValueFromPipelineByPropertyName=$true)]
  36.                    [ValidateNotNull()]
  37.                    [ValidateNotNullOrEmpty()]
  38.                    [string]$PerfMonRepoSQLServerInstance,
  39.  
  40.         [Parameter(Mandatory=$true,
  41.                    ValueFromPipeline=$true,
  42.                    ValueFromPipelineByPropertyName=$true)]
  43.                    [ValidateNotNull()]
  44.                    [ValidateNotNullOrEmpty()]
  45.                    [string]$PerfMonRepoDB
  46.    )
  47.  
  48.     [Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet]$collections
  49.  
  50.     #$PerfMonRepoSQLServerInstance = "YourMonitorServerName"
  51.     #$PerfMonRepoDB = "YourMonitorDatabase"
  52.  
  53. $counters = @(
  54.         "\Process(sqlservr)\% Privileged Time",
  55.         "\LogicalDisk(*)\% Free Space",
  56.         "\LogicalDisk(*)\Avg. Disk Queue Length",
  57.         "\LogicalDisk(*)\Avg. Disk sec/Read",
  58.         "\LogicalDisk(*)\Avg. Disk sec/Write",
  59.         "\LogicalDisk(*)\Disk Transfers/sec",
  60.         "\LogicalDisk(*)\Free Megabytes",
  61.         "\Memory\Available MBytes",
  62.         "\Memory\Page Faults/sec",
  63.         "\Memory\Page Reads/sec",
  64.         "\Memory\Pages/sec",
  65.         "\Network Interface(*)\Bytes Received/Sec",
  66.         "\Network Interface(*)\Bytes Sent/sec",
  67.         "\Paging File(*)\% Usage",
  68.         "\PhysicalDisk(*)\Avg. Disk sec/Read",
  69.         "\PhysicalDisk(*)\Avg. Disk sec/Write",
  70.         "\Process(_Total)\IO Data Operations/sec",
  71.         "\Process(_Total)\IO Other Operations/sec",
  72.         "\Processor(_Total)\% Processor Time",
  73.         "\SQLServer:Buffer Manager\Buffer cache hit ratio",
  74.         "\SQLServer:Buffer Manager\Checkpoint pages/sec",
  75.         "\SQLServer:Buffer Manager\Lazy writes/sec",
  76.         "\SQLServer:Buffer Manager\Page Life Expectancy",
  77.         "\SQLServer:Databases(_Total)\Transactions/sec",
  78.         "\SQLServer:General Statistics\Active Temp Tables",
  79.         "\SQLServer:General Statistics\User Connections",
  80.         "\SQLServer:Locks(Database)\Average Wait Time (ms)",
  81.         "\SQLServer:Locks(Database)\Number of Deadlocks/sec",
  82.         "\SQLServer:Memory Manager\Memory Grants Pending",
  83.         "\SQLServer:Memory Manager\Target Server Memory (KB)",
  84.         "\SQLServer:Memory Manager\Total Server Memory (KB)",
  85.         "\SQLServer:SQL Statistics\Batch Requests/sec",
  86.         "\SQLServer:SQL Statistics\SQL Compilations/sec",
  87.         "\SQLServer:SQL Statistics\SQL Re-Compilations/sec"
  88.         "\System\Context Switches/sec",
  89.         "\System\Processor Queue Length"
  90.  
  91.         )
  92.  
  93.  
  94.     $collections = Get-Counter -ComputerName $server -Counter $counters -SampleInterval 1 -MaxSamples 1
  95.  
  96.     $sampling = $collections.CounterSamples | Select-Object -Property TimeStamp, Path, Cookedvalue
  97.     $xmlString = $sampling | ConvertTo-Xml -As String
  98.  
  99.     $query = "dbo.usp_InsertPerfmonCounter '$xmlString';"
  100.     Invoke-Sqlcmd -ServerInstance $PerfMonRepoSQLServerInstance -Database $PerfMonRepoDB -Query $query
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement