![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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 string array as a parameter from one script to another Dear Chums, I have a script, scrpt1.ps1, in which I create a string array (string[]) named straryA. I wish to pass this string array to a second script, scrpt2.ps1, as an input parameter. The trial attempt was as follows: scrpt1.ps1: invoke-expression ".\scrpt2.ps1 $straryA $par2" scrpt2.ps1: param($straryA2, $par2loc="", $errCatch="") This fails: by inspection, it is clear that $straryA2 holds the first string of $straryA, $par2loc holds the second string of $straryA and $errCatch holds the third string of $straryA. Any help with the correct syntax for passing a string array woulfd be appreciated. -- Regards, Brillig. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Passing a string array as a parameter from one script to another Hi These are my files: test2.ps1 --------- param ($p1, $p2, $p3, $p4, $p5) write-host "p1 = $p1" write-host "p2 = $p2" write-host "p3 = $p3" write-host "p4 = $p4" write-host "p5 = $p5" test1.ps1 --------- $a = "a","b","c" $b = "second" ..\test2.ps1 $a[0] $a[1] $a[2] $b ....which produces this: PS U:\> .\test1 p1 = a p2 = b p3 = c p4 = second p5 = ....which is waht you want, but this test1.ps1 ------------------- $a = "a","b","c" $b = "second" ..\test2.ps1 $a $b produces this output PS U:\> .\test1 p1 = a b c p2 = second p3 = p4 = p5 = ....which is not what you want, so the answer is either to pass the array elements in individually because you can't pass all the array elements into another script by specifying the array's name, or to separate the components of the appropriate parameter in the second script. Hope this helps Jay "Brillig" <Brillig@xxxxxx> wrote in message news:ECAF53FC-9EDE-40B8-A3FA-61F11E05ADEE@xxxxxx Quote: > Dear Chums, > > I have a script, scrpt1.ps1, in which I create a string array (string[]) > named straryA. > I wish to pass this string array to a second script, scrpt2.ps1, as an > input > parameter. > > The trial attempt was as follows: > scrpt1.ps1: > invoke-expression ".\scrpt2.ps1 $straryA $par2" > > scrpt2.ps1: > param($straryA2, $par2loc="", $errCatch="") > > This fails: by inspection, it is clear that $straryA2 holds the first > string > of $straryA, $par2loc holds the second string of $straryA and $errCatch > holds the third string of $straryA. > > Any help with the correct syntax for passing a string array woulfd be > appreciated. > > -- > Regards, > Brillig. |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Passing a string array as a parameter from one script to another On Sep 26, 4:12 pm, Brillig <Bril...@xxxxxx> wrote: Quote: > Dear Chums, > > I have a script, scrpt1.ps1, in which I create a string array (string[]) > named straryA. > I wish to pass this string array to a second script, scrpt2.ps1, as an input > parameter. > > The trial attempt was as follows: > scrpt1.ps1: > invoke-expression ".\scrpt2.ps1 $straryA $par2" > > scrpt2.ps1: > param($straryA2, $par2loc="", $errCatch="") > > This fails: by inspection, it is clear that $straryA2 holds the first string > of $straryA, $par2loc holds the second string of $straryA and $errCatch > holds the third string of $straryA. > > Any help with the correct syntax for passing a string array woulfd be > appreciated. > > -- > Regards, > Brillig. directly, without using Invoke-Expression: ..\scrpt2.ps1 $straryA $par2 Is there a reason you are using Invoke-Expression? You will also get what you want if you use single quotes instead of double quotes. Invoke-Expression '.\scrpt2.ps1 $straryA $par2' Remember, variables within double quotes are replaced with their values before being passed on to a command; in your case, Invoke- Expression. Jeff |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Passing a string array as a parameter from one script to another You can also pass the array and use multiple assignments in scrpt2.ps1: # scrpt1.ps1 # [string[]]$straryA = $args[0..2] $straryA # scrpt1.ps1 # # scrpt2.ps1 # param([string[]]$straryA2) $straryA2, $par2loc, $errCatch = $straryA2 "`$straryA2 = $straryA2" "`$par2loc = $par2loc" "`$errCatch = $errCatch" # scrpt2.ps1 # Quote: >./scrpt2 (./scrpt1 one two three) $par2loc = two $errCatch = three # ...or: Quote: >$straryB = ./scrpt1 one two three >./scrpt2 $straryB $par2loc = two $errCatch = three -- Kiron |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Passing a string array as a parameter from one script to another On Sep 26, 5:12 am, Brillig <Bril...@xxxxxx> wrote: Quote: > Dear Chums, > > I have a script, scrpt1.ps1, in which I create a string array (string[]) > named straryA. > I wish to pass this string array to a second script, scrpt2.ps1, as an input > parameter. > > The trial attempt was as follows: > scrpt1.ps1: > invoke-expression ".\scrpt2.ps1 $straryA $par2" > > scrpt2.ps1: > param($straryA2, $par2loc="", $errCatch="") > > This fails: by inspection, it is clear that $straryA2 holds the first string > of $straryA, $par2loc holds the second string of $straryA and $errCatch > holds the third string of $straryA. > > Any help with the correct syntax for passing a string array woulfd be > appreciated. > > -- > Regards, > Brillig. single quotes, or escape the dollar signs: -- begin s1.ps1 -- $arr = @(1,2,3) $p = "test param" invoke-expression ".\s2.ps1 `$arr `$p" # escaped dollar # invoke-expression '.\s2.ps1 $arr $p' # single quotes -- end s1.ps1 ....and the receiving script: -- begin s2.ps1 -- param([array]$arr, [string]$p) "`$arr is a {0} with length {1}" -f $arr.gettype(), $arr.length "`$p is a {0} with length {1} and value '{2}'" -f $p.gettype(), $p.length, $p -- end s2.ps1 Results: PS> .\s1.ps1 $arr is a System.Object[] with length 3 $p is a System.String with length 10 and value 'test param' Hope this helps, - Oisin |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Passing a string array as a parameter from one script to anoth Dear Jeff, Having a "Duh" moment. Thanks a lot to you and the other correspondents for you valuable help. -- Regards, Brillig. "Jeff" wrote: Quote: > On Sep 26, 4:12 pm, Brillig <Bril...@xxxxxx> wrote: Quote: > > Dear Chums, > > > > I have a script, scrpt1.ps1, in which I create a string array (string[]) > > named straryA. > > I wish to pass this string array to a second script, scrpt2.ps1, as an input > > parameter. > > > > The trial attempt was as follows: > > scrpt1.ps1: > > invoke-expression ".\scrpt2.ps1 $straryA $par2" > > > > scrpt2.ps1: > > param($straryA2, $par2loc="", $errCatch="") > > > > This fails: by inspection, it is clear that $straryA2 holds the first string > > of $straryA, $par2loc holds the second string of $straryA and $errCatch > > holds the third string of $straryA. > > > > Any help with the correct syntax for passing a string array woulfd be > > appreciated. > > > > -- > > Regards, > > Brillig. > It looks to me like you get what you want if you call the script > directly, without using Invoke-Expression: > > ..\scrpt2.ps1 $straryA $par2 > > Is there a reason you are using Invoke-Expression? > > You will also get what you want if you use single quotes instead of > double quotes. > > Invoke-Expression '.\scrpt2.ps1 $straryA $par2' > > Remember, variables within double quotes are replaced with their > values before being passed on to a command; in your case, Invoke- > Expression. > > Jeff > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Passing filename as parameter | PowerShell | |||
| Passing a HashTable object as a parameter to a script | PowerShell | |||
| Function Parameter passing wrong or am I ? | PowerShell | |||
| Problem with parameter passing? | PowerShell | |||