Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Passing a HashTable object as a parameter to a script

Closed Thread
 
Thread Tools Display Modes
Old 02-06-2007   #1 (permalink)
Brillig
Guest


 

Passing a HashTable object as a parameter to a script

Dear Chums.

I have one script that creates a hastable of paired strings.

I have tried to pass it as a parameter to a second script, but have recieved
errors like:
Cannot convert value "dirScripts" to type "System.Int32". Error: "Input
string was not in a correct format."

Has anyone had any success with passing hashtables form one script to another?

--
Regards,
Brillig.
Old 02-06-2007   #2 (permalink)
RichS
Guest


 

RE: Passing a HashTable object as a parameter to a script

Can you post the relevant parts of your scripts - how you are passing the
hashtable & how you are defining the incoming arguments
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Brillig" wrote:

> Dear Chums.
>
> I have one script that creates a hastable of paired strings.
>
> I have tried to pass it as a parameter to a second script, but have recieved
> errors like:
> Cannot convert value "dirScripts" to type "System.Int32". Error: "Input
> string was not in a correct format."
>
> Has anyone had any success with passing hashtables form one script to another?
>
> --
> Regards,
> Brillig.

Old 02-06-2007   #3 (permalink)
Brillig
Guest


 

RE: Passing a HashTable object as a parameter to a script

Dear RichS,

Here are two simple scripts: SetUpHT.ps1 creates a HashTable and calls
CalledWithHT.ps1, suplying a string and the HashTable as parameters:
===============

#
# SetUpHT.ps1
#


# Set up hashtable
$ht1=@{"k1"="v1"; "k2"="v2"; "k3"="v3";}

#Display hashtable
$ht1.keys
$ht1.values

# Call other script using hashtable as parameter
$strMsg = '"Hello from SetUpHT.ps1"'
invoke-expression ".\CalledWithHT.ps1 $strMsg $ht1"

================


================

#
# CalledWithHT.ps1
#


# Deal with input parameter of a hashtable
# FAILS TOO: param($str1="", $ht=@{})
param($str1="", $ht)


#Display hashtable
Write-Host Hi from CalledWithHT.ps1...
$str1
$ht.keys
Write-Host Where any keys displayed?...
================

The output was:
================
PS C:\ScriptsPS\Utilities> .\setupht.ps1
k1
k3
k2
v1
v3
v2
Hi from CalledWithHT.ps1...
Hello from SetUpHT.ps1
Where any keys displayed?...
================

No keys were displayed for the input parameter "$ht"

--
Regards,
Brillig.


"RichS" wrote:

> Can you post the relevant parts of your scripts - how you are passing the
> hashtable & how you are defining the incoming arguments
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Brillig" wrote:
>
> > Dear Chums.
> >
> > I have one script that creates a hastable of paired strings.
> >
> > I have tried to pass it as a parameter to a second script, but have recieved
> > errors like:
> > Cannot convert value "dirScripts" to type "System.Int32". Error: "Input
> > string was not in a correct format."
> >
> > Has anyone had any success with passing hashtables form one script to another?
> >
> > --
> > Regards,
> > Brillig.

Old 02-07-2007   #4 (permalink)
RichS
Guest


 

RE: Passing a HashTable object as a parameter to a script

Hi
Looking at the scripts the reason the hashtable is not being passed into the
second script is the way its being called from the first script using
invoke-expression.

If you change the line

invoke-expression ".\CalledWithHT.ps1 $strMsg $ht1"

to be just

..\CalledWithHT.ps1 $strMsg $ht1

it works as you want it to.

The problem appears to be the way $ht1 is substituted into the string you
are passing to invoke-expression. If you try this

PS> $x = ".\CalledWithHT.ps1 $strMsg $ht1"
PS> $x
..\CalledWithHT.ps1 "Hello from SetUpHT.ps1" System.Collections.Hashtable

You can see that $ht1 isn't substituted correctly. It may be possible to
build a string for invoke-expression with containing a hashtable but I'm not
sure how at the moment. The only thing that comes to mind is to pass through
the keys and values and recreate it but that is just too messy.

So short answer is if you want to pass a hashtable as an argument don't use
invoke-expression

--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Brillig" wrote:

> Dear RichS,
>
> Here are two simple scripts: SetUpHT.ps1 creates a HashTable and calls
> CalledWithHT.ps1, suplying a string and the HashTable as parameters:
> ===============
>
> #
> # SetUpHT.ps1
> #
>
>
> # Set up hashtable
> $ht1=@{"k1"="v1"; "k2"="v2"; "k3"="v3";}
>
> #Display hashtable
> $ht1.keys
> $ht1.values
>
> # Call other script using hashtable as parameter
> $strMsg = '"Hello from SetUpHT.ps1"'
> invoke-expression ".\CalledWithHT.ps1 $strMsg $ht1"
>
> ================
>
>
> ================
>
> #
> # CalledWithHT.ps1
> #
>
>
> # Deal with input parameter of a hashtable
> # FAILS TOO: param($str1="", $ht=@{})
> param($str1="", $ht)
>
>
> #Display hashtable
> Write-Host Hi from CalledWithHT.ps1...
> $str1
> $ht.keys
> Write-Host Where any keys displayed?...
> ================
>
> The output was:
> ================
> PS C:\ScriptsPS\Utilities> .\setupht.ps1
> k1
> k3
> k2
> v1
> v3
> v2
> Hi from CalledWithHT.ps1...
> Hello from SetUpHT.ps1
> Where any keys displayed?...
> ================
>
> No keys were displayed for the input parameter "$ht"
>
> --
> Regards,
> Brillig.
>
>
> "RichS" wrote:
>
> > Can you post the relevant parts of your scripts - how you are passing the
> > hashtable & how you are defining the incoming arguments
> > --
> > Richard Siddaway
> > Please note that all scripts are supplied "as is" and with no warranty
> > Blog: http://richardsiddaway.spaces.live.com/
> > PowerShell User Group: http://www.get-psuguk.org.uk
> >
> >
> > "Brillig" wrote:
> >
> > > Dear Chums.
> > >
> > > I have one script that creates a hastable of paired strings.
> > >
> > > I have tried to pass it as a parameter to a second script, but have recieved
> > > errors like:
> > > Cannot convert value "dirScripts" to type "System.Int32". Error: "Input
> > > string was not in a correct format."
> > >
> > > Has anyone had any success with passing hashtables form one script to another?
> > >
> > > --
> > > Regards,
> > > Brillig.

Old 02-07-2007   #5 (permalink)
Keith Hill
Guest


 

Re: Passing a HashTable object as a parameter to a script


"RichS" <RichS@discussions.microsoft.com> wrote in message
news:03CF54F5-E5D3-4158-8E66-619449382A90@microsoft.com...
> Hi
> Looking at the scripts the reason the hashtable is not being passed into
> the
> second script is the way its being called from the first script using
> invoke-expression.
>
> If you change the line
>
> invoke-expression ".\CalledWithHT.ps1 $strMsg $ht1"
>
> to be just
>
> .\CalledWithHT.ps1 $strMsg $ht1
>
> it works as you want it to.
>
> The problem appears to be the way $ht1 is substituted into the string you
> are passing to invoke-expression. If you try this
>
> PS> $x = ".\CalledWithHT.ps1 $strMsg $ht1"
> PS> $x
> .\CalledWithHT.ps1 "Hello from SetUpHT.ps1" System.Collections.Hashtable
>
> You can see that $ht1 isn't substituted correctly. It may be possible to
> build a string for invoke-expression with containing a hashtable but I'm
> not
> sure how at the moment. The only thing that comes to mind is to pass
> through
> the keys and values and recreate it but that is just too messy.


What I have found to work is to not use double (expanding quotes) around the
the hashtable variables. Use single quotes instead. If you pass in the
text to invoke-expression with the literal text $ht1 then when
invoke-expression runs it will try to find $ht1 in the current scope.

--
Keith

Old 02-07-2007   #6 (permalink)
RichS
Guest


 

Re: Passing a HashTable object as a parameter to a script

Odd - isn't that counter to what you would expect with double & single quotes?
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Keith Hill" wrote:

>
> "RichS" <RichS@discussions.microsoft.com> wrote in message
> news:03CF54F5-E5D3-4158-8E66-619449382A90@microsoft.com...
> > Hi
> > Looking at the scripts the reason the hashtable is not being passed into
> > the
> > second script is the way its being called from the first script using
> > invoke-expression.
> >
> > If you change the line
> >
> > invoke-expression ".\CalledWithHT.ps1 $strMsg $ht1"
> >
> > to be just
> >
> > .\CalledWithHT.ps1 $strMsg $ht1
> >
> > it works as you want it to.
> >
> > The problem appears to be the way $ht1 is substituted into the string you
> > are passing to invoke-expression. If you try this
> >
> > PS> $x = ".\CalledWithHT.ps1 $strMsg $ht1"
> > PS> $x
> > .\CalledWithHT.ps1 "Hello from SetUpHT.ps1" System.Collections.Hashtable
> >
> > You can see that $ht1 isn't substituted correctly. It may be possible to
> > build a string for invoke-expression with containing a hashtable but I'm
> > not
> > sure how at the moment. The only thing that comes to mind is to pass
> > through
> > the keys and values and recreate it but that is just too messy.

>
> What I have found to work is to not use double (expanding quotes) around the
> the hashtable variables. Use single quotes instead. If you pass in the
> text to invoke-expression with the literal text $ht1 then when
> invoke-expression runs it will try to find $ht1 in the current scope.
>
> --
> Keith
>

Old 02-07-2007   #7 (permalink)
Brillig
Guest


 

RE: Passing a HashTable object as a parameter to a script

Dear RichS,

That worked! Many thanks.
--
Regards,
Brillig.


"RichS" wrote:

> Hi
> Looking at the scripts the reason the hashtable is not being passed into the
> second script is the way its being called from the first script using
> invoke-expression.
>
> If you change the line
>
> invoke-expression ".\CalledWithHT.ps1 $strMsg $ht1"
>
> to be just
>
> .\CalledWithHT.ps1 $strMsg $ht1
>
> it works as you want it to.
>
> The problem appears to be the way $ht1 is substituted into the string you
> are passing to invoke-expression. If you try this
>
> PS> $x = ".\CalledWithHT.ps1 $strMsg $ht1"
> PS> $x
> .\CalledWithHT.ps1 "Hello from SetUpHT.ps1" System.Collections.Hashtable
>
> You can see that $ht1 isn't substituted correctly. It may be possible to
> build a string for invoke-expression with containing a hashtable but I'm not
> sure how at the moment. The only thing that comes to mind is to pass through
> the keys and values and recreate it but that is just too messy.
>
> So short answer is if you want to pass a hashtable as an argument don't use
> invoke-expression
>
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Brillig" wrote:
>
> > Dear RichS,
> >
> > Here are two simple scripts: SetUpHT.ps1 creates a HashTable and calls
> > CalledWithHT.ps1, suplying a string and the HashTable as parameters:
> > ===============
> >
> > #
> > # SetUpHT.ps1
> > #
> >
> >
> > # Set up hashtable
> > $ht1=@{"k1"="v1"; "k2"="v2"; "k3"="v3";}
> >
> > #Display hashtable
> > $ht1.keys
> > $ht1.values
> >
> > # Call other script using hashtable as parameter
> > $strMsg = '"Hello from SetUpHT.ps1"'
> > invoke-expression ".\CalledWithHT.ps1 $strMsg $ht1"
> >
> > ================
> >
> >
> > ================
> >
> > #
> > # CalledWithHT.ps1
> > #
> >
> >
> > # Deal with input parameter of a hashtable
> > # FAILS TOO: param($str1="", $ht=@{})
> > param($str1="", $ht)
> >
> >
> > #Display hashtable
> > Write-Host Hi from CalledWithHT.ps1...
> > $str1
> > $ht.keys
> > Write-Host Where any keys displayed?...
> > ================
> >
> > The output was:
> > ================
> > PS C:\ScriptsPS\Utilities> .\setupht.ps1
> > k1
> > k3
> > k2
> > v1
> > v3
> > v2
> > Hi from CalledWithHT.ps1...
> > Hello from SetUpHT.ps1
> > Where any keys displayed?...
> > ================
> >
> > No keys were displayed for the input parameter "$ht"
> >
> > --
> > Regards,
> > Brillig.
> >
> >
> > "RichS" wrote:
> >
> > > Can you post the relevant parts of your scripts - how you are passing the
> > > hashtable & how you are defining the incoming arguments
> > > --
> > > Richard Siddaway
> > > Please note that all scripts are supplied "as is" and with no warranty
> > > Blog: http://richardsiddaway.spaces.live.com/
> > > PowerShell User Group: http://www.get-psuguk.org.uk
> > >
> > >
> > > "Brillig" wrote:
> > >
> > > > Dear Chums.
> > > >
> > > > I have one script that creates a hastable of paired strings.
> > > >
> > > > I have tried to pass it as a parameter to a second script, but have recieved
> > > > errors like:
> > > > Cannot convert value "dirScripts" to type "System.Int32". Error: "Input
> > > > string was not in a correct format."
> > > >
> > > > Has anyone had any success with passing hashtables form one script to another?
> > > >
> > > > --
> > > > Regards,
> > > > Brillig.

Old 02-07-2007   #8 (permalink)
Keith Hill
Guest


 

Re: Passing a HashTable object as a parameter to a script

"RichS" <RichS@discussions.microsoft.com> wrote in message news:C9FD2024-642A-4C82-A3E5-3B989CD42DB5@microsoft.com...
> Odd - isn't that counter to what you would expect with double & single quotes?


If you are constructing a string to pass to Invoke-Expression then some variables can be expanded *before* passing the string to Invoke-Expression. In this case, double quotes are the way to cause the variables to expand. Unfortunately hashtables expand to System.Collections.Hashtable which most likely won't work as expected when the resulting command string is passed to Invoke-Expression e.g.:

3>PS>$ht = @{e={$_.PSIsContainer};Asc=0}
4>PS>$cmd = "gci | sort $ht"
5>PS>invoke-expression $cmd
.... <doesn't sort dirs first> ...
6>PS>$cmd
gci | sort System.Collections.Hashtable

That's why I suggest using single quotes around the hashtable variable. Alternatively you could just escape the $ on the variable name e.g.:

7>PS>$cmd = "gci | sort `$ht"
8>PS>invoke-expression $cmd
9>PS>$cmd
gci | sort $ht

Which sorts dirs first as specified by the hashtable.

--
Keith
Old 02-07-2007   #9 (permalink)
William Stacey [C# MVP]
Guest


 

Re: Passing a HashTable object as a parameter to a script

A script block is another way IIRC:

$cmd = {gci | sort $ht}
&$cmd

--
William Stacey [C# MVP]
PCR concurrency library: www.codeplex.com/pcr
PSH Scripts Project www.codeplex.com/psobject


"RichS" <RichS@discussions.microsoft.com> wrote in message
news:C9FD2024-642A-4C82-A3E5-3B989CD42DB5@microsoft.com...
| Odd - isn't that counter to what you would expect with double & single
quotes?
| --
| Richard Siddaway
| Please note that all scripts are supplied "as is" and with no warranty
| Blog: http://richardsiddaway.spaces.live.com/
| PowerShell User Group: http://www.get-psuguk.org.uk
|
|
| "Keith Hill" wrote:
|
| >
| > "RichS" <RichS@discussions.microsoft.com> wrote in message
| > news:03CF54F5-E5D3-4158-8E66-619449382A90@microsoft.com...
| > > Hi
| > > Looking at the scripts the reason the hashtable is not being passed
into
| > > the
| > > second script is the way its being called from the first script using
| > > invoke-expression.
| > >
| > > If you change the line
| > >
| > > invoke-expression ".\CalledWithHT.ps1 $strMsg $ht1"
| > >
| > > to be just
| > >
| > > .\CalledWithHT.ps1 $strMsg $ht1
| > >
| > > it works as you want it to.
| > >
| > > The problem appears to be the way $ht1 is substituted into the string
you
| > > are passing to invoke-expression. If you try this
| > >
| > > PS> $x = ".\CalledWithHT.ps1 $strMsg $ht1"
| > > PS> $x
| > > .\CalledWithHT.ps1 "Hello from SetUpHT.ps1"
System.Collections.Hashtable
| > >
| > > You can see that $ht1 isn't substituted correctly. It may be possible
to
| > > build a string for invoke-expression with containing a hashtable but
I'm
| > > not
| > > sure how at the moment. The only thing that comes to mind is to pass
| > > through
| > > the keys and values and recreate it but that is just too messy.
| >
| > What I have found to work is to not use double (expanding quotes) around
the
| > the hashtable variables. Use single quotes instead. If you pass in the
| > text to invoke-expression with the literal text $ht1 then when
| > invoke-expression runs it will try to find $ht1 in the current scope.
| >
| > --
| > Keith
| >


Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing filename as parameter anth PowerShell 3 09-28-2007 10:24 PM
Passing a string array as a parameter from one script to another Brillig PowerShell 5 09-27-2007 04:16 AM
Passing script function as a callback into .NET object Ilya PowerShell 0 01-20-2007 03:34 PM
Problem with parameter passing? =?Utf-8?B?TWF0dA==?= PowerShell 3 10-06-2006 04:35 PM
Passing parameter from one Page to another Johann MacDonagh Avalon 3 04-10-2006 05:01 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50