Iron Scripter prelude

Iron Scripter 2019 is Coming

It is a new year and the Chairman is preparing a fiendish new challenge for this year’s PowerShell + DevOps Summit. That time is rapidly. If you partook in last year’s battle then you understand what is expected of you. if you have not already done so align yourself with the faction that best suits you.

In order to prepare you for battle the Chairman has prepared a series of preparatory challenges. It is a foolish warrior who goes into battle unprepared. Just sayin’.

To prepare you for the upcoming battle, see what you can create according to the guiding principles of your faction that solves the following problem. The bottom line is that your solution should work.





The Challenge

The Get-Counter cmdlet is very useful in obtaining performance counter information. Unfortunately, the Chairman feels the output is less than friendly. Especially if you wish to consume the output such as with additional filtering, exporting to a json file or writing to a database.

Your challenge is to create a PowerShell function that takes the output from Get-Counter and transforms it into a more user friendly object. Each counter sample should be a separate object with these properties:

  • Timestamp
  • Computername
  • CounterSet
  • Counter
  • Value

You are free to use whatever property names you want as long as it is clear what the values represent. A sample might look like this:

Advanced Challenge

If you want to really challenge yourself, define a custom type and format view that gives you a result like this:

All the information you need to know is included in this post. The Chairman will share a possible solution within the week. Good Luck and Good Scripting!

9 thoughts on “Iron Scripter 2019 is Coming”

    1. I took a shot at this and here’s what I came up with. The function allows you to specify -tableView $true or $false, and the results will display accordingly as specified by the challenge criteria. If you do not declare -tableView when calling the function, it will default to false and display the list.

      function Parse-CounterData($input, [bool]$tableView)
      {

      $objectArray = @()
      $functionData = $input.CounterSamples | Select Path, CookedValue, Timestamp
      $functionData | Foreach-Object {

      $varPath = $_.Path
      $pathObjects = $varPath.Split(‘\’)

      $object = New-Object -TypeName PSObject
      $object | Add-Member -MemberType NoteProperty -Name ComputerName -Value $pathObjects[2]
      $object | Add-Member -MemberType NoteProperty -Name CounterSet -Value $pathObjects[3]
      $object | Add-Member -MemberType NoteProperty -Name Counter -Value $pathObjects[4]
      $object | Add-Member -MemberType NoteProperty -Name Value -Value $_.CookedValue

      If ($tableView -ne $true)
      {
      Return $object
      }
      else
      {
      $objectArray += $object
      $timestamp = “Timestamp: ” + $_.Timestamp
      }
      }

      If ($tableView -eq $true)
      {

      Return $timestamp, $objectArray | Format-Table
      }

      }

      Get-Counter | Parse-CounterData -tableView $true

  1. Who needs a function

    Get-Counter|foreach-object{
    $Timestamp = $_ |Select-object -expandproperty Timestamp
    $_.CounterSamples | Where-object {$_.path -match “\\\\(.*)\\(.*)\((.*)\)\\(.*)”} | foreach-object{
    $_|Select-object @{l=’Timestamp’;e={$Timestamp}},
    @{l=’Computername’;e={$matches[1]}},
    @{l=’Counterset’;e={$matches[2]}},
    @{l=’Instance’;e={$matches[3]}},
    @{l=’Counter’;e={$matches[4]}},
    @{l=’Value’;e={$_.CookedValue}}
    }}|format-table

    to simple for a function really

  2. function MyFunction {
    param(
    [parameter(Mandatory, ValueFromPipeline)] [object] $InputObject
    )

    $InputObject.CounterSamples | ForEach-Object {
    [void] ($_.Path -match ‘\\\\(?.+?)\\(?.+?)\\(?.+)’)
    $_ | Select-Object -Property @{n=’Datetime’; e={$_.Timestamp}},
    @{n=’Computername’; e={$matches.Computername}},
    @{n=’Counterset’; e={$matches.Counterset}},
    @{n=’Counter’; e={$matches.Counter}},
    @{n=’Value’; e={$_.CookedValue}}
    }
    }

Comments are closed.