2

I have a powershell script, say: function2.ps1 with:

function try2
{
    return "Hello"
}

and then in c#, I have:

RunspaceConfiguration rsc = RunspaceConfiguration.Create();
Runspace rs = RunspaceFactory.CreateRunspace(rsc);
rs.Open();

RunspaceInvoke si = new RunspaceInvoke(rs);
PowerShell ps = PowerShell.Create();

ps.Commands.AddScript(". .\\function2.ps1");
ps.Invoke();

ps.AddCommand("try2");
ps.Invoke();

It gives out a System.Managment.Automation.CommandNotFoundException saying that try2 is not recognized as the name of a cmdlet, function, script file, blah blah blah.

This is really tricky, what do I miss? :)

UPDATE:

function.ps1 is located: c:\function.ps1.

The current approach is:

ps.Commands.AddScript(@"cd C:\; . .\function.ps1;try2");
ps.Invoke();

but still failing, and even more interesting:

ps.Commands.AddScript(@"cd C:\; . .\function222222222222222222.ps1;try2");
ps.Invoke();

although I am 100% sure the function22222222222222222222.ps1 DOES NOT EXIST but no error will be given. For sure there is something wrong with giving the file path...

[UPDATE]

It turned out that it is because of some runtime errors in the referncing assembly in the powershell file: This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

After changing the target framework from 4.0 to 3.5, I find that the hello is printed! So the error is not about calling the script but is about the script itself. Sorry for confusion and thank you everyone!

jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
  • Does this work: `ps.Commands.AddScript(@". .\function2.ps1; try2");` ? BTW, I would specify a fully qualified path to function2.ps1. – Keith Hill Jan 24 '13 at 21:03
  • This is what I use now: ps.Commands.AddScript(@". C:\\function.ps1; try2"); but still showing the same exception.. – jamesdeath123 Jan 24 '13 at 21:17
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Jan 24 '13 at 21:31
  • After you call AddScript, Invoke can you check if there's anything in `ps.Streams.Error`? FYI, if AddScript fails an exception is not raised. – Serguei Jan 24 '13 at 21:40

2 Answers2

2

I think you need to specify the full path to function2.ps1. ".\function2.ps1" will only look for the script in the current directory which is whatever the C# process's initial working directory is set to unless you've changed it somewhere in your C# code. You can also change it in script e.g.:

ps.Commands.AddScript(@"cd <path>; . .\function2.ps1; try2"); 
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • You answer gives the best explanation to invoke a script and is most close to my question, although it was asked in the wrong direction... good score for you! – jamesdeath123 Jan 24 '13 at 21:57
  • yep, I've come across this problem when working with IIS as well (in .NET 4).. Thankfully, using a full path resolved the issue. Also, the current working directory can be checked in c# by looking at the System.Environment.CurrentDirectory variable. – Steve Rathbone Oct 04 '14 at 11:50
0

One method (very close to yours) of running .ps1 scripts from C# can be found at:

http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

You seem to be missing a few Pipeline commands in order to run your script.

eg:

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);

pipeline.Commands.Add("Out-String");

// execute the script

Collection<psobject /> results = pipeline.Invoke();

If you can't find the System.Management.Automation dll then perhaps you may need to download a more recent version of the Windows SDK, if not, you can probably find it at: C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\

from: Referencing system.management.automation.dll in Visual Studio

Community
  • 1
  • 1
malgca
  • 2,707
  • 2
  • 16
  • 9
  • Actually pipeline is not necessary, and the reference of system.management.automation.dll is not the issue, otherwise the program won't even compiled. You provided very detailed solution though :) Thanks anyway! – jamesdeath123 Jan 24 '13 at 21:59