header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Named input parameters
Last Post 06 Aug 2008 04:24 PM by NJBrad. 6 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
NJBradUser is Offline
New Member
New Member
Posts:9
Avatar

--
04 Aug 2008 11:41 AM

    I have a script that I can run from the command line, but when I try to call it from a C# program I get an error.

    Script:

    Param($VarA)

    Write-Host $VarA

     

    When I run it, I get a CommandNotFoundException  with the message "The term 'param' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again."

    Any suggestions?

    If I remove the Param from the top of the script, it runs fine.  I set the values with

    .SessionStateProxy.SetVariable(...), but would like to be able to test and run without changing the script.

     

    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    05 Aug 2008 11:27 AM
    Can you post some or all of your C# code so we can have a better picture of what your surrounding code looks like.
    NJBradUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    05 Aug 2008 02:18 PM
    The majority of the code came from http://reader.feedshow.com/show_ite...54d7076f27

    A piece of code I found somewhere else
    // helper method that takes your script path, loads up the script
    // into a variable, and passes the variable to the RunScript method
    // that will then execute the contents
    private string LoadScript(string filename)
    {
    try
    {
    // Create an instance of StreamReader to read from our file.
    // The using statement also closes the StreamReader.
    using (StreamReader sr = new StreamReader(filename))
    {

    // use a string builder to get all our lines from the file
    StringBuilder fileContents = new StringBuilder();

    // string to hold the current line
    string curLine;

    // loop through our file and read each line into our
    // stringbuilder as we go along
    while ((curLine = sr.ReadLine()) != null)
    {
    // read each line and MAKE SURE YOU ADD BACK THE
    // LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
    fileContents.Append(curLine + "\n");
    }

    // call RunScript and pass in our file contents
    // converted to a string
    return fileContents.ToString();
    }
    }
    catch (Exception e)
    {
    // Let the user know what went wrong.
    string errorText = "The file could not be read:";
    errorText += e.Message + "\n";
    return errorText;
    }

    }


    And finally my call
    string scriptResults = RubyToPS.InvokePS(LoadScript(@"c:\scripts\AddItUp.ps1"));

    And here's the script
    param($VarA)

    function AddStuff($x,$y)
    {
    $x + $y
    }

    AddStuff 6 5

    Write-Output $VarA


    And finally. The output
    ERROR: The term 'param' is not recognized as a cmdlet, function, operable program, or
    ERROR: script file. Verify the term and try again.
    ERROR: At line:1 char:6
    ERROR: + param( <<<< $VarA)
    11

    Thanks again for your help
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    06 Aug 2008 04:45 AM
    Well, the script isn't being parsed properly. OK, so you're using C#->IronRuby->PowerShell. Is that required?
    NJBradUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    06 Aug 2008 07:12 AM
    I got the sample from a Ruby focused site when I did a Google search. I'm not actually using Ruby. It's just in the name of the class I copied. (I don't have Ruby installed on my system)

    I'm really just doing C#->PowerShell
    NJBradUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    06 Aug 2008 08:57 AM

    I got it to work, but I don't like it

    I ended up putting each line of the script as a seperate command
    pipeline.Commands.AddScript("Param([String] $VarA)");
    pipeline.Commands.AddScript("echo $VarA");
    instead of
    pipeline.Commands.AddScript("Param([String] $VarA) \n" +
    "echo $VarA");

    I can do this, but I end up with this line and I'm not sure how to resolve it.
    pipeline.Commands[ 0 ].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);

    Do I need to repeat this for each command?

    Thanks
    Brad

    <>

    Well, it doesn't work.  Powershell ends code blocks other places besides EOL. 

    When I used the above code with a more complex script, I got a System.Management.Automation.IncompleteParseException - "Missing function body in function declaration"

    Brad

    NJBradUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    06 Aug 2008 04:24 PM

    Here we go again!

    When I load the script directly from within the pipeline from a file everything works

    pipeline.Commands.Add( File Name );

    I wonder what the end of line character is supposed to be for the AddScript method????

     

    Brad

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer