Thursday, August 13, 2009

Easy database for Powershell

I run into situation with my current project where I need to have easy to use translation table. Nothing too complicated – just be able to search for PackageID (we are talking about SCCM) and retrieve location of that package.

Ideal candidate for such tasks is HashTable or dictionary – each entry in hashtable contains Key and Value. Key is unique identifier of that entry and allows you to easily retrieve value.

Initialization of hashtable is very simple:
[HashTable]$People = @{}

Also adding members is easy, either powershell way:
$People += “Martin” = “Zugec”
or using .NET approach:
$People.Add(“Martin”, “Zugec”)

In order to retrieve surname, easiest method is to used built-in functionality of Posh – it makes it very easy for you:
$People.Martin

Returned value is Zugec.

Ok, so far pretty easy. However I realized that for my project I don’t need only surname (package location), but also company. As I said before – hashtable is collection of key\value pairs and key must be unique. Well, value doesn’t need to be single value ;)

$People += @{“Martin Zugec” = @{“Name” = “Martin”; “Surname” = “Zugec”; “Company” = “Login Consultants”}}

In this case, I decided to use hashtable as value of hashtable. I really like the way how complicated stuff can be done easily using Powershell :)

To retrieve company, I can use $People.”Martin Zugec”.Company

Pretty useful sometimes

2 comments:

Anonymous said...

I was having a problem returning hashtable so looked on internet and found your blog.

I could gain information regarding hashtables and moreover I liked your profile
you know "Real Men don't click"

Unknown said...

To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment. See the link below for more info.


#trying
www.ufgop.org