41

Is there a way to run PowerShell scripts from .net-core ?

I'm trying to run a PowerShell script in a new .net core 'website\api'. From what I can tell in order to run PowerShell on .net we need to add the

System.Management.Automation namespace.

This isn't possible for .net core ( or I haven't found the appropriate way to add it). There are a couple of NuGet packages which are also aimed at adding this DLL to project but those aren't compatible with .net core either. Is there a way to do this on .net core ? Here are some links I've tried but none of them are .net core specific:

http://www.powershellmagazine.com/2014/03/18/writing-a-powershell-module-in-c-part-1-the-basics/

Referencing system.management.automation.dll in Visual Studio

https://www.nuget.org/packages/System.Management.Automation/

wonea
  • 4,783
  • 17
  • 86
  • 139
JanivZ
  • 2,265
  • 3
  • 25
  • 31
  • hun, tek frist link is 2014 – Jaqueline Vanek Aug 25 '16 at 09:53
  • the first link is there to show the code ( and the namespace) necessary to run shell via code .... – JanivZ Aug 25 '16 at 10:26
  • how about net-core? – Jaqueline Vanek Aug 25 '16 at 10:35
  • I don't understand what you're asking – JanivZ Aug 25 '16 at 10:40
  • 2
    Me neither ;) ... However, I think this is not a namespace curated right now by the .NET team for .NET Core. Go to the Github Repo and ask it from the Powershell team. They should be the ones curating this kind of integrations. File a question here with more concrete details on what you want to do: https://github.com/PowerShell/PowerShell – Thomas Aug 25 '16 at 18:04
  • Dude! Thanks man! – JanivZ Aug 25 '16 at 18:11
  • 3
    [The powershell-core MyGet feed](https://powershell.myget.org/gallery/powershell-core/) contains alpha version of `System.Management.Automation` that supports .Net Core. Would that work for you? – svick Aug 25 '16 at 19:56
  • @svick - did manage to build and reference the dll in a core app ? took me since you and pointed me in that direction until now to actually build everything there - i've nuget packaged it and everything but it still says its incompatible .... – JanivZ Aug 27 '16 at 22:55
  • @JanivZ Referencing and building works fine for me. Actually executing some commands less so, I will have to investigate that further. – svick Aug 28 '16 at 00:01
  • @svick - yeh me too - it builds, i can reference it and it builds fine - but im getting unhandled exceptions when using it .. let me know if you make any progress .. – JanivZ Aug 28 '16 at 09:03
  • @JanivZ [Here is my report of the exception I'm getting.](https://github.com/PowerShell/PowerShell/issues/2108) – svick Aug 28 '16 at 13:24
  • @svick - thanks so much ! you pointed me in a great direction and got me started with all this .net core stuff.. Copy one of your comments to an answer so you can get some credit at least ! thanks again ! – JanivZ Aug 28 '16 at 19:47
  • 2
    @Thomas - you too Thomas - thanks a lot - great lead you guys gave me – JanivZ Aug 28 '16 at 19:48

4 Answers4

50

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());
            }
        }
    }
}
lindexi
  • 4,182
  • 3
  • 19
  • 65
James Eby
  • 1,784
  • 20
  • 25
  • 2
    Yep, support was added! Thanks for adding the info! – JanivZ Dec 12 '17 at 16:57
  • 2
    No problem, thanks to svick for pointing me in the right direction and you for asking the question. – James Eby Dec 12 '17 at 18:04
  • 1
    I get System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' but only on prod, in dev it works well... – Sylvain Gantois May 29 '19 at 06:33
6

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.

svick
  • 236,525
  • 50
  • 385
  • 514
  • 3
    AppDomain.GetAssemblies() is available in .NET Core 2.0. I don't know if that solves the question asked here. https://learn.microsoft.com/en-gb/dotnet/api/system.appdomain.getassemblies?view=netcore-2.0 – Bernard Vander Beken Oct 24 '17 at 10:49
6

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

lindexi
  • 4,182
  • 3
  • 19
  • 65
1

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

Roman
  • 1,727
  • 1
  • 20
  • 28