Making a Hash out of a switch

Well that post title might get some interesting hits 🙂

I mentioned this in an earlier post where using a hash table can be better that using a switch statement. The advantage is that you don’t actually do a lookup by executing a switch statement repeatedly in each iteration of a loop instead you just index into the hash table using the key.  there are less lines of code and the end result is code that is intuitive to read.

The first example  I came across when I looked through my scripts also has a nice little trick of storing whole objects in the hash table that can be reused.

# the next 2 variables are used as indexes into an array and makes the code look cleaner and more easy to remember.
$domain       = 1
$connection = 0

# create the hash tables that will replace a switch statement – store the NETBIOS name as the key and
# the connection object as the value

$connSetting = @{}
$connSetting.add(“domain1″,$(@(Connect-QADService”domain1.ad.com”),”domain1″))
$connSetting.add(“domain2″,$(@(Connect-QADService”domain2.ad.com”),”domain2″))
$connSetting.add(“domain3″,$(@(Connect-QADService”domain3.ad.com”),”domain3″))

# You can then use this in a cmdline like this
$connsetting.Item(“domain1”)[$Domain]      # returns the NETBIOS domain name
$connsetting.Item(“domain1”)[$connection] # returns the connection object which can be used in a cmdline

# So in the main body of the script you might have a loop that determines from one object which domain is needed and then use
# this cmd line – $domainName was set to the NETBIOS domain name by another lookup and we reference the connection object in
# the array using $connection = 1 

-connection $($connsetting.item($domainName)[$connection]

I just revisited this post as I decided to combine this tip with my post on getting admin credentials and corrected a typo above as I’d not correctly shown the array that was being put into the hash table.  If you just add the connection objects to the hash table rather than an array of the connection and the NETBIOS domain name then you can directly reference the connection object using

$connSetting.add(“domain1″,$(Connect-QADService”domain1.ad.com”))
$connSetting.add(“domain2″,$(Connect-QADService”domain1.ad.com”))
$connSetting.add(“domain3″,$(Connect-QADService”domain1.ad.com”))

-connection $connsetting.$domainName

I can’t think for the life of me now why I added an array into the hash table so I could get back the NETBIOS name of the domain when this was the hash table key 🙂 perhaps I just did this as an example but I’m pretty sure I actually used this in one of my scripts so I’ll have to dig through them when I have an 8th day in the week to see how I made use of that.

Advertisements

One thought on “Making a Hash out of a switch

  1. […] I’ve now modified my get-AdminCredentials function and combined it with my post on using a hash table to store the domain connection objects. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.