Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.


Go Back   Vista Forums > Misc Newsgroups > PowerShell

RB

Vista - Passing a string array as a parameter from one script to another

Reply
 
09-26-2007   #1
Brillig


 

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 SpecsSystem Spec
09-26-2007   #2
Jay


 

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 SpecsSystem Spec
09-26-2007   #3
Jeff


 

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

My System SpecsSystem Spec
09-26-2007   #4
Kiron


 

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

My System SpecsSystem Spec
09-26-2007   #5
Oisin Grehan


 

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



My System SpecsSystem Spec
09-27-2007   #6
Brillig


 

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 SpecsSystem Spec
Reply

RB


Thread Tools


Similar Threads for: Passing a string array as a parameter from one script to another
Thread Forum
Invoke-Expression parameter Passing PowerShell
Passing filename as parameter PowerShell
Passing a HashTable object as a parameter to a script PowerShell
Problem with parameter passing? PowerShell


Vista Forums 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 Ltd