Hello, I’m in the process of migrating our code base to be at least compatible with PowerShell 7 (7.0.3 to be exact). Some of our Powershell code collects perfmon counters via logman and I’m running into an issue converting that to be PS7 friendly.
The code flow is:
[Start Counter Collection as a Job] –> [Do a task] –> [End Counter Collection] –> [Process Counters]
The problem is this:
We were using logman.exe to start/stop the task. This creates a BLG file that can be imported via Import-Counter. However, the Import-Counter cmdlet appears to have been deprecated in PS7. I wrote code to use Get-Counter to start the counter collection, but unfortunately, when you start/stop it via Start-Job scriptblock, the return data gets de-serialized and useless.
I’ve also tried using the PSCompatSession PSSession via Invoke-Command, but it also de-serialized the return data.
The only solutions I’ve read about involves writing the data to disk in CSV format and then importing the data back. However, that does not seem really efficient… especially when dealing with large data sets.
Attempt #1: Using Start-Job scriptblock for Get-Counter cmdlet
Result: $data return from Receive-Job is de-serialized and counter data can’t be used
$counterNames = "\Processor(*)\% Interrupt Time"
$SampleInterval = 1
$MaxSamples = 30
$CurrentJob = Start-Job -ScriptBlock {
Param($counterNames, $SampleInterval, $MaxSamples)
Get-Counter -Counter $counterNames -SampleInterval $SampleInterval -MaxSamples $MaxSamples
} -ArgumentList $counterNames, $SampleInterval, $MaxSamples
# Not the actual function name, but basically we do some processor intensive work here
Invoke-SomeWork
$CurrentJob | Wait-Job -Timeout 60
$data = $CurrentJob | Receive-Job -Wait -AutoRemoveJob
Attempt #2: Using WinPSCompatSession to use Import-Counter cmdlet under PS7.
Result: $data return from Invoke-Command is de-serialized and counter data can’t be used
# Where $BlgFile is the path to the counter file, e.g. C:\Temp\MyCounter.blg
if ($Global:PSVersionTable.PSVersion.Major -ge 7) {
Write-DebugTime "Detected PS7 or newer"
$s = Get-PSSession -Name WinPSCompatSession
$data = Invoke-Command -Session $s -ScriptBlock {
Param($BlgFile)
Import-Counter -Path $BlgFile
} -ArgumentList $BlgFile
} else {
$data = Import-Counter -Path $BlgFile
}