Solved

Add row number to data after sort-object has been applied

Posted on 2013-02-07
3
287 Views
Last Modified: 2013-02-13
I have a load of data which I am applying a Sort-Object to, is there a way I can add a row number to the data after the sort. I will be using this to calculate a percentage of the overall work complete...
0
Comment
Question by:Blowfelt82
  • 2
3 Comments
 

Author Comment

by:Blowfelt82
ID: 38863590
Infact it would also be useful if I could add a column to the data with average run rates etc based on other columns data... If someone could show me roughly how I could acheive that I should be able to work the rest out... Code so far (coutesy of segwig is...)

$total= 5785
'MAC1,'MAC2','MAC3','MAC4' | %{
$path = "\\$_\Z$\TEST\ROUTE"
$mach = $_
Get-ChildItem $path -include ('initialised.txt') -recurse | Select-Object CreationTime, LastWriteTime, @{Name="Machine";Expression={$mach}} , @{Name="Harness";Expression={($_.Directory -Replace ([regex]::Escape("\\$_\Z$\TEST\ROUTE"),""))}}, @{Name="TotalHarnesses";Expression={$total_harnesses}}, @{Name="RunTime";Expression={"{0:N0}" -f ($_.LastWriteTime - $_.CreationTime).TotalMinutes}}| Where-Object {$_.RunTime -gt 0} | Sort-Object CreationTime 
} # | Export-Csv "C:\MachineLoad.txt"

Open in new window

0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 38866715
I'm not sure how the whole "percentage complete" thing is supposed to work, but this should work for adding a row number.  Add the following after the sort to pipe it's output.
| ForEach-Object { $i++; $_ | Add-Member -type noteproperty -name Index -value $i; $_}

Open in new window

You wouldn't be able to then just pipe that to Export-CSV though.  You'd have to save all this data to another variable.  You could then work with that variable to create a new calculated property for average run rates and then output the data.  If I'm understanding what your ouput will be, each row will have a runtime, it couldn't then have an average as well.  What's being averaged?
0
 

Author Closing Comment

by:Blowfelt82
ID: 38884789
Sorry for the late reply, perfect answer!
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question