Can anyone help a newb?

Welcome Forums General PowerShell Q&A Can anyone help a newb?

This topic contains 8 replies, has 4 voices, and was last updated by

 
Participant
1 week, 4 days ago.

  • Author
    Posts
  • #174982

    Participant
    Topics: 1
    Replies: 4
    Points: 22
    Rank: Member

    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
    }

     

  • #175015

    Participant
    Topics: 2
    Replies: 483
    Points: 1,151
    Helping Hand
    Rank: Community Hero

    Couple things to point out here...

    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.

    Something like the following, I suppose?

    • #175018

      Participant
      Topics: 2
      Replies: 483
      Points: 1,151
      Helping Hand
      Rank: Community Hero

      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. 🙂

  • #175027

    Participant
    Topics: 8
    Replies: 1203
    Points: 706
    Helping Hand
    Rank: Major Contributor

    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
        }
    }
    
  • #175045

    Participant
    Topics: 1
    Replies: 4
    Points: 22
    Rank: Member

    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!

  • #175444

    Participant
    Topics: 1
    Replies: 4
    Points: 22
    Rank: Member

    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

  • #175462

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    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.

     

  • #175492

    Participant
    Topics: 1
    Replies: 4
    Points: 22
    Rank: Member

    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.

  • #175495

    Participant
    Topics: 1
    Replies: 4
    Points: 22
    Rank: Member

    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

     

You must be logged in to reply to this topic.