Can anyone help a newb?

Im trying to get some data from a CSV file. If I run the following, specifying name of VM, I get the output I need.

 

Get-Content ‘.\Collect-IOPS - 1 hour-5-6pm.csv’ | ConvertFrom-Csv | Where {$_.VM -eq “wsus01”} | Measure-Object “WriteIOPS” -Average -Sum -Maximum -Minimum

 

If I try and do a for each loop to get all unique servernames, and perform this task on all, Im getting no output. But it appears to be running based on CPU stats

Apologies in advance, im very new to powershell. Its probably something very simple but its eluding me today!

Loop example is here

 

$rawcsv=Get-Content ‘.\Collect-IOPS - 1 hour-5-6pm.csv’ | ConvertFrom-Csv
$uniquenames=Get-Content ‘.\Collect-IOPS - 1 hour-5-6pm.csv’ | ConvertFrom-Csv | Select VM | Sort-Object -property VM -unique
foreach ($v in $uniquenames)
{
$obj= $rawcsv | Where {$_.VM -eq $v}
echo $obj
}

 

Couple things to point out here…

First, no need to Get-Content | ConvertFrom-Csv – that’s what Import-Csv is for. :slight_smile:

If I understand you correctly, you’re just looking to get the entries for each unique VM in the table and measure the results for that specific VM? I’d say this is a classic use case for Group-Object. This cmdlet essentially groups multiple objects in a dataset that share the same property into a single result object that you can operate on.

Using this, you can group records by the VM name, and then do the measure on each group. Then, I’d probably want to create an output object with all the details, including VM name and all the statistics you’re collecting, so that the output is crystal clear.

Something like the following, I suppose?

https://gist.github.com/vexx32/699d31f61fcc640cfef1b6e65ce22303

Also, slightly off-topic, but if you need some learning material to help you get familiar with PowerShell, PSKoans might be of use to you. :slight_smile:

The problem you are having is that you are trying to work with CSV like a text file.

First, you should be using Import-CSV:

$rawcsv = Import-CSV -Path '.\Collect-IOPS – 1 hour-5-6pm.csv'

Next, you should take a look at Group-Object:

$rawcsv | Group-Object -Property VM

This would group all unique VMs by name. Then you can loop through the groups:

$rawcsv = Import-CSV -Path '.\Collect-IOPS – 1 hour-5-6pm.csv'

$grpVM = $rawcsv | Group-Object -Property VM

foreach ($grp in $grpVM.Group) {
    $grp.Name
}

If you just want the unique vms, you can do this multiple ways as well:

$rawCSV | Select VM -Unique
#or
$grpCSV | Select Name

You would use the group loop if you want to do match on collected information for a group of VM’s, which you can use Measure-Object against a group collection.

$rawcsv = Import-CSV -Path '.\Collect-IOPS – 1 hour-5-6pm.csv'

$grpVM = $rawcsv | Group-Object -Property VM

foreach ($grp in $grpVM.Group) {
    [pscustomobject]@{
        Name = $_.Name
        TotalIops =  Measure-Object -InputObject $_.Iops -Sum
    }
}

Thanks guys, I will try this on Monday. And thanks for the learning resource, I definitely need it :slight_smile:

I’m a bit late to the party, been a sysadmin for years and only ever used vbscript, because it was what I knew and had spent a long time learning and building up my own code library. I’ve finally realised just how good powershell is and decided I have to use it now, so I’ve thrown myself in at the deep end!

Thanks again Guys, Using groups doesn’t seem to get unique names. This may have something to do with the fact the CSV is not sorted by name, it contains a list of names, repeated over and over.

I’ll have a go at sorting that content when I import it and see how that works. Also I had to use $grp.VM, $grp.Name didn’t return anything, probably a typo

Adam,

Post the exact code and output you received, note you can use the -Unique has been frequently spoken about in this thread. What exactly are you seeing with the output.

 

Sure, but the unique cannot be used with the Group-Object method? The last example Rob posted showed me how to use Measure-Object in a foreach loop with a group collection. I don’t seem able to get anything working otherwise.

This code for example:

 

$rawcsv = Import-CSV -Path '.\Collect-IOPS – 1 hour-5-6pm.csv'

$grpVM = $rawcsv | Group-Object -Property VM

foreach ($grp in $grpVM.Group) {
$grp.Name
}


Shows each VM name, but many times, for each instance its seen in the file, not a unique list of VMs.

Actually, THIS code seems to do what I want

 

$rawcsv = Import-CSV -Path '.\Collect-IOPS - 1 hour-5-6pm.csv'

$grpvm = $rawcsv | Select VM -Unique

 

foreach($grp in $grpvm)

{

write-host $grp.VM

$rawcsv | Where {$_.VM -eq $grp.VM} | Measure-Object “WriteIOPS” -Average -Sum -Maximum -Minimum

}


Now I can hopefully play around with the formatting of these results