I have a basic script that I want to improve so it goes off and collects perfmon counters as Jobs instead of what I am currently doing (which is centrally retrieving counters).
The problem is, when I run Get-Counter in a Job in Powershell, the data that comes back can’t be inspected/used properly. I think it’s because it serializes the data.
This is my script, pretty basic:
Start-Job -ScriptBlock {$Counter = Get-Counter "\LogicalDisk(C:)\% Free Space" -MaxSamples 1 -SampleInterval 1 ; return $Counter}
do
{
[array]$JobCount = Get-Job -State "Completed"
$JobResults = Get-Job | Receive-Job
if ($JobCount.Count -gt 0)
{
Write-Host "Job Completed"
}
else
{
Write-Host "Sleeping 1 Second..."
Start-Sleep 1
}
}
while ($JobCount.Count -lt 0)
$JobResults.CounterSamples
$JobResults.CounterSamples returns a string which says “Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample”. That is the data type of the counter objects.
If I run this outside of a job it returns just fine:
$Counter = Get-Counter "\LogicalDisk(C:)\% Free Space" -MaxSamples 1 -SampleInterval 1
$Counter.CounterSamples
Anyone know how I can use the data within the Job results properly?
Still not had the answer I am looking for on this, any ideas people?
I found a way around this myself, and it was basically to process the Countersamples in the job itself rather than try to pass the results back for processing: