Running PowerShell from .NET Core

39,175

Solution 1

The official answer is that running PowerShell Core from your own application is currently not supported. Probably the biggest issue is that .Net Core is missing AppDomain.GetAssemblies(), which might be fixed in .Net Core 1.2.

Solution 2

Looks like it is well supported as of .NET Core 2.0 and PowerShell 6 Beta 3 (although it was supported in Beta 1 and 2 also but not as easily), here is a link to the Host PowerShell documentation in the GitHub repo

And they give a good sample application showing it running with .NET Core 2.0 and PowerShell Core v6.0.0-beta.3 and Later:

https://github.com/PowerShell/PowerShell/tree/master/docs/host-powershell

In order to get the correct packages into my NuGet package list I did need to add powershell-core as a new NuGet repository location which was:

https://powershell.myget.org/F/powershell-core/api/v3/index.json

I could then install the NuGet packages:

install-package microsoft.powershell.sdk -version 6.0.0-rc
install-package microsoft.powershell.commands.diagnostics -version 6.0.0-rc
install-package microsoft.wsman.management -version 6.0.0-rc

All three of these dependencies were required and then I could execute the following simple PowerShell command in my asp.net core MVC Web Application:

public class PowerShellHelper
{
    public void Execute(string command)
    {
        using (var ps = PowerShell.Create())
        {
            var results = ps.AddScript(command).Invoke();
            foreach (var result in results)
            {
                Debug.Write(result.ToString());
            }
        }
    }
}

Solution 3

Thx for @Roman and @JamesEby.

If we can not use dotnet core 2.0 or later and we can use Process to run the PowerShell.exe in Windows.

The path is C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and we can use Process in this code.

        var process = new Process
        {
            StartInfo = new ProcessStartInfo(@"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe",
                script)
            {
                WorkingDirectory = Environment.CurrentDirectory,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
            }
        };
        process.Start();

        var reader = process.StandardOutput;
        return reader.ReadToEnd();

The script value is the PowerShell Script and the reader.ReadToEnd() return the power shell output text.

See: https://stackoverflow.com/a/30846513/6116637

Solution 4

Although James Eby's answer is correct, there has been some new info that I found useful.

There is now PowerShell Core available for cross-platform use. And it is open source!

Here is some useful points from the article from Microsoft:

PowerShell now officially supports macOS and Linux, including:

  • Windows 7, 8.1, and 10
  • Windows Server 2008 R2, 2012 R2, 2016
  • Windows Server Semi-Annual Channel
  • Ubuntu 14.04, 16.04, and 17.04
  • Debian 8.7+, and 9
  • CentOS 7
  • Red Hat Enterprise Linux 7
  • OpenSUSE 42.2
  • Fedora 25, 26
  • macOS 10.12+

Logging

On macOS, PowerShell uses the native os_log APIs to log to Apple's unified logging system. On Linux, PowerShell uses Syslog, a ubiquitous logging solution.

SSH-based PowerShell Remoting

The PowerShell Remoting Protocol (PSRP) now works with the Secure Shell (SSH) protocol in addition to the traditional WinRM-based PSRP. Source

Breaking Changes link

Share:
39,175
JanivZ
Author by

JanivZ

Updated on February 24, 2020

Comments