Solved

Adding a header to an array in powershell

Posted on 2011-09-15
6
4,744 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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