Solved

Trying to group data in a report

Posted on 2014-03-01
3
244 Views
Last Modified: 2014-03-01
I have a csv file of servers and the amount of data each one uses. I'd like to create a report that contains groups of servers that have a combined total storage of 500GB or less. I'm virutalizing these servers and need to fit them onto 500GB datastores.  

The csv file looks like this:
Server,Storage
servr1,200gb
servr2,100gb
servr3,420gb
servr4,350gb
servr5,50gb

I'm trying to group them so that I would end up with output like this:
Server,Storage,group
servr1,200gb,group1
servr2,100gb,group1
servr3,420gb,group2
servr4,350gb,group3
servr5,50gb,group3

Is there a way in powershell  I could do a "do while" loop or maybe a way in excel that I could do this?
0
Comment
Question by:LN41
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39897849
This should work:
$input = "X:\your\path\servers.csv"
$output = "x:\your\path\servergroups.csv"
$maxStorage = 500

if (!(Test-Path $output)) {
    New-Item $output
}
echo "Server,Storage,group" > $output
$groupNo = 1
$groupStorage = 0
Import-Csv $input | % {
    $currServerStorage = $_.Storage.substring(0,$_.Storage.length - 2)
    if ($groupStorage + $currServerStorage -ge $maxStorage) {
        $groupNo ++
        $groupStorage = 0
    } 
    echo "$($_.Server),$($_.Storage),Group$groupNo" >> $output
    $groupStorage += $currServerStorage
}

Open in new window

HTH,
Dan
0
 
LVL 1

Author Closing Comment

by:LN41
ID: 39897937
That's perfect. Just tested and it's exactly what I needed. One thing though - when I run the script, I get a prompt that says "Type:" I hit enter and it fails, second time it runs and provides output file. Would you know what it's asking for for "Type"? Output type? If not, no biggy, this is a huge help either way.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39898186
Replace this:
New-Item $output
with
New-Item $output -type file
and it won't ask for the type anymore :)

Glad I could help!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

739 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