性能计数器不是由Powershell作业编写的

时间:2013-04-26 19:59:20

标签: powershell performancecounter powershell-v3.0

我正在设置一个可以从任何Windows服务器运行的脚本,以便在滚动日志文件中在后台收集一些perfmon脚本。我可以使用PerfMon GUI,但我认为这会给我一个很好的机会同时学习一些Powershell,我可以根据我的项目特别需要在我自己的简化GUI中包装它。

基本上,脚本会验证ExecutionPolicy是否设置为remotesigned(因此它可以运行),然后在以下命令中定义几个变量以供使用,并概述要抓取的计数器。

之后,它将命令存储为变量,然后将其作为作业启动。问题是我永远不会以我要求的.csv文件的形式看到那份工作的结果。这是代码:

Set-ExecutionPolicy remotesigned -Force
$Computer = $env:COMPUTERNAME
$1GBInBytes = 1GB
$p = "\\$Computer\Process(System)\% Processor Time",
     "\\$Computer\PhysicalDisk(_Total)\Current Disk Queue Length",
     "\\$Computer\PhysicalDisk(0 c:)\% Disk Time",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Queue Length",
     "\\$Computer\PhysicalDisk(0 c:)\% Disk Read Time",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Read Queue Length",
     "\\$Computer\PhysicalDisk(0 c:)\% Disk Write Time",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Write Queue Length",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk sec/Transfer",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk sec/Read",    
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk sec/Write",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Transfers/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Reads/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Writes/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Bytes/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Read Bytes/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Write Bytes/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Bytes/Transfer",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Bytes/Read",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Bytes/Write",
     "\\$Computer\PhysicalDisk(0 c:)\% Idle Time",
     "\\$Computer\PhysicalDisk(0 c:)\Split IO/Sec";

$counter = {get-counter -counter $p -Continuous | Export-Counter  C:\PerfLogs\Storage_BBCRM.csv -Force -FileFormat CSV -Circular -MaxSize $1GBInBytes}
Start-job $counter

有什么想法吗?现在,我想开始作为后台工作,我将通过一个单独的powershell命令停止它。我只是希望它将所有这些都转换成.csv。

1 个答案:

答案 0 :(得分:1)

如果您使用的是psv3,请按以下步骤进行更改:

$counter = {get-counter -counter $using:p -Continuous | Export-Counter  C:\PerfLogs\Storage_BBCRM.csv -Force -FileFormat CSV -Circular -MaxSize $using:1GBInBytes}

否则你无法访问$ p和$ 1GBInBytes

如果您使用的是v2:

$counter = {param($p, $1GBInBytes ) get-counter -counter $p -Continuous | Export-Counter  C:\PerfLogs\Storage_BBCRM.csv -Force -FileFormat CSV -Circular -MaxSize $1GBInBytes}
Start-job $counter -ArgumentList $p, $1GBInBytes