![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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. |
My System Specs![]() |
| | #2 (permalink) |
| | 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. |
My System Specs![]() |
| | #3 (permalink) |
| | 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. |
My System Specs![]() |
| | #4 (permalink) |
| | 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. |
My System Specs![]() |
| | #5 (permalink) |
| | 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 |
My System Specs![]() |
| | #6 (permalink) |
| | 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 > |
My System Specs![]() |
| | #7 (permalink) |
| | 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. |
My System Specs![]() |
| | #8 (permalink) |
| | 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 |
My System Specs![]() |
| | #9 (permalink) |
| | 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 | > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| using foreach-object as a parameter for another script | PowerShell | |||
| Passing a string array as a parameter from one script to another | PowerShell | |||
| Passing script function as a callback into .NET object | PowerShell | |||
| Problem with parameter passing? | PowerShell | |||