Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7330
  • Last Modified:

Adding a header to an array in powershell

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
PeterSinger
Asked:
PeterSinger
  • 3
  • 3
1 Solution
 
KenMcFCommented:
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
 
PeterSingerAuthor Commented:
Each varable in the lline has a different number it it. I want to sort thoes numbers, will this do it?
0
 
KenMcFCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
PeterSingerAuthor Commented:
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
 
KenMcFCommented:
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
 
PeterSingerAuthor Commented:
Thank you
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now