开发者

Is this the simplest way to print 'Hello World' using PowerShell from C#?

开发者 https://www.devze.com 2023-02-01 18:50 出处:网络
Console.WriteLine( PowerShell .Create() .AddScript(\"\'Hello from PowerShell\'\") .Invoke()[0] ); Here are IronPython and IronRuby
Console.WriteLine(
    PowerShell
      .Create()
      .AddScript("'Hello from PowerShell'")
      .Invoke()[0]
);

Here are IronPython and IronRuby

Python.CreateEngine()
    .Execute("print 'Hello from Iro开发者_如何学CnPython'");

Ruby.CreateEngine()
    .Execute("puts 'Hello from IronRuby'");


Yes.

Generally speaking, when creating a "real" PowerShell host, you would need to ADD "Out-Default" to every pipeline, and then implement the host API (UI.WriteLine) so that the output would show up somewhere.

But for a simple "hello world" ... getting the output off the .Invoke() is the simplest thing.

I'm still holding out hope that a future release of PowerShell will be based on the DLR and support the same syntax as the Iron* languages do now for simple invoking... but I don't know if even then you'd get automatic output like that (I'm not sure I'd even want it) because that would basically require the current default "ConsoleHost" to be built into the engine (or something).

0

精彩评论

暂无评论...
验证码 换一张
取 消