Solved

Adding a header to an array in powershell

Posted on 2011-09-15
6
4,201 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now