1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Powershell -sum and then output totals

Discussion in 'HTML, Graphics & Programming' started by darkgen, Jun 29, 2018.

  1. darkgen

    Wise Guy

    Joined: Mar 8, 2005

    Posts: 2,177

    Location: London, UK

    I am trying to pull together some license consumption statistics where there are multiple license entries for each license type which need to be summed.

    First part gives me:
    Code:
    $usagetot = ($licensePool | where-object {$_.License -like "License Type"} | measure Count ,InUseCount, PooledAvailable,Overdraft -sum)
    
    Outputs ...
    
    Count    : 2
    Average  :
    Sum      : 925
    Maximum  :
    Minimum  :
    Property : Count
    
    Count    : 2
    Average  :
    Sum      : 560
    Maximum  :
    Minimum  :
    Property : InUseCount
    
    Count    : 2
    Average  :
    Sum      : 365
    Maximum  :
    Minimum  :
    Property : PooledAvailable
    
    Count    : 2
    Average  :
    Sum      : 0
    Maximum  :
    Minimum  :
    Property : Overdraft
    
    However, I'm struggling to then call each properties sum separately from this array.

    Ultimately I'm trying to get it into a format recongised by the rrdtool e.g.

    Code:
    $unixtime = [int][double]::Parse($(Get-date -date (Get-Date).ToUniversalTime()-uformat %s))
    
    Something then like -
    $cmd = " rrdtool update rrdtooldb $($unixtime):$($usagetot.Count.sum):$($usagetot.InUseCount.sum):$($usagetot.PooledAvailable.sum):$($usagetot.Overdraft.sum)"
    which would then output like ...
    rrdtool update rrdtooldb 1530260250:925:560:365:0
    I'm almost there ...

    Cheers, Paul.
     
  2. Washout

    Wise Guy

    Joined: Jul 2, 2003

    Posts: 1,488

    Try these?
    ($usagetot | Where {$_.Property -Eq "Count"}).Sum
    ($usagetot | Where {$_.Property -Eq "InUseCount"}).Sum
    ($usagetot | Where {$_.Property -Eq "PooledAvailable"}).Sum
    ($usagetot | Where {$_.Property -Eq "Overdraft"}).Sum

    or if they're always at the same index of the array
    $usagetot[0].Sum
    $usagetot[1].Sum
    $usagetot[2].Sum
    $usagetot[3].Sum
     
  3. darkgen

    Wise Guy

    Joined: Mar 8, 2005

    Posts: 2,177

    Location: London, UK

    Thank you!

    Powershell really does blind-side me with its simplicity!
     
  4. PaulCa

    Gangster

    Joined: May 3, 2018

    Posts: 462

    My God powershell is horrible. No wonder they are implementing bash for windows server.