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
}
First, no need to Get-Content | ConvertFrom-Csv – that’s what Import-Csv is for.
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.
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.
Thanks guys, I will try this on Monday. And thanks for the learning resource, I definitely need it
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
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.