Using Hash Table as Parameter in Function

Hi all,

I'm currently building a PowerShell function. Whilst I've done a lot of scripting before, I've never really created any functions, so I'm a bit of a newbie.

I'd like to be able to pass a hashtable to a function. I've written something basic to explain what I'm trying to do below:

 

$hashtable1 = @{"Name1"="Value1";"Name2"="Value2"}

function hashfunction {
Param($hash)
foreach ($foo in $hash)
{
Write-Host $foo.value
}
}

hashfunction -hash $hashtable1

 

This doesn't return anything.

 

I've tried specifying hashtable like this...

Param([hashtable]$hash)

...but didn't have any luck.

 

Can anyone please point me in the right direction? I've got the rest of my script working brilliantly, I'm just trying to turn it into a usable function!

Thanks,

Gavin



January 29th, 2012 2:51pm

In case anyone is interested, I got this working using a multidimensional array, which is altogether a much more powerful solution:

 

$array1 = ("Name1","Value1"),("Name2","Value2")

function arrayfunction {
Param([array]$arrayvar)
foreach ($foo in $arrayvar)
{
Write-Host "Value is:" $foo[1]
Write-Host "Name is:" $foo[0]
}
}

arrayfunction -arrayvar $array1


 

 


 

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2012 3:40pm

Gavin - A hash is a collection of name/value pairs.  The names are keys and the values are associated with the key,  A hash is not a format object and it is not an array.  A hash is a collection of key/value pairs.

This is what you were trying to do. 

$hashtable1 = @{Name1="Value1";Name2="Value2"}

function hashfunction {
    Param($hash)
    foreach ($key in $hash.Keys){
         Write-Host $hash[$key]
     }
}

hashfunction -hash $hashtable1

When done correctly it works as you expected it to work.

We can also do this:
     $hashtable1.Name1
     $hashtable1.Name2

   ...which cannot be done with an array. An array can only be derefernced with numeric indexes.  Very inconvenient.

An MultiD array is not a true synthesis of a hash and cannot easily do most of what a hash can do.

 

January 29th, 2012 8:48pm

Gavin - A hash is a collection of name/value pairs.  The names are keys and the values are associated with the key,  A hash is not a format object and it is not an array.  A hash is a collection of key/value pairs.

This is what you were trying to do. 

$hashtable1 = @{Name1="Value1";Name2="Value2"}

function hashfunction {
    Param($hash)
    foreach ($key in $hash.Keys){
         Write-Host $hash[$key]
     }
}

hashfunction -hash $hashtable1

When done correctly it works as you expected it to work.

We can also do this:
     $hashtable1.Name1
     $hashtable1.Name2

   ...which cannot be done with an array. An array can only be derefernced with numeric indexes.  Very inconvenient.

An MultiD array is not a true synthesis of a hash and cannot easily do most of what a hash can do.

Free Windows Admin Tool Kit Click here and download it now
September 24th, 2012 2:17pm

When calling the function, you must use @hashtable1 and not $hashtable1. Using the variable name passes the variable as a HashTable object type instead of passing the keys and values.

Example: hashfunction -hash @hashtable1

The @ syntax is a shorthand technique for passing parameters to a function or script. If I understood the OPs question, he's not looking to pass a set of parameters to a function, but rather pass the hashtable itself as a parameter to the function.

Bill

September 24th, 2012 2:44pm

I'd recommend trying always to use a hashtable instead of an array.  Having a key/index pairing is so much easier to code and debug than a multidimensional array.  

Also, remember, you can easily return a hashtable containing multiply values back to the calling function.

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 10:07am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics