Battle Faction Puzzle 5

This topic contains 6 replies, has 5 voices, and was last updated by  Darwin Reiswig 3 months, 1 week ago.

  • Author
    Posts
  • #93429

    Axel Bøg Andersen
    Participant

    My take on a solution:

    This time the code will not run in Powershell 6.0. Seems like Get-Counter was included in the beta version, but the this happend:
    Remove the *-Counter cmdlets in Microsoft.PowerShell.Diagnostics due to the use of unsupported APIs until a better solution is found. (#4303)

    I guess something could be cooked up, that would do the job in 6.0, we could get most of the info by combining the previous puzzles, but for now it's probably better to wait until the team figures out the better solution 🙂

    Fun fact:
    The InterfaceDescription of the Get-NetAdapter is the ProductName on win32_networkadapter objects. The Name parameter on win32_networkadapter objects seems to be the same as the Description parameter on the Netcard->Setting GUI. The Counter Parameter on Get-Counter uses the Name parameter from the win32_networkadapter object. In short: To get the Counters on a specific Netcard, you need to know the description of the netcard.

  • #93433

    Darwin
    Participant

    I'm not going to have anymore time to spend on this, so I thought I'd drop in what I have.

    The below code handles formatting adapter names, including transforms for Intel's frustrating decision to include "(c)" in their device names.

    It also uses 'where HardwareInterface' to filter out virtual adapters that remain after "Get-NetAdapter -Physical".

    (get-counter -counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
    
    (get-counter -counter '\LogicalDisk(C:)\% Free Space').CounterSamples.CookedValue
    
    (get-counter -counter '\Memory\Committed Bytes').CounterSamples.CookedValue
    
    $formattedphysicaladapterdesc = (get-netadapter -physical | where HardwareInterface | select -expand InterfaceDescription).tolower().replace('(','[').replace(')',']')
    
    get-counter -counter '\Network Interface(*)\Bytes Total/sec' | select -expand countersamples | where {$formattedphysicaladapterdesc -icontains $_.InstanceName} | sort-object -property CookedValue | Select-Object -last 2
    
  • #93435

    Yubu
    Participant

    Oneliners, almost...
    5.1

    $counters="\processor(_total)\% processor time","\LogicalDisk(C:)\% Free Space","\memory\% committed bytes in use","\Network Interface(*)\Bytes Total/sec";(get-counter $counters -MaxSamples 1 | select @{n="Time";e={$_.Timestamp}} -ExpandProperty CounterSamples | select time,@{n="Host";e={$env:computername}},path,@{n="Counter";e={[math]::round($_.CookedValue,2)}}  | ? path -notmatch "network interface") + (get-counter $counters -MaxSamples 1 | select  @{n="Time";e={$_.Timestamp}} -ExpandProperty CounterSamples | select time,@{n="Host";e={$env:computername}},path,@{n="Counter";e={[math]::round($_.CookedValue,2)}} | ? path -match "network interface" | sort counter -desc | select -first 2)
    

    6.0

    powershell -noprofile -command {$counters="\processor(_total)\% processor time","\LogicalDisk(C:)\% Free Space","\memory\% committed bytes in use","\Network Interface(*)\Bytes Total/sec";(get-counter $counters | select  @{n="Time";e={$_.Timestamp}} -ExpandProperty CounterSamples | select time,@{n="Host";e={$env:computername}},path,@{n="Counter";e={[math]::round($_.CookedValue,2)}}
     | ? path -notmatch "network interface") + (get-counter $counters | select  @{n="Time";e={$_.Timestamp}} -ExpandProperty CounterSamples | select time,@{n="Host";e={$env:computername}},path,@{n="Counter";e={[math]::round($_.CookedValue,2)}} | ? path -match "network interface" | sort counter -desc | select -first 2)}
    
    • #93439

      js
      Participant

      You must be from the Oneliner Faction.

  • #93447

    Yubu
    Participant

    :). When production is down, you're in the battle. Oneliners will help.
    This is better then previous:

    $counters="\processor(_total)\% processor time","\LogicalDisk(C:)\% Free Space","\memory\% committed bytes in use","\Network Interface(*)\Bytes Total/sec"
    properties=@("time",@{n="Host";e={$env:computername}},"path",@{n="Counter";e={[math]::round($_.CookedValue,2)}})
    (get-counter $counters | select  @{n="Time";e={$_.Timestamp}} -ExpandProperty CounterSamples | select $properties | ? path -notmatch "network interface") + (get-counter $counters | select  @{n="Time";e={$_.Timestamp}} -ExpandProperty CounterSamples | select $properties | ? path -match "network interface" | sort counter -desc | select -first 2)
    
  • #93495

    js
    Participant

    I re-indented it and put the missing $ in front of properties. At the end you could take out (...) + (...) and the result would still be the same.

    $counters="\processor(_total)\% processor time","\LogicalDisk(C:)\% Free Space",
    "\memory\% committed bytes in use","\Network Interface(*)\Bytes Total/sec"
    
    $properties=@("time",@{n="Host";e={$env:computername}},"path",
    @{n="Counter";e={[math]::round($_.CookedValue,2)}})
    
    (get-counter $counters | select @{n="Time";e={$_.Timestamp}} -ExpandProperty CounterSamples |
    select $properties | ? path -notmatch "network interface") +
    (get-counter $counters | select  @{n="Time";e={$_.Timestamp}} -ExpandProperty CounterSamples |
    select $properties | ? path -match "network interface" | sort counter -desc | select -first 2)
    
  • #93508

    Darwin Reiswig
    Participant

    I resisted the urge to add more parameters and flexibility than it needed. Also, who needs number formatting?

    Function Get-ISPerfCounters
    {
        [CmdletBinding()]
        Param (
            [Parameter()]
                [string[]]$ComputerName=$env:COMPUTERNAME,
            [Parameter()]
                [string]$File
        )
    
        Process {
            $Result = @()
            foreach ($Computer in $ComputerName) {
                $NetAdapter = (Get-Counter -ComputerName $Computer "\Network Adapter(*)\Bytes Total/sec").CounterSamples.CookedValue | sort -Descending
    
                $Result +=  @([PSCustomObject]@{
                    DateTime = Get-Date
                    ComputerName = $Computer
                    PctProcessorTime = (Get-Counter -ComputerName $Computer "\Processor(_total)\% Processor Time").CounterSamples.CookedValue
                    PctFreeSpace = (Get-Counter -ComputerName $Computer "\LogicalDisk(C:)\% Free Space").CounterSamples.CookedValue
                    PctMemoryInUse = (Get-Counter -ComputerName $Computer "\memory\% committed bytes in use").CounterSamples.CookedValue
                    BytesTotalPerSec = $NetAdapter[0] + $NetAdapter[1]
                })
            }
            if ($File) {
                $Result | ConvertTo-Html | Out-File $File
            } else {
                Write-Output $Result
            }
        }
    }
    

You must be logged in to reply to this topic.