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