Windows Vista Forums

Handling param statement in custom host application
  1. #1


    paul Guest

    Handling param statement in custom host application

    I have written a WinForms application that hosts powershell. The problem is
    that some of the scripts I want to run through my host contain the "param"
    statement at the beginning of them. It seems that the Microsoft PowerShell
    host application looks for this and does something special with it in order
    to hook up the parameter references in the script and the parameter values.
    "param" doesn't seem to be processed by the pipeline anyhow and is says it's
    not recognised at a cmdlet, function etc.



    Any help as to how a process the "param" statement in my Powershell host
    would be appreciated.
    Thanks

      My System SpecsSystem Spec

  2. #2


    Roman Kuzmin Guest

    Re: Handling param statement in custom host application

    Could you please provide a piece of code that fails and\or a script with
    "param" that causes this failure?
    (side note: I develop a PS host application for quite a long time and I have
    never seen this problem)

    --
    Thanks,
    Roman Kuzmin

    http://code.google.com/p/farnet/
    PowerShell and .NET in FAR Manager



      My System SpecsSystem Spec

  3. #3


    NJBrad Guest

    Re: Handling param statement in custom host application



    "Roman Kuzmin" wrote:

    > Could you please provide a piece of code that fails and\or a script with
    > "param" that causes this failure?
    > (side note: I develop a PS host application for quite a long time and I have
    > never seen this problem)
    >
    > --
    > Thanks,
    > Roman Kuzmin
    >
    > http://code.google.com/p/farnet/
    > PowerShell and .NET in FAR Manager
    >
    >
    I know this is rather late in the thread, but I ran into the same issue this
    week.

    When you call
    pipeline.Commands.AddScript("Param([String] $VarA) \r\n"+
    "echo $VarA");
    the error will occur.

    If you save the same two lines to a file and then call
    pipeline.Commands.Add( name of file );
    everything works fine


      My System SpecsSystem Spec

  4. #4


    Roman Kuzmin Guest

    Re: Handling param statement in custom host application

    NJBrad,

    It's not too late :-)

    *) I think that two commands you provided are not the same at all (the
    second is correct, indeed).

    *) I am not sure what you are going to do with the first command (still not
    enough the code). Thinking in pure PowerShell it looks rather like the code:

    Param([String] $VarA); echo $VarA | ...
    or
    .... | Param([String] $VarA); echo $VarA
    or
    .... | Param([String] $VarA); echo $VarA | ...
    or
    ???

    In any case syntax is odd and apparently not correct.

    I cannot play with it just now, so that I would recommend you to try to use
    a script block, i.e.

    pipeline.Commands.AddScript("{ Param ... }")
    (or perhaps this: pipeline.Commands.AddScript("&{ Param ... }")

    which corresponds to at least valid and working (at least in V2) syntax:

    0> & {param($vara); echo $vara } 'foo'
    foo

    --
    Thanks,
    Roman Kuzmin

    http://code.google.com/p/farnet/
    PowerShell and .NET in FAR Manager



      My System SpecsSystem Spec

  5. #5


    NJBrad Guest

    Re: Handling param statement in custom host application



    "Roman Kuzmin" wrote:

    > NJBrad,
    >
    > It's not too late :-)
    >
    > *) I think that two commands you provided are not the same at all (the
    > second is correct, indeed).
    >
    > *) I am not sure what you are going to do with the first command (still not
    > enough the code). Thinking in pure PowerShell it looks rather like the code:
    >
    > Param([String] $VarA); echo $VarA | ...
    > or
    > ... | Param([String] $VarA); echo $VarA
    > or
    > ... | Param([String] $VarA); echo $VarA | ...
    > or
    > ???
    >
    > In any case syntax is odd and apparently not correct.
    >
    > I cannot play with it just now, so that I would recommend you to try to use
    > a script block, i.e.
    >
    > pipeline.Commands.AddScript("{ Param ... }")
    > (or perhaps this: pipeline.Commands.AddScript("&{ Param ... }")
    >
    > which corresponds to at least valid and working (at least in V2) syntax:
    >
    > 0> & {param($vara); echo $vara } 'foo'
    > foo
    >
    > --
    > Thanks,
    > Roman Kuzmin
    >
    > http://code.google.com/p/farnet/
    > PowerShell and .NET in FAR Manager
    >
    >
    I'm trying to execute the exact same thing in PowerShell. This is a
    stripped down script showing the problem. I want to be able to pass a named
    parameter into a script. I want the same script to be able to be run from
    PowerShell itself and also from within my own host. Taking the exact same
    text that is in a file and calling AddScript "should" do the same thing. The
    only difference I'm running into is the param at the beginning of the script.
    Wrapping the script with { and } doesn't yield the same result as running it
    by itself.


      My System SpecsSystem Spec

Handling param statement in custom host application problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling Application Block Polly Anna .NET General 2 17 May 2008
Handling Application.Start via Custom HTTP Module Cramer .NET General 0 23 Apr 2008
Custom Exception Handling And Logging using EnterPrise Library app Shrikant More .NET General 0 31 Mar 2008
Exception handling in Vista Application Aale de Winkel Vista General 0 16 Apr 2007
Handling Drag Events in Custom Control viRtual Avalon 2 04 Jun 2006