2016-02-25 74 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