Powershell: How to add PowerShell to an ASP.Net Core 2.0 project?

Created on 25 Aug 2017  路  52Comments  路  Source: PowerShell/PowerShell

I want to collect data via a webpage using ASP.NET Core 2.0, and on save/create, call PowerShell to go complete the work, and update the site that the work is complete.

In full ASP.NET you could easily target .NET 2.0, call system.management.automation and use runspaces. I understand there is now a different load method to access PowerShell for ASP.NET CORE 2.0. However I've been unable to find a way to add Microsoft.PowerShell.SDK to my project.

I found https://powershell.myget.org/feed/powershell-core/package/nuget/Microsoft.PowerShell.SDK/6.0.0-beta.6 but using that site for Install-Package fails on both beta5 and 6 due to missing files. Assuming this is even how you would add integration into VS2017. A regular search of GitHub Packages reveals no links at all.

Am I doing something wrong, can you even do this with ASP.NET Core 2.0, or is there an actual issue here with the repositories?

PM> Install-Package Microsoft.PowerShell.SDK -Version 6.0.0-beta.6 -Source https://powershell.myget.org/F/powershell-core/api/v3/index.json
Restoring packages for C:\Users\dbreeze\Documents\Visual Studio 2017\Projects\DNSFrontEnd\DNSFrontEnd\DNSFrontEnd.csproj...
GET https://api.nuget.org/v3-flatcontainer/microsoft.powershell.security/index.json
GET https://api.nuget.org/v3-flatcontainer/microsoft.powershell.commands.utility/index.json
GET https://api.nuget.org/v3-flatcontainer/microsoft.powershell.commands.management/index.json
GET https://api.nuget.org/v3-flatcontainer/system.management.automation/index.json
GET https://api.nuget.org/v3-flatcontainer/microsoft.powershell.consolehost/index.json
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.powershell.security/index.json 151ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.powershell.commands.utility/index.json 163ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.powershell.commands.management/index.json 245ms
OK https://api.nuget.org/v3-flatcontainer/system.management.automation/index.json 335ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.powershell.consolehost/index.json 459ms
Microsoft.PowerShell.SDK 6.0.0-beta.6 depends on System.Management.Automation (>= 6.0.0-beta.6) but System.Management.Automation 6.0.0-beta.6 was not found. An approximate best match of System.Management.Automation 6.1.7601.17514 was resolved.
Install-Package : Unable to find package Microsoft.PowerShell.Security. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages, nuget.org, Package source
At line:1 char:1

  • Install-Package Microsoft.PowerShell.SDK -Version 6.0.0-beta.6 -Sourc ...
  • ~~~~~~~~~~~~~~~~~

    • CategoryInfo : NotSpecified: (:) [Install-Package], Exception

    • FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Install-Package : Unable to find package Microsoft.PowerShell.Commands.Utility. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages, nuget.org, Package source
At line:1 char:1

  • Install-Package Microsoft.PowerShell.SDK -Version 6.0.0-beta.6 -Sourc ...
  • ~~~~~~~~~~~~~~~~~

    • CategoryInfo : NotSpecified: (:) [Install-Package], Exception

    • FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Install-Package : Unable to find package Microsoft.PowerShell.Commands.Management. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages, nuget.org, Package source
At line:1 char:1

  • Install-Package Microsoft.PowerShell.SDK -Version 6.0.0-beta.6 -Sourc ...
  • ~~~~~~~~~~~~~~~~~

    • CategoryInfo : NotSpecified: (:) [Install-Package], Exception

    • FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Install-Package : Unable to find package Microsoft.PowerShell.ConsoleHost. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages, nuget.org, Package source
At line:1 char:1

  • Install-Package Microsoft.PowerShell.SDK -Version 6.0.0-beta.6 -Sourc ...
  • ~~~~~~~~~~~~~~~~~

    • CategoryInfo : NotSpecified: (:) [Install-Package], Exception

    • FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Install-Package : Package restore failed. Rolling back package changes for 'DNSFrontEnd'.
At line:1 char:1

  • Install-Package Microsoft.PowerShell.SDK -Version 6.0.0-beta.6 -Sourc ...
  • ~~~~~~~~~~~~~~~~~

    • CategoryInfo : NotSpecified: (:) [Install-Package], Exception

    • FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Issue-Question Resolution-Fixed

Most helpful comment

any updates regarding NuGet packages on nuget.org?

All 52 comments

Please see docs

I can't get it to work either, looked at the docs
any chance we can get a hand?

I tried the example project, but the packages its using don't seem to exist any more

when I use the latest ones

<PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="6.0.1" />
    <PackageReference Include="Microsoft.PowerShell.Commands.Management" Version="6.0.1" />
    <PackageReference Include="Microsoft.PowerShell.Commands.Utility" Version="6.0.1" />

I get a "cannot load Microsoft.Powershell" exception

May we get an update on the example?

@daxian-dbw Could you please comment? Seems our documentation needs updating. Maybe after #3417?

I have recently managed to run PS in my asp.net core 2.x app. I had to reference another nuget feed for this though:

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

And installed:
Microsoft.PowerShell.Commands.Diagnostics 6.0.1
Microsoft.PowerShell.SDK 6.0.1
Microsoft.WSMan.Management 6.0.1

not working for me

  • new core console app v 2.1.100-preview-007363
  • nuget as above added the same packages

image

https://github.com/MrAntix/DotNetCore2.PSTest

@hassejansson sorry, skim read, of course they are different packages than I had

with the three you mention the app works fine

Microsoft.PowerShell.Commands.Diagnostics 6.0.1
Microsoft.PowerShell.SDK 6.0.1
Microsoft.WSMan.Management 6.0.1

Many thanks

The docs reference PowerShell 6, is it possible to host Windows PowerShell in .net core 2.0?

No, you won't be able to get Windows PowerShell (5.x) to load in .NET Core -- it's a fat .NET 4x project.

On the original post note, I can confirm that the new 6.0.1.1 SDK works (at least on Windows and Linux).

Can we get that published to NuGet now?

Hi Folks, I appreciate the work that has gone into PowerShell Core, it has been very useful for our project! The 6.0.1.1 NuGet packages work as described for our .NET Core 2.0 project.

Just wondering: is there any particular reason as to why this is being hosted on MyGet? Our development team is having proxy issues when having MyGet and our internally-hosted NuGet repository added in Visual Studio.

If this official package was in NuGet it would definitely simplify restoring NuGet packages.

/cc @SteveL-MSFT Could you please comment?

Nightlies should be published to MyGet. Nupkgs intended for public consumption should be published to Nuget. cc @adityapatwardhan

The NuGet packages will be posted on nuget.org by the end of this week.

What about implementation of "Nightlies should be published to MyGet"?

Currently we do not produce nightly builds for the hosting SDK.

any updates regarding NuGet packages on nuget.org?

I've been struggling with this too. The 6.1.0-preview.1 release works fine in a simple .NET Core 2.0 console app, but when this package is added to an Azure Function (.NET Standard 2.0, Class lib) it throws an exception:

"Could not load file or assembly 'System.Management.Automation, Version=6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.".

I added the 6.1.0-preview.1 package in the same way for both and both are running on the same PC. By the way, I run my Azure Fn using VS2017 debugger and invoke it by posting msg to EventGrid which accesses my local host via Ngrok. The same happens if I publish / run on Azure.

What's going on? I understood that packages were meant to avoid this sort of problem, so suspect that there is something wrong with the MyGet package.

The sooner we can get a stable version released on Nuget, the better.

@ImageQC can you share your csproj file.
@el7lme Still working on the packages. Sorry for the delay.

Here is my csproj file as requested. These are the packages as published to Azure which cause the error message to appear in the log - see previous post:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta5" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0-beta5" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventGrid" Version="2.0.0-beta1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.13" />
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="6.1.0-preview.1" />
    <PackageReference Include="System.Data.SqlClient" Version="4.4.0-preview1-25305-02" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Note 1: I am using System.Data.SqlClient 4.4.0-preview1-25305-02 instead of 4.4.3 to avoid errors at runtime.

any idea of timescale for fixing this? I need to run Powershell from an Azure function, ideally using C#. This would seem to be a common requirement so I'm surprised isn't not working. However, thanks for your help.

@el7lme I am actively working on releasing the packages on nuget.org. It should just be a few more days. Sorry for the delay.

@ImageQC I will try to repro this in an Azure function to investigate this further. Thanks for your patience.

please consider me as your first beta tester ;-) Many thanks Will

First Beta Tester rank is assigned only after 1000th feedback :-)

iSazonov might well be right - I will need to ask 1000 times before this software is released ;-)

Seriously, how's it going Adityapwardhan? It would be great to get a timescale. In the meantime I shall have to implement as a WebJob.

@ImageQC I believe Azure is high priority but I don't know about the timescale. You could have a conversation with @SteveL-MSFT and @joeyaiello about your project.

Still working through a few issues. Unfortunately I do not have a timeline yet.

@adityapatwardhan
Hi
We too have a requirement on the package being available from NuGet.org in order to be able to use in in our products.
Matt

@Mattacks I understand your concerns and am working towards getting it published soon. Thank you for your patience.

  • 9th April "The NuGet packages will be posted on nuget.org by the end of this week."

  • 26 May - still no Powershell package released on NuGet (pre-release or otherwise) and even the MyGet package (6.1.0-preview.1) doesn't work with a Azure Function (.NET Standard 2.0, Class lib).

Clearly there is a significant problem here. I suspect it relates to package dependencies which are quite fluid in the pre-release space. Is it that Microsoft are in the middle to sorting out this problem (not before time) so release of the Powershell package is being held-up until this work is done? If this is the case, it would be good to get some idea of timescales - a month, three months, next year?

It is surprising that ASP.NET Core 2.0 was released almost a year ago, but still Powershell integration isn't fully supported. Therefore instead of using the new cool Azure Function Apps, I'm forced to use a WebJob running on .NET Framework. I'm not happy.

@ImageQC Sorry for the delay. We are hitting some unforeseen issues. I am working on resolving those. A best estimate timeline for releasing on NuGet.org is before end of June.

Its almost the end of June. Can we get this on Nuget Pleasee ...

This is still work in progress. We have to release nuget packages for all the dependencies as well on nuget.org. This is what is taking time to complete. The work is happening in https://github.com/PowerShell/powershell-native for building and releasing native dependencies for all supported platforms. Thank you for your patience.

Eagerly awaiting release of this Nuget package as well. We are working with projects targeted to .NET Standard 2.0 and want to use powershell invokes in C# (using System.Management.Automation)

@adityapatwardhan with recent Preview.4 re-publish this should be fixed?

Can we assume this won't be supported this year? Is this currently the only supported way to host powershell core?

@mi-hol no. Building modules and _hosting_ PowerShell are two different problems.

This is still being worked on. I'd hesitate to provide any ETA that we may end up missing.

Microsoft.PowerShell.SDK and related packages were release for version 6.0.4 today morning on nuget.org

Please give them a try and let me know if you see any issues.

@ImageQC Can you give a try for release on nuget.org for version 6.0.4. Thanks for your patience.

This should be resolved, please comment on this issue if it's not addressed or open new specific issues for new problems.

@RogerDodger123 Please open new Issue with your question, repo steps, versions. Have you the project or example project on GitHub to share for review?

@SteveL-MSFT Can .Net Core 2.1 Console app with package Microsoft.Powershell.SDK 6.1.1 installed run on a Windows without Powershell Core 6.1?

I am writing a .Net core 2.1 console to execute some powershell files, but will be using Windows Powershell and I am getting $env:PSModulePath not the same.

$env:PSModulePath in Windows Powershell ISE returns correct path to my modules at c:\programfiles\windowspowershell

But trying to call $env:PSModulePath in C# app, it will return different value with module location at c:\programfiles\powershell (without prefix windows). That is why I could not run PS files due to error "Could not find any module directory"

Thanks

@sonphnt PowerShell Core 6 has different PSModulePath than Windows PowerShell (which enables them to run side by side). If you need the same paths as ISE (aka Windows PowerShell), you should use https://www.nuget.org/packages/Microsoft.PowerShell.5.ReferenceAssemblies/ which will use the runtime assemblies on the machine.

@SteveL-MSFT Hi, we just need to install that nuget package and done?. Any cumbersome configuration (with C# code) with that package?

@sonphnt it depends on what you are trying to do, but if you reference that nupkg it should just work with Windows PowerShell 5

@SteveL-MSFT What I am doing is to develop a .net core 2.1 console app and run as Windows Service. I will deploy it to a fresh Windows Server 2016 (WindowsPowershell v5) without installing anything. And hope It will work but I am not sure this is possible?

https://www.nuget.org/packages/Microsoft.PowerShell.5.ReferenceAssemblies/ is not compatible with .net core 2.1

@sonphnt if you are using .net core 2.1, you must use Microsoft.PowerShell.SDK

@sonphnt those are two independent things. If you are writing a C# project, you would use the Microsoft.PowerShell.SDK nuget pkg to run PowerShell within your app. The GitHub release is so you can use PowerShell as your console.

@sonphnt those are two independent things. If you are writing a C# project, you would use the Microsoft.PowerShell.SDK nuget pkg to run PowerShell within your app. The GitHub release is so you can use PowerShell as your console.

What if I am using Net Framework as the target in the new csproj format. Do you have the experience which I have mentioned in issue #8698

@SteveL-MSFT, @joeyaiello I have same problem as @ImageQC with Azure Functions. Here is my project (just a new empty Azure Function in VS 2019 with Microsoft.PowerShell.SDK added):

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="6.2.2" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

When try to build I got this:

d:.nuget\microsoft.net.sdk.functions\1.0.29\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : System.IO.FileNotFoundException: Could not load file or assembly 'System.Management.Automation, Version=6.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
d:.nuget\microsoft.net.sdk.functions\1.0.29\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : File name: 'System.Management.Automation, Version=6.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
d:.nuget\microsoft.net.sdk.functions\1.0.29\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
d:.nuget\microsoft.net.sdk.functions\1.0.29\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at System.Reflection.RuntimeAssembly.GetExportedTypes()
d:.nuget\microsoft.net.sdk.functions\1.0.29\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.FunctionJsonConverter.TryGenerateFunctionJsons()
d:.nuget\microsoft.net.sdk.functions\1.0.29\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets(41,5): error : at MakeFunctionJson.FunctionJsonConverter.TryRun()

Does Azure Functions still not supported for PSH SDK?

@anirudhgarg can someone on your team look into this?

Was this page helpful?
0 / 5 - 0 ratings