header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Named input parameters
Last Post 07 Aug 2008 01:24 AM by NJBrad. 6 Replies.
Printer Friendly
Sort:
NextNext
You are not authorized to post a reply.
Author Messages
NJBradUser is Offline
New Member
New Member
Posts:8
Avatar

--
04 Aug 2008 08: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.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:195
Avatar

--
05 Aug 2008 08: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: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
NJBradUser is Offline
New Member
New Member
Posts:8
Avatar

--
05 Aug 2008 11:18 PM  
The majority of the code came from http://reader.feedshow.com/show_items-feed=f75024de5578391a6f447554d7076f27

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
Co-Community Director
Basic Member
Basic Member
Posts:195
Avatar

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

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
NJBradUser is Offline
New Member
New Member
Posts:8
Avatar

--
06 Aug 2008 04:12 PM  
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:8
Avatar

--
06 Aug 2008 05:57 PM  

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:8
Avatar

--
07 Aug 2008 01:24 AM  

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.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer