我试图设置一个脚本,使用PowerShell生成blg文件每一段时间。 我知道这可以使用perfmon XML模板来完成,但是对于这个特定的项目,我必须尽可能使用powershell来完成。
我的主要问题是,我无法存储我想在一个variables中使用的性能计数器的列表,并重新使用它们。
我试图使用下面的脚本来创build一个性能计数器列表来使用:
$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.
然而,当我复制string的确切值,并使用get-counter -counter $ string的值,它工作正常…
有人可能会build议我让计数器与数组或带有计数器列表的string一起工作吗?
当我使用你的+=块,我得到一个长$countersstring的$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