我有以下脚本在作业中运行性能计数器:
$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".
我知道这是由于输出被序列化,所以是否可以返回反序列化的输出?
答案 0 :(得分: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以并行方式运行多个线程时)。还有几行可以使用它:
$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