Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding a header to an array in powershell

Posted on 2011-09-15
6
Medium Priority
?
6,630 Views
Last Modified: 2012-05-12
Hi,

I have a set of veriables I want to sort, but I need to sort it by a heading;

The headings should be the first two "Name, Size" but the sort is butting name,size in the middle of the sort and not sorting by the size colum.

How do I fix it?
$array = "Name Size","DB01size,$DB01size","DB02size,$DB02size","DB03size,$DB03size","DB04size,$DB04size","DB05size,$DB05size","DB06size,$DB06size","DB07size,$DB07size","DB08size,$DB08size","DB09size,$DB09size","DB10size,$DB10size","DB11size,$DB11size","DB12size,$DB12size" |Sort-Object Size

Open in new window

0
Comment
Question by:PeterSinger
[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
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:KenMcF
ID: 36546804
Hi Peter

Try to keep all your database names and database size an object. I am not sure what the rest of your code looks likes but here is an example of something that might work for you.


$DataBaseInfo = @()

#GetSize of each DB
Foreach ($DB in $DATBASES){
	$DBInfo = new-object psobject
	$DBInfo | Add-Member -membertype noteproperty -name "DBName" -Value $DB.Name
	$DBInfo | Add-Member -membertype noteproperty -name "Size" -Value $$DB.Size
	$DataBaseInfo += $DBInfo
	
}

$DataBaseInfo | Sort Size

Open in new window

0
 
LVL 4

Author Comment

by:PeterSinger
ID: 36546923
Each varable in the lline has a different number it it. I want to sort thoes numbers, will this do it?
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36546953
I cant say for sure without seeing the rest of your code. But in the example I posted it would get the name and size from each database and add to the array. Then you can sort by name or size.
0
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 
LVL 4

Author Comment

by:PeterSinger
ID: 36547153
Databases are not in the array.  The name $DB01size, $DB02size is just a line like this "352.9 GB (378,907,197,440 bytes)"
0
 
LVL 27

Accepted Solution

by:
KenMcF earned 2000 total points
ID: 36548434
But it looks like you want it to have a property NAME, like below "DB01SIZE"

"Name Size","DB01size,$DB01size"

So right now your array is

Name, Size
DB01size, #######

and when you sort is will sort each line not the properties. So if you create an object and keep adding that object to an array you can have a NAME and SIZE property

For example

With what you have if you would type
$array | Select Name
you would not get anything because is it a string

but with my example

$array | Select Name
Name
---------
DB01size
DB02size
DN03size

1
 
LVL 4

Author Closing Comment

by:PeterSinger
ID: 36570071
Thank you
0

Featured Post

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

721 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