如何使用 xml 模板作为输入在 Windows 10 企业服务器上通过 PowerShell v5 及更高版本设置和启动 Perfmon 计数器

问题描述 投票:0回答:1

我正在对 Windows 10 企业服务器上的一些可能的性能问题进行故障排除,并希望为内存设置性能计数器,并为一些进程设置 CPU 利用率。 我已经设置了几个 Perfmon 计数器并将其保存为一台服务器上的 xml 模板。 我的想法是能够使用这个模板,将它推送到多个服务器,然后使用这个模板设置相同的计数器,这样我就可以节省跨多个服务器创建相同计数器的手动工作。 有什么方法可以通过 PowerShell v5 及更高版本完成上述任务吗?还有其他方法可以通过 PowerShell 实现相同的目的吗?

我已将我的 Perfmon 计数器保存为 xml 模板。 我发现使用 - 'New-Object -COM Pla.DataCollectorSet' 可用于创建数据收集器集。 不确定如何进一步进行。

powershell-5.0 perfmon
1个回答
0
投票

对于通用数据收集器集,我通常使用 2 个 CSV 文件:1 个用于计算机列表,另一个用于计数器,如下所示:

带有标题的计数器列表(CounterList.csv)文件-

CntrPath
\LogicalDisk(C:)\Current Disk Queue Length
\LogicalDisk(C:)\Disk Bytes/sec
\LogicalDisk(C:)\Disk Read Bytes/sec
\LogicalDisk(C:)\Disk Write Bytes/sec
\LogicalDisk(C:)\Disk Writes/sec

带标题的服务器列表 (SrvList.csv) 文件-

Hostname
server1

然后我有一个简单的 PS1,它结合了性能计数器列表和机器名称,并创建了一个“测试”数据收集器-

    # PerfLog.ps1
#   Variables
#------------

$ScriptPath       =   $MyInvocation.MyCommand.Path
$ScriptDir        =   Split-Path -Parent $ScriptPath
$CollectorSetName =   "Test"

# Files for input and output
$DataFilePath     = $ScriptDir
$SrvListFile      = $DataFilePath + "\SrvList.csv"
$CountersFile     = $DataFilePath + "\CounterList.csv"

# get Servers and the Performance Counters list
$Servers          = Import-Csv -Path $SrvListFile
$Counters         = Import-Csv -Path $CountersFile

$AllCountersList = @()


#   Main Program
#---------------
# create a new array for counters (with remote server names)
foreach ($Srv in $Servers)
  {  $SrvName = "\\" + $Srv.Hostname
    foreach ($counter in $Counters)
      { $newCounter       = $SrvName + $counter.CntrPath
        $AllCountersList += $newCounter
      }
  }

# Create a Data CollectorSet object
$datacollectorset = New-Object -COM Pla.DataCollectorSet
$datacollectorset.DisplayName = $CollectorSetName
$datacollectorset.Duration = 60                       # total logging duration in seconds
$datacollectorset.SubdirectoryFormat = 0              # alternate value = 1
$datacollectorset.SubdirectoryFormatPattern = $null   # alternate value = "yyyy\-MM"
$datacollectorset.RootPath = $ScriptDir + "\" + $CollectorSetName

# Create a Data Collector object and specify properties
$DataCollector = $datacollectorset.DataCollectors.CreateDataCollector(0)
$DataCollector.LogFileFormat = 0                      # formats are: 0- CSV, 1- TAB, 2- SQL, 3- BIN
$DataCollector.FileName = $CollectorSetName
$DataCollector.FileNameFormat = 0x1
$DataCollector.FileNameFormatPattern = "\-yyyy\-MM\-dd\_\_HH\-mm"
$DataCollector.SampleInterval = 30  # every 30 secs log data
$DataCollector.LogAppend = $false

# assign perf counters to the DataCollector
$DataCollector.PerformanceCounters = $AllCountersList

# specify credentials under which the collecto will operate
# if you're using a service account
# $datacollectorset.SetCredentials($global:U,$global:P)

    # DataCollector scheduling properties (if it is going to run on a schedule)
      #  $StartDate = [DateTime]('2022-07-01 07:00:00')
      #
      #  $NewSchedule = $datacollectorset.schedules.CreateSchedule()
      #  $NewSchedule.Days = 127
      #  $NewSchedule.StartDate = $StartDate
      #  $NewSchedule.StartTime = $StartDate

    # if a schedule is to be used, add the schedule!!! (disabled)
      #  $datacollectorset.schedules.Add($NewSchedule)
try
  {
    # Add the new DataCollector to the DataCollectorSet
    $datacollectorset.DataCollectors.Add($DataCollector)

    # commit the DataCollectorSet (should be visible in the Performance Mnitor mmc)
    $datacollectorset.Commit($CollectorSetName , $null , 0x0003) | Out-Null
    Write-Host "DataCollectorSet is comitted successfully for " + $CollectorSetName
  }
catch [Exception]
  { Write-Host "Exception Caught: " $_.Exception -ForegroundColor Red
  }

您可以使用注释掉的调度命令(在脚本中)来配置调度运行,或者您可以使用下面的 PS 命令手动启动和停止记录器-

# Create a Data CollectorSet object
$CollectorSetName =   "Test"  # this should match the previously created DataColSet name
$dcs = New-Object -COM Pla.DataCollectorSet
# get ( query) DataCollectorSet
# which should return the configured DataCollectorSet object properties
$dcs.Query( $CollectorSetName, $null )

# then, to start the logging
$dcs.Start($false)

# or to stop the logging
$dcs.stop($false)
# to query if it is running (1) or stopped (0)
$dcs.status

您可以通过使用带有循环(针对每台计算机)的

invoke-command
来扩展这个想法,以远程部署 DCS 并启动、停止和询问状态。

另见: https://www.serverbrain.org/system-administration/how-to-use-powershell-to-manage-counter-logs.html https://www.jonathanmedd.net/2010/11/managing-perfmon-data-collector-sets-with-powershell.html

© www.soinside.com 2019 - 2024. All rights reserved.