Solved

Adding a header to an array in powershell

Posted on 2011-09-15
6
5,762 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 500 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

Technology Partners: 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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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