How do i convert my values in an array/object to measure them?

frupfrup

i am new to stackoverflow and i'm not really a powershell crack. Maybe you can help me with my problem. For monitoring reasons i want to get a ssh result from "vmstat 1 10" on an Linux-Server. I need to get the Avarage Values of the 10 Values i have. A Usual "vmstat 1 10" result looks like this:

procs -----------memory---------- ---swap-- -----io---- -system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0  42352 599980 315860 2488096    0    0  2606   794    5    2  3  0 94  2  0
 0  0  42352 600044 315860 2488096    0    0    64    84 1107 2107  0  1 99  1  0
 0  0  42352 596820 315860 2488096    0    0   280     4 1218 2179  7  1 91  2  0
 0  0  42352 589240 315860 2488096    0    0    32     7 1322 2366  1  1 95  3  0
 0  0  42352 588620 315860 2488096    0    0   144    37 1161 2181  1  0 96  3  0
 0  0  42352 588620 315860 2488096    0    0     0    24 1074 2075  0  0 100  0  0
 0  0  42352 588620 315860 2488096    0    0     0     0 1081 2081  0  1 100  0  0
 0  0  42352 588628 315860 2488096    0    0   128    32 1133 2091  1  0 96  3  0
 0  0  42352 588652 315860 2488100    0    0     0     0 1077 2058  0  0 100  0  0
 0  0  42352 588528 315860 2488100    0    0     0     5 1137 2147  0  0 99  0  0

So to get the AVG of the Values like swpd, free, buff, etc. my plan was to get the whole values in an Object an then make something like $tab.swpd | Measure-Object -avg but i get an error.

Here is my Script to get the values in the Object. (I know that this is not very nice - sorry for that. I'm still learning...):

$runs = 10
#Get the Values via plink (putty)
$vmstat = .\plink.exe -ssh USER@SERVER -batch -pw "PASSWORD-YES-I-KNOW-THIS-IS-BAD" vmstat 1 $runs
$vmstat
##########################
#Get the Values in somthing like a csv
$vmstat = $vmstat.replace(" ", ";")
#replace alle double ";;" with single ";"
while ($vmstat -match ";;"){
    $vmstat = $vmstat.replace(";;", ";")
    }

#Delete ";" at the beginning (and at the end. Just to be sure. normally there arent ";" 's)
$vmstat = $vmstat.Trim(";")
#Save Columnames
$tabhead = $vmstat[1].split(";")
#Delete first and second row
$vmstat = $vmstat[2..($runs+1)]

##########################
#Split Values and Store it in an two-dimensional array
$values = New-Object system.Array[][] $tabhead.Length, $runs

for ($i=0;$i -lt ($vmstat.Length); $i++){
    $tmp = $vmstat[$i].split(";")

    for ($j=0;$j -lt ($tmp.Length); $j++){
        $values[$j][$i] = $tmp[$j]
        }
}

##########################
# Make an Object for Measurement!
$tab = New-Object System.Object

for ($i=0;$i -lt $tabhead.Length; $i++){
    $tab | Add-Member -type NoteProperty -name $tabhead[$i] -Value $values[$i]
}

so after that all my Values are like i want in the Array or in the Object. I am Able to do select Values i want to see:

PS C:\> $tab

r     : {1, 0, 0, 0...}
b     : {0, 0, 0, 0...}
swpd  : {42352, 42352, 42352, 42352...}
free  : {599980, 600044, 596820, 589240...}
buff  : {315860, 315860, 315860, 315860...}
cache : {2488096, 2488096, 2488096, 2488096...}
si    : {0, 0, 0, 0...}
so    : {0, 0, 0, 0...}
bi    : {2606, 64, 280, 32...}
bo    : {794, 84, 4, 7...}
in    : {5, 1107, 1218, 1322...}
cs    : {2, 2107, 2179, 2366...}
us    : {3, 0, 7, 1...}
sy    : {0, 1, 1, 1...}
id    : {94, 99, 91, 95...}
wa    : {2, 1, 2, 3...}
st    : {0, 0, 0, 0...}

PS C:\> $tab.in
5
1107
1218
1322
1161
1074
1081
1133
1077
1137

But when i try somthing like: $tab.swpd | Measure-Object -Average i get the following error:

Measure-Object : Das Eingabeobjekt "System.Object[]" ist nicht numerisch.
In Zeile:1 Zeichen:13
+ $tab.swpd | Measure-Object -Average
+             ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (System.Object[]:Object[]) [Measure-Object], PSInvalidOperationException
    + FullyQualifiedErrorId : NonNumericInputObject,Microsoft.PowerShell.Commands.MeasureObjectCommand

i think i even would not need the Object, i think it must also be posible to do something like $values[0] | Measure-Object -Average but there is the same error. I know the Problem is that the values arent int values. But i dont know how to convert them so i can make my measurements.

Matt

I dont have access to the same resources so I was not able to perfectly recreate the problem. You are right. Measure-Object is trying to tell you that it cannot work with non-numerical values. I think newer versions of powershell handle this better but a cast might do you just fine.

[int[]]($tab.swpd) | Measure-Object -Average

That should cast the System.Object[] array to System.Int32[]

For efficiency

You are splitting the empty space with semicolons and cosolidating those until you have one. Regex might save that whole loop. This will take groups of whitespace and convert them to a semicolon. That might save your issue towards the end. This might address the issue with you Measure-Object since that should remove all whitespace assuming you are not adding any somewhere else along the way. You would still have to address the leading and trailing semi colons created by this like you already do.

$vmstat = $vmstat.replace("\s+", ";")

Edit from comments

So I see the cast failed. You have whitespace in your array values i didnt notice at first. Perhaps that is why you are having and issue. You have an array of strings it seems. I will look at your code again and find out where these are coming from. There are other ways to address this but a simple one might be to update this line at the bottom of your code

$tab | Add-Member -type NoteProperty -name $tabhead[$i] -Value $values[$i].Trim()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I measure the difference between two values within each row of my dataframe if they are separated by amino acids?

How can I get my measure values legend to appear?

How do I measure FPS in my WebGL page using FPSMeter?

How do I convert a bunch of md files and put them into my page as a prop?

How do I convert the x and y values in polar form from these coupled ODEs to to cartesian form and graph them?

How do I delete elements from a list based on the boolean values in my list instead of deleting them by index?

Tkinter, How do I extract values from a slider, manipulate them and display them in a new grid on my tk window?

How do I measure ping?

How can I convert a collection of values into a HashMap that counts them?

How do I convert certain values of a 2 string array dimensional array to int and compare them with another integer in java?

How do I convert a JSON array into individual values to insert into my SQL table?

How can I convert a dimension into a measure in Tableau

How do I convert my constant into a timezone?

How do I convert my string?

How do I convert my asynctask to Rxjava?

How do I convert my BaseAdapter to a RecyclerAdapter

How do I convert a string of numbers with commas in it to an integer and add them?

How do I correctly convert strings to ints and input them into a list?

How do I convert missing values into strings?

How do I convert RGB to float values?

How do I convert values to iterators

How can I measure my computer temperature?

How do I pass function references into my method and use them?

How do I add values to class objects then put them in an array?

How do I EXTRACT all values ending in .000 and print them?

How do I count values in a column and match them with a specific row?

How do I get decimals values without rounding them off

how do I match up the values in order to compare them

What are the leaf values in sklearn GBDT, and How do I obtain them?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive