![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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. |
| | #2 (permalink) |
| 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. |
| | #3 (permalink) |
| 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. |
| | #4 (permalink) |
| 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. |
| | #5 (permalink) |
| 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 |
| | #6 (permalink) |
| 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 > |
| | #7 (permalink) |
| 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. |
| | #8 (permalink) |
| 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 |
| | #9 (permalink) |
| 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 | > |
| |
| |
![]() |
| 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 |