我正在尝试使用 powershell 设置一个脚本,该脚本每隔一定时间生成 blg 文件。我知道可以使用 perfmon XML 模板来完成此操作,但对于这个特定项目,如果可能的话,我必须使用 powershell 来完成此操作。
我的主要问题是我无法将想要使用的性能计数器列表存储在变量中并重复使用它们。
我尝试使用以下脚本来创建要使用的性能计数器列表:
$Counters = @()
$Counters += '\Memory\Available Bytes'
$counters += '\Paging File(*)\% Usage'
$Counters += '\PhysicalDisk(*)\Disk Reads/sec'
$Counters += '\PhysicalDisk(*)\Disk Writes/sec'
$Counters += '\PhysicalDisk(*)\Avg. Disk sec/Read'
$Counters += '\PhysicalDisk(*)\Avg. Disk sec/Write'
$Counters += '\Processor(*)\% Processor Time'
$Counters += '\System\Processor Queue Length'
foreach($counter in $Counters)
{
$string = $string + ", '" + $counter + "'"
$string = $string.TrimStart(",")
}
如果我继续使用 get-counter $string,我会收到以下错误:
Get-Counter : The specified counter path could not be interpreted.
但是当我复制字符串的精确值并使用 get-counter -counter$string 的值它工作得很好......
有人能告诉我如何获得获取计数器使用带有计数器列表的数组或字符串吗?
答案1
当我使用你的+=
块时,我得到一个$counters
长连接字符串。
$counters += '\Memory\Available Bytes'
$counters += '\Paging File(*)\% Usage'
$counters += '\PhysicalDisk(*)\Disk Reads/sec'
$counters += '\PhysicalDisk(*)\Disk Writes/sec'
$counters += '\PhysicalDisk(*)\Avg. Disk sec/Read'
$counters += '\PhysicalDisk(*)\Avg. Disk sec/Write'
$counters += '\Processor(*)\% Processor Time'
$counters += '\System\Processor Queue Length'
$counters
\Memory\Available Bytes\Paging File(*)\% Usage\PhysicalDisk(*)\Disk Reads/sec\PhysicalDisk(*)\Disk Writes/sec\PhysicalDisk(*)\Avg. Disk sec/Read\PhysicalDisk(*)\Avg. Disk sec/Write\Processor(*)\% Processor Time\System\Processor Queue Length
$counters.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
这可能不是你想要的。如果你$counters
明确地创建一个数组,情况会好一点。
$counters = @()
$counters += '\Memory\Available Bytes'
$counters += '\Paging File(*)\% Usage'
$counters += '\PhysicalDisk(*)\Disk Reads/sec'
$counters += '\PhysicalDisk(*)\Disk Writes/sec'
$counters += '\PhysicalDisk(*)\Avg. Disk sec/Read'
$counters += '\PhysicalDisk(*)\Avg. Disk sec/Write'
$counters += '\Processor(*)\% Processor Time'
$counters += '\System\Processor Queue Length'
$counters.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$counters | Get-Counter