性能计数器路径无效

时间:2016-02-28 13:51:45

标签: sql-server powershell counter

此问题与this post有关。

如果计数器看起来像这样,还有一个问题:

"\MSSQL`$SQLMET:Databases($DatabaseName)\Log Bytes Flushed/sec"

我收到此错误:

The \MSSQL$SQLMET:Databases(fee_monsson)\Log Bytes Flushed/sec performance
counter path  is not valid.
At C:\Users\MihaiDarzanGefee\Desktop\sql-perfmon.ps1:49 char:12
+ Get-Counter <<<<  -Counter $counters -SampleInterval 1 -MaxSamples 3600 | 
    + CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
    + FullyQualifiedErrorId : CounterPathIsInvalid,Microsoft.PowerShell.Commands.GetCounterCommand

如果我转到perfmon,我会找到计数器\MSSQL$SQLMET:Databases(fee_monsson)\Log Bytes Flushed/sec,所以我无法理解错误。

代码是:

<#
    .SYNOPSIS
    Collect counters required for DTU Calculator and log as CSV.

    .DESCRIPTION
    Collect counters required for DTU Calculator and log as CSV. Default disk
    drive parameters is F:. Default log file location is C:\sql-perfmon-log.csv.
    Counters are collected at 1 second intervals for 1 hour or 3600 seconds.

    .PARAMETER DatabaseName
    The name of the SQL Server database to monitor.
#>
[CmdletBinding()]
param (
    [Parameter(Mandatory = $true)]
    [String]$DatabaseName
)

$ErrorActionPreference = "Stop"
$VerbosePreference = "Continue"

cls

Write-Output "Collecting counters..."
Write-Output "Press Ctrl+C to exit."

$counters = @("\Processor(_Total)\% Processor Time",
    "\LogicalDisk(C:)\Disk Reads/sec", "\LogicalDisk(C:)\Disk Writes/sec", 
    "\LogicalDisk(C:)\Disk Read Bytes/sec",
    "\LogicalDisk(C:)\Disk Write Bytes/sec",
    "\MSSQL`$SQLMET:Databases($DatabaseName)\Log Bytes Flushed/sec")

Get-Counter -Counter $counters -SampleInterval 1 -MaxSamples 3600 |
    Export-Counter -FileFormat csv -Path "C:\sql-perfmon-log.csv" -Force

1 个答案:

答案 0 :(得分:0)

必须以管理员身份运行powershell脚本(例如:右键单击powershell.exe并单击“以管理员身份运行”,而不仅仅是从管理员帐户运行它)。它抓取性能计数器,而不是数据库中的数据,所以它需要适当的权限。