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

Welcome Forums General PowerShell Q&A Powershell 7.x and Get-Counter as a Background Job

Viewing 4 reply threads
  • Author
    Posts
    • #269210
      Participant
      Topics: 1
      Replies: 2
      Points: 21
      Rank: Member

      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.

      • This topic was modified 4 weeks ago by AndyC.
      • This topic was modified 4 weeks ago by AndyC.
    • #269321
      Participant
      Topics: 9
      Replies: 677
      Points: 2,680
      Helping Hand
      Rank: Community Hero

      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.

    • #269333
      Participant
      Topics: 1
      Replies: 2
      Points: 21
      Rank: Member

      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

       

      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

       

    • #269360
      Participant
      Topics: 9
      Replies: 677
      Points: 2,680
      Helping Hand
      Rank: Community Hero

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

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

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

    • #269417
      Participant
      Topics: 1
      Replies: 2
      Points: 21
      Rank: Member

      Thank you for the help.

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

       

Viewing 4 reply threads
  • You must be logged in to reply to this topic.