?
Solved

Trying to group data in a report

Posted on 2014-03-01
3
Medium Priority
?
248 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
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 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

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

840 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