/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" PowerShell : Setting SendAs permission in Exchange... "," PowerShell : Active Directory Part 10 (AD Browser)... "," PowerShell "," PowerShell : How Do I randomize a list, and remove... "," PowerShell : Can you do that less cryptic ? "," PowerShell : How can I tell whitch numbers are mis... "," PowerShell Active Directory Browser teaser "," Powershell, Has my Dell a dangerous battery Part 2... "," PowerShell, Has my Dell a dangerous battery ? "," PowerShell Adding -verbose switch to functions "," ")

Thursday, September 07, 2006

 


PowerShell : Learn about the HashTable Object and other Collections



In the TechNet ScriptCenter there is a series : Sesame Script  If you’re new to scripting, these articles will teach you the basics and help get you started.

(Check out also the  Sesame Script Archives , although the Series is aimed at beginners also some more advanced topics get explained, an all articles are a good read.), also the concepts are often also usable in PowerShell so even they are in VbScript they still can be a good resource for learning PowerShell) 

In this part of the series : Sesame Script: The Dictionary Object , a Dictionary Object is discussed

In PowerShell we have a similar object called a HashTable that works almost the same and other .NET Collection Classes we can use that work like this, so this concepts are also good to know mastering PowerShell, the WMI example might not be the best example as it is an collection of objects already, but as an ArrayList is used also in for example a Select statement for more info also PowerShell and Active Directory Part 5 so is good to understand, and if you did not get the use in the Select completely its also good to get this background.

the HashTable unlike many other things do not enumerate on the pipeline. this can be a catch Wmi-Help Part 1

In this post I will follow the Sesame Script Article with similar command in PowerShell and point out the PowerShell Specifics, be sure to read the part in the "Sesame Script" article completely first.

 

An Example that Doesn’t Use the HashTable Object

 

PoSH>gwmi win32_service | fl name,state

name : Alerter
state : Stopped

name : Eventlog
state : Running

The next example about Filtering

 

PoSH>gwmi win32_service -filter "state = 'Stopped'" | ft name,state

name state
---- -----
Alerter Stopped
ALG Stopped

An Example that Does Use the Dictionary Object

 

PoSH>gwmi win32_service |% {$ht = @{}}{$ht.add($_.name,$_.state)}
PoSH>$ht

Name Value
---- -----
Eventlog Running
Alerter Stopped

First note we can create a HashTable like this in PowerShell

 

PoSH>@{} | gm

TypeName: System.Collections.Hashtable

Name MemberType Definition
---- ---------- ----------
Add Method System.Void Add(Object key, Object value)
Clear Method System.Void Clear()

PoSH>@{name = 'mow'}

Name Value
---- -----
name mow

 

PoSH>@{'name' = 'mow'; 'shell' = 'PoSH'}

Name Value
---- -----
name mow
shell PoSH

Listing the Names of Stopped Services

 

PoSH>$ht.keys |? {$ht."$_" -eq 'Stopped'}
Alerter
ALG

Running :

 

PoSH>$ht.keys |? {$ht.Item($_) -eq 'Running'}
Eventlog
W32Time

Other Things You Should Know About the Dictionary Object

 

PoSH>$ht.Contains('Alerter')
True
PoSH>$ht.Contains('foo')
False

PoSH>$ht.ContainsValue('Running')
True
PoSH>$ht.ContainsValue('Alerter')
False

For the rest see the Get-Member output

you can get to the DictionaryEntries it contains by calling the GetEnumerator yourself :


PoSH>$ht.GetEnumerator()

Name Value
---- -----
Eventlog Running
Alerter Stopped

 

PoSH>$ht.GetEnumerator() | gm

TypeName: System.Collections.DictionaryEntry

Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = Key
Equals Method System.Boolean Equals(Object obj)
get_Key Method System.Object get_Key()
get_Value Method System.Object get_Value()
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
set_Key Method System.Void set_Key(Object value)
set_Value Method System.Void set_Value(Object value)
ToString Method System.String ToString()
Key Property System.Object Key {get;set;}
Value Property System.Object Value {get;set;}

You can also see more advanced examples of working with the HashTable Object in an MSH post (nothing changed here) here :

: Get AD info into a nested HashTable from MSH , (also a good read if you follow the AD series, as this also contains info about how to construct the Query for a Search of Active DIrectory using a directory Searcher)

Also use get-Member well  ( Strange behavour of get-member on DataTables  ), and Check out the Other collection members :

Other Usefull Collections

 

PoSH>$h = new Collections.Hashtable
PoSH>$h | gm

TypeName: System.Collections.Hashtable

PoSH>[System.Collections.ArrayList](1,2)
1
2

PoSH>,[System.Collections.ArrayList](1,2) | gm

TypeName: System.Collections.ArrayList

PoSH>$s = [System.Collections.Stack]('a','b')
PoSH>$s.count
2
PoSH>$s.pop()
b
PoSH>$s.count
1
PoSH>,$s | gm

TypeName: System.Collections.Stack

 

PoSH>$sl = new System.Collections.SortedList
PoSH>Get-Member -InputObject $sl

TypeName: System.Collections.SortedList

 

PoSH>$q = [System.Collections.Queue]('a','b')

PoSH>$q.Dequeue()
a
PoSH>,$q | gm

TypeName: System.Collections.Queue

 You see that this basic collection logic and get-member, can get a whole lot of possibilities next to a simple array to store Objects.

and you can use get-member-inputObject to list the methods of the Collection.

 

Greetings, /\/\o\/\/

Tags : Monad msh PowerShell




Comments: Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?