2013-04-26 53 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