2013-04-26 49 views
0

我正在设置一个脚本,可以从任何Windows服务器运行以在滚动日志文件的背景中收集一些perfmon脚本。我可以使用PerfMon GUI,但我想这会给我一个很好的机会同时学习一些Powershell,并且我可以根据我的项目特别需要将它们全部包装在我自己的简化GUI中。性能计数器不是由Powershell作业编写的

基本上,脚本验证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

如果你正在使用psv3,变化如下:

$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 
+0

我:

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

否则,你可以,如果你正在使用V2不能访问$ P $和1GBInBytes

使用3的时候,我会测试出这个解决方案,然后用$读一些。这是一个普遍的规则,如果我想使用一个局部变量,我将不得不在变量之前指定$ using? – 2013-04-28 16:25:01

+0

是的,对于invoke-command和start-job等,如果你想从该命令的'scriptblock'访问局部变量,你需要指定'$ using',否则你需要使用参数列表 – Jackie 2013-04-28 16:32:11