Solved

Trying to group data in a report

Posted on 2014-03-01
3
245 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

691 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