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 string array as a parameter from one script to another

Closed Thread
 
Thread Tools Display Modes
Old 09-26-2007   #1 (permalink)
Brillig
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.
Old 09-26-2007   #2 (permalink)
Jay
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
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.

Old 09-26-2007   #3 (permalink)
Jeff
Guest


 

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.
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

Old 09-26-2007   #4 (permalink)
Kiron
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 #
Quote:

>./scrpt2 (./scrpt1 one two three)
$straryA2 = one
$par2loc = two
$errCatch = three

# ...or:
Quote:

>$straryB = ./scrpt1 one two three
>./scrpt2 $straryB
$straryA2 = one
$par2loc = two
$errCatch = three

--
Kiron

Old 09-26-2007   #5 (permalink)
Oisin Grehan
Guest


 

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.
The variables are being expanded inside the double quoted string; use
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



Old 09-27-2007   #6 (permalink)
Brillig
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:
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
>
>
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 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








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