header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Named input parameters
Last Post 06 Aug 2008 05: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 12:41 PM

    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 Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1646
    Avatar

    --
    05 Aug 2008 12:27 PM
    Can you post some or all of your C# code so we can have a better picture of what your surrounding code looks like.
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    NJBradUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    05 Aug 2008 03: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 Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1646
    Avatar

    --
    06 Aug 2008 05:45 AM
    Well, the script isn't being parsed properly. OK, so you're using C#->IronRuby->PowerShell. Is that required?
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    NJBradUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    06 Aug 2008 08: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 09: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 05: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 Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer