# Powershell 7.x and Get-Counter as a Background Job

**URL:** <https://forums.powershell.org/t/powershell-7-x-and-get-counter-as-a-background-job/15182>\
**Category:** PowerShell Help\
**Created:** [November 4, 2020, 5:04pm UTC](https://forums.powershell.org/t/powershell-7-x-and-get-counter-as-a-background-job/15182 "2020-11-04T17:04:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dexvx](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dexvx](https://forums.powershell.org/u/dexvx)\
**Post date:** [November 4, 2020, 5:04pm UTC](https://forums.powershell.org/t/powershell-7-x-and-get-counter-as-a-background-job/15182/1 "2020-11-04T17:04:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 5, 2020, 6:14am UTC](https://forums.powershell.org/t/powershell-7-x-and-get-counter-as-a-background-job/15182/2 "2020-11-05T06:14:15Z")

</div>

No code usually leads to delayed or no answers.

> I wrote code to use Get-Counter to start the counter collection

This would be a good piece of code to share for us to get started helping you.

---

<div class="post-metadata">

**Author:** ![dexvx](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dexvx](https://forums.powershell.org/u/dexvx)\
**Post date:** [November 5, 2020, 7:41am UTC](https://forums.powershell.org/t/powershell-7-x-and-get-counter-as-a-background-job/15182/3 "2020-11-05T07:41:13Z")

</div>

Apologies, here are some code sniplets

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
```

&nbsp;

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
}
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 5, 2020, 10:20am UTC](https://forums.powershell.org/t/powershell-7-x-and-get-counter-as-a-background-job/15182/4 "2020-11-05T10:20:04Z")

</div>

So if all you’re after is the counter data, couldn’t this work for you?

```
$counterNames = "\Processor(*)\% Interrupt Time"
$SampleInterval = 1
$MaxSamples = 30
 
$CurrentJob = Start-Job -ScriptBlock {
    Param($counterNames, $SampleInterval, $MaxSamples)
    Get-Counter -Counter $counterNames -SampleInterval $SampleInterval -MaxSamples $MaxSamples |
        Select-Object -ExpandProperty CounterSamples | Select -Property *
} -ArgumentList $counterNames, $SampleInterval, $MaxSamples
 
$CurrentJob | Wait-Job -Timeout 60
$data = $CurrentJob | Receive-Job -Wait -AutoRemoveJob
```

You’ll have all the properties that are normally available under CounterSamples

```
Path : \\wks38\processor(_total)\% interrupt time
InstanceName : _total
CookedValue : 0.775770038028371
RawValue : 41814414062
SecondValue : 132490826317850285
MultipleCount : 1
CounterType : Timer100Ns
Timestamp : 2020-11-05 2:43:51 PM
Timestamp100NSec : 132490610317850000
Status : 0
DefaultScale : 0
TimeBase : 10000000
RunspaceId : ad0dd9a2-32c0-4ecd-971f-db555f61f414
```

If you don’t want to see the runspaceid just change the Receive-Job line to

```
$data = $CurrentJob | Receive-Job -Wait -AutoRemoveJob | Select -Property * -ExcludeProperty RunspaceID
```

---

<div class="post-metadata">

**Author:** ![dexvx](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dexvx](https://forums.powershell.org/u/dexvx)\
**Post date:** [November 5, 2020, 7:58pm UTC](https://forums.powershell.org/t/powershell-7-x-and-get-counter-as-a-background-job/15182/5 "2020-11-05T19:58:02Z")

</div>

Thank you for the help.

I settled on keeping the CounterSamples ArrayList and ended up with this:

```
$CurrentJob = Start-Job -ScriptBlock {
    Param($counterNames, $SampleInterval, $MaxSamples)
    Get-Counter -Counter $counterNames -SampleInterval $SampleInterval -MaxSamples $MaxSamples | `
        Select-Object -Property *
} -ArgumentList $counterNames, $SampleInterval, $MaxSamples
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/powershell-7-x-and-get-counter-as-a-background-job/15182/6 "2024-05-16T20:29:10Z")

</div>


