1

I"m somewhat new to PowerShell and I'm trying to do something that is pretty easy in other languages like C#.NET and Java.

What I'm doing:

  • I'm trying to read in a CSV file with 3 defined columns of "clientid", "parentid" and "schoolid".
  • I need to group to just unique rows of the data.
  • I need to access these rows of data to parse and perform other functions with the data by passing as parameters

    $obj = Import-Csv $Name | Group-Object clientid, parentid, schoolid | 
        foreach-object { 
            $_.group | 
                sort-object clientid, parentid, schoolid | 
                    select  clientid, parentid, schoolid -last 1
        }
    
    foreach ( $row in $obj ) { 
        $_clientid = $row[0];
        $_parentid = $row[1];
        $_schoolid = $row[2];   
        Other-Function -client $_.clientid -parent $_.parentid -schoolid $_.schoolid
    }
    
2
  • In your foreach loop, $row will be the current object. You can directly access its properties like $row.clientid, which can then be used As a parameter value in your function. Commented Nov 20, 2019 at 3:49
  • The index syntax [n] is used to access the nth item (starting from 0) in that position of a collection. $obj is a collection of objects with each of them having all of the properties you selected. So $obj[0] would return the first of those objects with all of its properties. The foreach loop just allows you to iterate over that collection. So $row[1] would almost always return nothing unless $obj contained a collection of collections. Commented Nov 20, 2019 at 3:59

1 Answer 1

1

Since you are only reading a 3-column CSV or only using those 3 columns you may wish to simplify quite a bit:

Import-Csv $Name | 
  Select-Object clientid, parentid, schoolid -unique | 
    Foreach-Object { 
      Other-Function -client $_.clientid -parent $_.parentid -schoolid $_.schoolid
    }

So what are we doing?

  • Import the CSV
  • Select the 3 columns uniquely
  • ...then for each row call your other function.

(I love Group-Object and think it is underused but it isn't necessary here and over-complicates; Select-Object with -Unique is much simpler.)

We might also wonder why your "Other-Function" takes 3 separate parameters instead of an object, but that is somewhat a style choice.

Let PowerShell do (most of) the work for you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the very straight forward answer as it did the trick. My other function takes several parameters instead of an object as I wasn't sure how to do that in PowerShell as I'm still getting into this, but I will look into that to strengthen the script. Thanks!
The simplest way is to change the parameter block of the "Other-Function" to (also) take the Object as such: [Object]$StudentInfo # or whatever you wish to call it. You can leave the separate parameters (as long as you continue to call one set of them with named parameters or you can just remove them if you will always use the 'full object'.) If you leave then then you'll need to make a code distinction between separate and single object. Also note, you can convert a bunch of elements/values to an object in a number of way: perhaps the easiest is [PSCustomObject]@{a=1; b=2; c=3}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.