Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- This script returns perfmon counters from a SQL Server and writes them to a table
- .DESCRIPTION
- script is dependent upon the existence of the destination table and database
- #.EXAMPLE
- # ExtractPerfmonData -server "YourRemoteServerName" -instance "MSSQL`$SQLTest" -PerfMonRepoSQLServerInstance "YourMonitorServerName\Instance" -PerfMonRepoDB "YourMonitorDatabaseName"
- .EXAMPLE
- ExtractPerfmonData -server "YourRemoteServerName" -PerfMonRepoSQLServerInstance "YourMonitorServerName\Instance" -PerfMonRepoDB "YourMonitorDatabaseName"
- .NOTES
- http://www.travisgan.com/2013/03/powershell-and-performance-monitor.html
- #>
- Import-Module "sqlps" -DisableNameChecking
- function ExtractPerfmonData
- {
- param(
- [Parameter(Mandatory=$true,
- ValueFromPipeline=$true,
- ValueFromPipelineByPropertyName=$true)]
- [ValidateNotNull()]
- [ValidateNotNullOrEmpty()]
- [string]$server,
- # [Parameter(Mandatory=$true,
- # ValueFromPipeline=$true,
- # ValueFromPipelineByPropertyName=$true)]
- # [ValidateNotNull()]
- # [ValidateNotNullOrEmpty()]
- # [string]$instance,
- [Parameter(Mandatory=$true,
- ValueFromPipeline=$true,
- ValueFromPipelineByPropertyName=$true)]
- [ValidateNotNull()]
- [ValidateNotNullOrEmpty()]
- [string]$PerfMonRepoSQLServerInstance,
- [Parameter(Mandatory=$true,
- ValueFromPipeline=$true,
- ValueFromPipelineByPropertyName=$true)]
- [ValidateNotNull()]
- [ValidateNotNullOrEmpty()]
- [string]$PerfMonRepoDB
- )
- [Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet]$collections
- #$PerfMonRepoSQLServerInstance = "YourMonitorServerName"
- #$PerfMonRepoDB = "YourMonitorDatabase"
- $counters = @(
- "\Process(sqlservr)\% Privileged Time",
- "\LogicalDisk(*)\% Free Space",
- "\LogicalDisk(*)\Avg. Disk Queue Length",
- "\LogicalDisk(*)\Avg. Disk sec/Read",
- "\LogicalDisk(*)\Avg. Disk sec/Write",
- "\LogicalDisk(*)\Disk Transfers/sec",
- "\LogicalDisk(*)\Free Megabytes",
- "\Memory\Available MBytes",
- "\Memory\Page Faults/sec",
- "\Memory\Page Reads/sec",
- "\Memory\Pages/sec",
- "\Network Interface(*)\Bytes Received/Sec",
- "\Network Interface(*)\Bytes Sent/sec",
- "\Paging File(*)\% Usage",
- "\PhysicalDisk(*)\Avg. Disk sec/Read",
- "\PhysicalDisk(*)\Avg. Disk sec/Write",
- "\Process(_Total)\IO Data Operations/sec",
- "\Process(_Total)\IO Other Operations/sec",
- "\Processor(_Total)\% Processor Time",
- "\SQLServer:Buffer Manager\Buffer cache hit ratio",
- "\SQLServer:Buffer Manager\Checkpoint pages/sec",
- "\SQLServer:Buffer Manager\Lazy writes/sec",
- "\SQLServer:Buffer Manager\Page Life Expectancy",
- "\SQLServer:Databases(_Total)\Transactions/sec",
- "\SQLServer:General Statistics\Active Temp Tables",
- "\SQLServer:General Statistics\User Connections",
- "\SQLServer:Locks(Database)\Average Wait Time (ms)",
- "\SQLServer:Locks(Database)\Number of Deadlocks/sec",
- "\SQLServer:Memory Manager\Memory Grants Pending",
- "\SQLServer:Memory Manager\Target Server Memory (KB)",
- "\SQLServer:Memory Manager\Total Server Memory (KB)",
- "\SQLServer:SQL Statistics\Batch Requests/sec",
- "\SQLServer:SQL Statistics\SQL Compilations/sec",
- "\SQLServer:SQL Statistics\SQL Re-Compilations/sec"
- "\System\Context Switches/sec",
- "\System\Processor Queue Length"
- )
- $collections = Get-Counter -ComputerName $server -Counter $counters -SampleInterval 1 -MaxSamples 1
- $sampling = $collections.CounterSamples | Select-Object -Property TimeStamp, Path, Cookedvalue
- $xmlString = $sampling | ConvertTo-Xml -As String
- $query = "dbo.usp_InsertPerfmonCounter '$xmlString';"
- Invoke-Sqlcmd -ServerInstance $PerfMonRepoSQLServerInstance -Database $PerfMonRepoDB -Query $query
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement