Group-Object how to access value from Group of formatted tables

Ok after hours of googling I’m finally breaking down and asking for help.
I have a SQL table that looks like this:

timestamp, testname, result
9/5/18, test1, 1111
9/6/18, test1, 2222
9/6/18, test2, 3333
9/6/18, test2, 4444
9/7/18, test2, 5555
9/7/18, test3, 6666

I SELECT * using Invoke-Sqlcmd and store the result in a variable ($SQLResult)…this works fine.
I would like to save the rows from each unique “testname” to a separate csv file. The csv file should be named ‘”timestamp”_”testname”.csv’ using the first timestamp from each test (showing when the test started).

$SQLResult | Group-Object testname | ForEach-Object { $name = $_.Name $_.Group | Export-Csv -Path "C:data$name.csv" -NoTypeInformation } 

This code works perfectly but obviously does not include the Timestamp in the csv filename. How do I get access to the timestamp value?
I feel like it should be something like $_.Group.Timestamp.Value[0].
I’ve also played around with GetEnumerator() but cant get that to work either.

submitted by /u/andersonee
[link] [comments]

Leave a Reply