![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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 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. |
| | #2 (permalink) | ||||||||||||
| Guest | 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
| ||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: Passing a string array as a parameter from one script to another On Sep 26, 4:12 pm, Brillig <Bril...@xxxxxx> wrote:
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 | ||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||
| Guest | 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 #
$par2loc = two $errCatch = three # ...or:
$par2loc = two $errCatch = three -- Kiron | ||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||
| Guest | Re: Passing a string array as a parameter from one script to another On Sep 26, 5:12 am, Brillig <Bril...@xxxxxx> wrote:
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 | ||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||
| Guest | 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:
| ||||||||||||||||||||||||
| |
| |
![]() |
| 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 HashTable object as a parameter to a script | Brillig | PowerShell | 8 | 02-07-2007 04:45 PM |
| Function Parameter passing wrong or am I ? | joergH | PowerShell | 4 | 01-23-2007 05:16 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 |