2016-02-25 78 views
1

我從工作中運行的性能計數器下面的腳本:返回性能計數器結果

$counter = { 
    param($TestLength) 
    $perfsample = Get-Counter -Counter ` 
    "\PhysicalDisk(_Total)\% Disk Time",` 
    "\Processor(_Total)\% Processor Time",` 
    "\Process(_Total)\Working Set",` 
    "\Network Interface(*)\Bytes Total/Sec"` 
    -SampleInterval 1 -MaxSamples $TestLength -ErrorAction SilentlyContinue 

    $perfsample 
} 

Start-Job -Name GettingCounters -ScriptBlock $counter -ArgumentList $TestLength | Wait-Job 

Export-Counter -Path $DestinationFile -FileFormat csv -InputObject (Receive-Job GettingCounters) 

當運行上面的代碼,我出現以下錯誤:

Export-Counter : Cannot bind parameter 'InputObject'. Cannot convert the "Micro 
soft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet" value of type 
"Deserialized.Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample 
Set" to type "Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample 
Set". 

據我所知,這是由於輸出序列化,所以有可能返回一個反序列化的輸出?

回答

1

不幸的是所有類型都不可序列化。爲什麼不直接出口工作內部?

$counter = { 
    param($TestLength, $DestinationFile) 
    $perfsample = Get-Counter -Counter ` 
    "\PhysicalDisk(_Total)\% Disk Time",` 
    "\Processor(_Total)\% Processor Time",` 
    "\Process(_Total)\Working Set",` 
    "\Network Interface(*)\Bytes Total/Sec"` 
    -SampleInterval 1 -MaxSamples $TestLength -ErrorAction SilentlyContinue 

    $perfsample | Export-Counter -Path $DestinationFile -FileFormat csv 
} 

Start-Job -Name GettingCounters -ScriptBlock $counter -ArgumentList $TestLength, $DestinationFile | Wait-Job 

作爲替代方案,你可以使用運行空間,而不是喬布斯它們不需要對象序列化和通常更快(至少當您使用RunspacePool在parrallel運行多個線程)。還有幾條線可以使用它:

$DestinationFile = ".\Desktop\test.csv" 
$TestLength = 10 

$counter = { 
    param($TestLength) 
    $perfsample = Get-Counter -Counter ` 
    "\PhysicalDisk(_Total)\% Disk Time",` 
    "\Processor(_Total)\% Processor Time",` 
    "\Process(_Total)\Working Set",` 
    "\Network Interface(*)\Bytes Total/Sec"` 
    -SampleInterval 1 -MaxSamples $TestLength -ErrorAction SilentlyContinue 

    $perfsample 
} 

$Powershell = [powershell]::Create() 
$Powershell.AddScript($counter) 
$Powershell.AddParameter("TestLength",$TestLength) 
$job = $Powershell.BeginInvoke() 

#Get results 
$result = $Powershell.EndInvoke($job) 
$result | Export-Counter -Path $DestinationFile -FileFormat csv 

#Cleanup 
$Powershell.Dispose() 
$Powershell = $null 
$job = $null