Windows Vista Forums

Using param with InvokeReturnAsIs
  1. #1


    cashfoley Guest

    Using param with InvokeReturnAsIs

    I can't seem to use param when invoking a scriptblock using
    InvokeReturnAsIs. Also, the same problem occurs when using a
    PSScriptMethod. Here are some examples of what works and does not
    work.



    First the Easy:
    &{param($x,$Y) Write-Host "v1 X: $x Y:$y"; $x+$y} 5 6

    v1 X: 5 Y:6
    11

    This demonstrates params come in as Args:
    $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host
    "v2 X: $x Y:$y"; $x+$y}
    &$script2 9 10

    v2 X: 9 Y:10
    19

    $script2.InvokeReturnAsIs(11,12)

    v2 X: 11 Y:12
    23

    Now with a param statement:
    $script3 = [scriptBlock] { param($x,$y); Write-Host "v3 X: $x Y:$y"; $x
    +$y}
    &$script3 13 14

    v3 X: 13 Y:14
    27

    $script3.InvokeReturnAsIs(15,16)

    v3 X: Y:


    All works as expected except for the last call.

    Is there any fix or work around?

      My System SpecsSystem Spec

  2. #2


    Shay Levi Guest

    Re: Using param with InvokeReturnAsIs

    Works fine for me :

    PS > $script3 = [scriptBlock] { param($x,$y); Write-Host "v3 X: $x Y:$y";
    $x+$y}
    PS > $script3.InvokeReturnAsIs(15,16)
    v3 X: 15 Y:16
    31

    PS > $script3.Invoke(15,16)
    v3 X: 15 Y:16
    31



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > I can't seem to use param when invoking a scriptblock using
    > InvokeReturnAsIs. Also, the same problem occurs when using a
    > PSScriptMethod. Here are some examples of what works and does not
    > work.
    >
    > First the Easy:
    > &{param($x,$Y) Write-Host "v1 X: $x Y:$y"; $x+$y} 5 6
    > v1 X: 5 Y:6
    > 11
    > This demonstrates params come in as Args:
    > $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host
    > "v2 X: $x Y:$y"; $x+$y}
    > &$script2 9 10
    > v2 X: 9 Y:10
    > 19
    > $script2.InvokeReturnAsIs(11,12)
    >
    > v2 X: 11 Y:12
    > 23
    > Now with a param statement:
    > $script3 = [scriptBlock] { param($x,$y); Write-Host "v3 X: $x Y:$y";
    > $x
    > +$y}
    > &$script3 13 14
    > v3 X: 13 Y:14
    > 27
    > $script3.InvokeReturnAsIs(15,16)
    >
    > v3 X: Y:
    >
    > All works as expected except for the last call.
    >
    > Is there any fix or work around?
    >


      My System SpecsSystem Spec

  3. #3


    Shay Levi Guest

    Re: Using param with InvokeReturnAsIs

    BTW, why not writing it as a function?

    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > Works fine for me :
    >
    PS>> $script3 = [scriptBlock] { param($x,$y); Write-Host "v3 X: $x
    PS>> Y:$y";
    PS>>

    > $x+$y}
    >
    PS>> $script3.InvokeReturnAsIs(15,16)
    PS>>

    > v3 X: 15 Y:16
    > 31
    PS>> $script3.Invoke(15,16)
    PS>>

    > v3 X: 15 Y:16
    > 31
    > -----
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com

    >> I can't seem to use param when invoking a scriptblock using
    >> InvokeReturnAsIs. Also, the same problem occurs when using a
    >> PSScriptMethod. Here are some examples of what works and does not
    >> work.
    >>
    >> First the Easy:
    >> &{param($x,$Y) Write-Host "v1 X: $x Y:$y"; $x+$y} 5 6
    >> v1 X: 5 Y:6
    >> 11
    >> This demonstrates params come in as Args:
    >> $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host
    >> "v2 X: $x Y:$y"; $x+$y}
    >> &$script2 9 10
    >> v2 X: 9 Y:10
    >> 19
    >> $script2.InvokeReturnAsIs(11,12)
    >> v2 X: 11 Y:12
    >> 23
    >> Now with a param statement:
    >> $script3 = [scriptBlock] { param($x,$y); Write-Host "v3 X: $x Y:$y";
    >> $x
    >> +$y}
    >> &$script3 13 14
    >> v3 X: 13 Y:14
    >> 27
    >> $script3.InvokeReturnAsIs(15,16)
    >> v3 X: Y:
    >>
    >> All works as expected except for the last call.
    >>
    >> Is there any fix or work around?
    >>


      My System SpecsSystem Spec

  4. #4


    cashfoley Guest

    Re: Using param with InvokeReturnAsIs

    On Jan 18, 8:55*am, Shay Levi <n...@xxxxxx> wrote:

    > Works fine for me :
    >
    > PS > $script3 = [scriptBlock] { param($x,$y); Write-Host "v3 X: $x Y:$y";
    > $x+$y}
    > PS > $script3.InvokeReturnAsIs(15,16)
    > v3 X: 15 Y:16
    > 31
    >
    > PS > $script3.Invoke(15,16)
    > v3 X: 15 Y:16
    > 31
    >
    > -----
    > Shay Levi
    > $cript Fanatichttp://scriptolog.blogspot.com
    >
    This is a little disturbing. What OS are you running on? I'm on
    Windows 2003 Server.

    As to why I'm needing to invoke a script, it's not worth digging into
    the problem set. But as I said I get the same problem with
    PSScriptMethod
    How does this work for you?

    $object = new-object Management.Automation.PSObject

    $script1 = [scriptBlock] { param($x,$y); Write-Host "Method1 X: $x Y:
    $y"; $x+$y}
    $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host
    "Method2 X: $x Y:$y"; $x+$y}

    $member1 = new-object management.automation.PSScriptMethod "Method1",
    $script1
    $object.psobject.members.Add($member1)

    $member2 = new-object management.automation.PSScriptMethod "Method2",
    $script2
    $object.psobject.members.Add($member2)

    $object.Method1(5,6)
    Method1 X: Y:

    $object.Method2(7,8)

    Method2 X: 7 Y:8
    15




      My System SpecsSystem Spec

  5. #5


    Shay Levi Guest

    Re: Using param with InvokeReturnAsIs

    I'm runing PowerShell CTP on WinXP SP2. I checked
    the commands on a W3K SP1 box and it works.

    As for the below, you need to *capture* the new object you created, assign
    it to a new variable.


    $object = new-object Management.Automation.PSObject
    $script1 = [scriptBlock] { param($x,$y); Write-Host "Method1 X: $x Y: $y";
    $x+$y}
    $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host "Method2
    X: $x Y:$y"; $x+$y}

    $member1 = new-object management.automation.PSScriptMethod "Method1",$script1
    $object.psobject.members.Add($member1)

    $member2 = new-object management.automation.PSScriptMethod "Method2",$script2
    $object.psobject.members.Add($member2)

    $new = $object
    $new.Method1(5,6)
    Method1 X: 5 Y: 6
    11

    $new.Method2(7,8)
    Method2 X: 7 Y:8
    15



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > On Jan 18, 8:55 am, Shay Levi <n...@xxxxxx> wrote:
    >

    >> Works fine for me :
    >>
    >> PS > $script3 = [scriptBlock] { param($x,$y); Write-Host "v3 X: $x
    >> Y:$y";
    >> $x+$y}
    >> PS > $script3.InvokeReturnAsIs(15,16)
    >> v3 X: 15 Y:16
    >> 31
    >> PS > $script3.Invoke(15,16)
    >> v3 X: 15 Y:16
    >> 31
    >> -----
    >> Shay Levi
    >> $cript Fanatichttp://scriptolog.blogspot.com
    > This is a little disturbing. What OS are you running on? I'm on
    > Windows 2003 Server.
    >
    > As to why I'm needing to invoke a script, it's not worth digging into
    > the problem set. But as I said I get the same problem with
    > PSScriptMethod
    > How does this work for you?
    > $object = new-object Management.Automation.PSObject
    >
    > $script1 = [scriptBlock] { param($x,$y); Write-Host "Method1 X: $x Y:
    > $y"; $x+$y}
    > $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host
    > "Method2 X: $x Y:$y"; $x+$y}
    > $member1 = new-object management.automation.PSScriptMethod "Method1",
    > $script1
    > $object.psobject.members.Add($member1)
    > $member2 = new-object management.automation.PSScriptMethod "Method2",
    > $script2
    > $object.psobject.members.Add($member2)
    > $object.Method1(5,6)
    > Method1 X: Y:
    > $object.Method2(7,8)
    >
    > Method2 X: 7 Y:8
    > 15


      My System SpecsSystem Spec

  6. #6


    Oisin Grehan [MVP] Guest

    Re: Using param with InvokeReturnAsIs

    On Jan 18, 11:21*am, Shay Levi <n...@xxxxxx> wrote:

    > I'm runing PowerShell CTP on WinXP SP2. I checked
    > the commands on a W3K SP1 box and it works.
    >
    > As for the below, you need to *capture* the new object you created, assign
    > it to a new variable.
    >
    > $object = new-object Management.Automation.PSObject
    > $script1 = [scriptBlock] { param($x,$y); Write-Host "Method1 X: $x Y: $y";
    > $x+$y}
    > $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host "Method2
    > X: $x Y:$y"; $x+$y}
    >
    > $member1 = new-object management.automation.PSScriptMethod "Method1",$script1
    > $object.psobject.members.Add($member1)
    >
    > $member2 = new-object management.automation.PSScriptMethod "Method2",$script2
    > $object.psobject.members.Add($member2)
    >
    > $new = $object
    > $new.Method1(5,6)
    > Method1 X: 5 Y: 6
    > 11
    >
    > $new.Method2(7,8)
    > Method2 X: 7 Y:8
    > 15
    >
    > -----
    > Shay Levi
    > $cript Fanatichttp://scriptolog.blogspot.com
    >
    >
    >

    > > On Jan 18, 8:55 am, Shay Levi <n...@xxxxxx> wrote:
    >

    > >> Works fine for me :
    >

    > >> PS > $script3 = [scriptBlock] { param($x,$y); Write-Host "v3 X: $x
    > >> Y:$y";
    > >> $x+$y}
    > >> PS > $script3.InvokeReturnAsIs(15,16)
    > >> v3 X: 15 Y:16
    > >> 31
    > >> PS > $script3.Invoke(15,16)
    > >> v3 X: 15 Y:16
    > >> 31
    > >> -----
    > >> Shay Levi
    > >> $cript Fanatichttp://scriptolog.blogspot.com
    > > This is a little disturbing. *What OS are you running on? *I'm on
    > > Windows 2003 Server.
    >

    > > As to why I'm needing to invoke a script, it's not worth digging into
    > > the problem set. *But as I said I get the same problem with
    > > PSScriptMethod
    > > How does this work for you?
    > > $object = new-object Management.Automation.PSObject
    >

    > > $script1 = [scriptBlock] { param($x,$y); Write-Host "Method1 X: $x Y:
    > > $y"; $x+$y}
    > > $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host
    > > "Method2 X: $x Y:$y"; $x+$y}
    > > $member1 = new-object management.automation.PSScriptMethod "Method1",
    > > $script1
    > > $object.psobject.members.Add($member1)
    > > $member2 = new-object management.automation.PSScriptMethod "Method2",
    > > $script2
    > > $object.psobject.members.Add($member2)
    > > $object.Method1(5,6)
    > > Method1 X: *Y:
    > > $object.Method2(7,8)
    >

    > > Method2 X: 7 Y:8
    > > 15- Hide quoted text -
    >
    > - Show quoted text -
    FYI Shay et al - using named parameters in anonymous functions
    (scriptblocks) is *broken* in powershell 1.0. As you can see, it`s
    fixed in in 2.0 CTP.

    - Oisin

      My System SpecsSystem Spec

  7. #7


    cashfoley Guest

    Re: Using param with InvokeReturnAsIs

    On Jan 18, 10:21*am, Shay Levi <n...@xxxxxx> wrote:

    > I'm runing PowerShell CTP on WinXP SP2. I checked
    > the commands on a W3K SP1 box and it works.
    >
    > As for the below, you need to *capture* the new object you created, assign
    > it to a new variable.
    >
    > $object = new-object Management.Automation.PSObject
    > $script1 = [scriptBlock] { param($x,$y); Write-Host "Method1 X: $x Y: $y";
    > $x+$y}
    > $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host "Method2
    > X: $x Y:$y"; $x+$y}
    >
    > $member1 = new-object management.automation.PSScriptMethod "Method1",$script1
    > $object.psobject.members.Add($member1)
    >
    > $member2 = new-object management.automation.PSScriptMethod "Method2",$script2
    > $object.psobject.members.Add($member2)
    >
    > $new = $object
    > $new.Method1(5,6)
    > Method1 X: 5 Y: 6
    > 11
    >
    > $new.Method2(7,8)
    > Method2 X: 7 Y:8
    > 15
    >
    > -----
    > Shay Levi
    > $cript Fanatichttp://scriptolog.blogspot.com
    >
    I executed

    $new = $object
    $new.Method1(5,6)

    and got:

    Method1 X: Y:

    There has to be some explanation. I'm fairly certain I've had the
    same behavior on more than one system but they are W3K

    Why would I need to "Capture" the object? What happens when you run
    the method on $object?


      My System SpecsSystem Spec

  8. #8


    Oisin Grehan [MVP] Guest

    Re: Using param with InvokeReturnAsIs

    On Jan 18, 11:57*am, cashfo...@xxxxxx wrote:

    > On Jan 18, 10:21*am, Shay Levi <n...@xxxxxx> wrote:
    >
    >
    >
    >
    >

    > > I'm runing PowerShell CTP on WinXP SP2. I checked
    > > the commands on a W3K SP1 box and it works.
    >

    > > As for the below, you need to *capture* the new object you created, assign
    > > it to a new variable.
    >

    > > $object = new-object Management.Automation.PSObject
    > > $script1 = [scriptBlock] { param($x,$y); Write-Host "Method1 X: $x Y: $y";
    > > $x+$y}
    > > $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host "Method2
    > > X: $x Y:$y"; $x+$y}
    >

    > > $member1 = new-object management.automation.PSScriptMethod "Method1",$script1
    > > $object.psobject.members.Add($member1)
    >

    > > $member2 = new-object management.automation.PSScriptMethod "Method2",$script2
    > > $object.psobject.members.Add($member2)
    >

    > > $new = $object
    > > $new.Method1(5,6)
    > > Method1 X: 5 Y: 6
    > > 11
    >

    > > $new.Method2(7,8)
    > > Method2 X: 7 Y:8
    > > 15
    >

    > > -----
    > > Shay Levi
    > > $cript Fanatichttp://scriptolog.blogspot.com
    >
    > I executed
    >
    > $new = $object
    > $new.Method1(5,6)
    >
    > and got:
    >
    > Method1 X: *Y:
    >
    > There has to be some explanation. *I'm fairly certain I've had the
    > same behavior on more than one system but they are W3K
    >
    > Why would I need to "Capture" the object? *What happens when you run
    > the method on $object?- Hide quoted text -
    >
    > - Show quoted text -
    Hey cash,

    You may have missed my other post - the explanation is simple. What
    you're trying to do won't work in powershell 1.0 - its broken - a bug
    - foobared. Shay is using a beta of powershell 2.0 where the problem
    has been fixed. You will have to use the $args collection in anonymous
    scriptblocks. Named parameters with "param" doesn`t work.

    Hope this helps,

    - Oisin / x0n

      My System SpecsSystem Spec

  9. #9


    Shay Levi Guest

    Re: Using param with InvokeReturnAsIs

    Thanks.

    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > On Jan 18, 11:57 am, cashfo...@xxxxxx wrote:
    >

    >> On Jan 18, 10:21 am, Shay Levi <n...@xxxxxx> wrote:
    >>

    >>> I'm runing PowerShell CTP on WinXP SP2. I checked
    >>> the commands on a W3K SP1 box and it works.
    >>> As for the below, you need to *capture* the new object you created,
    >>> assign it to a new variable.
    >>>
    >>> $object = new-object Management.Automation.PSObject
    >>> $script1 = [scriptBlock] { param($x,$y); Write-Host "Method1 X: $x
    >>> Y: $y";
    >>> $x+$y}
    >>> $script2 = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host
    >>> "Method2
    >>> X: $x Y:$y"; $x+$y}
    >>> $member1 = new-object management.automation.PSScriptMethod
    >>> "Method1",$script1 $object.psobject.members.Add($member1)
    >>>
    >>> $member2 = new-object management.automation.PSScriptMethod
    >>> "Method2",$script2 $object.psobject.members.Add($member2)
    >>>
    >>> $new = $object
    >>> $new.Method1(5,6)
    >>> Method1 X: 5 Y: 6
    >>> 11
    >>> $new.Method2(7,8)
    >>> Method2 X: 7 Y:8
    >>> 15
    >>> -----
    >>> Shay Levi
    >>> $cript Fanatichttp://scriptolog.blogspot.com
    >> I executed
    >>
    >> $new = $object
    >> $new.Method1(5,6)
    >> and got:
    >>
    >> Method1 X: Y:
    >>
    >> There has to be some explanation. I'm fairly certain I've had the
    >> same behavior on more than one system but they are W3K
    >>
    >> Why would I need to "Capture" the object? What happens when you run
    >> the method on $object?- Hide quoted text -
    >>
    >> - Show quoted text -
    >>
    > Hey cash,
    >
    > You may have missed my other post - the explanation is simple. What
    > you're trying to do won't work in powershell 1.0 - its broken - a bug
    > - foobared. Shay is using a beta of powershell 2.0 where the problem
    > has been fixed. You will have to use the $args collection in anonymous
    > scriptblocks. Named parameters with "param" doesn`t work.
    >
    > Hope this helps,
    >
    > - Oisin / x0n
    >


      My System SpecsSystem Spec

  10. #10


    cashfoley Guest

    Re: Using param with InvokeReturnAsIs

    Thanks everyone. This is less disturbing than an OS difference.

    Cash

      My System SpecsSystem Spec

Using param with InvokeReturnAsIs problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Param - named vs positional? Gerry Hickman PowerShell 6 03 Feb 2010
PowerShell param array Daniel Jameson PowerShell 2 23 Aug 2008
What expressions can you specify in the -filter param for dir Bob Landau PowerShell 2 28 Aug 2007
param VS dotsourcing Nicopilami PowerShell 5 21 Aug 2007
Invoke-Item with param. MiroslavK PowerShell 2 03 May 2007