I\'m trying to call a function in a powershell file as follows:
string script = System.IO.File.ReadAllText(@\"C:\\Users\\Bob\\Desktop\\CallPS.ps1\");
The solution can be simplified further as in this case a non-default Runspace is not needed.
var ps = PowerShell.Create();
ps.AddScript(script);
ps.Invoke();
ps.AddCommand("BatAvg").AddParameters(new Dictionary
{
{"Name" , "John"}, {"Runs", "6996"}, {"Outs","70"}
});
foreach (var result in ps.Invoke())
{
Console.WriteLine(result);
}
One other pitfall would be to use AddScript(script, true)
in order to use a local scope. The same exception would be encountered (i.e. "The term 'BatAvg' is not recognized as the name of a cmdlet, function, script file, or operable program.").