question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run powershell samples in clean dotnet core container

See original GitHub issue

I am trying to run dot-net core app from “Host Powershell” section., https://github.com/PowerShell/PowerShell/tree/master/docs/host-powershell So I can run .net app which require PS sessions support in minimalistic environment

Steps to reproduce

  1. Make sure you have your Docker daemon in Linux mode or just run it on Linux host
  2. Save following dockerfile locally as Dockerfile.1.1.2_ps:
FROM buildpack-deps:jessie-scm

# Install .NET CLI dependencies
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        libc6 \
        libcurl3 \
        libgcc1 \
        libgssapi-krb5-2 \
        libicu52 \
        liblttng-ust0 \
        libssl1.0.0 \
        libstdc++6 \
        libunwind8 \
        libuuid1 \
        zlib1g \
        apt-transport-https	\
		git-core\
    && rm -rf /var/lib/apt/lists/*

# Install .NET Core SDK
ENV DOTNET_SDK_VERSION 1.0.4
ENV DOTNET_SDK_DOWNLOAD_URL https://dotnetcli.blob.core.windows.net/dotnet/Sdk/$DOTNET_SDK_VERSION/dotnet-dev-debian-x64.$DOTNET_SDK_VERSION.tar.gz
ENV DOTNET_CLI_TELEMETRY_OPTOUT 1


RUN curl -SL $DOTNET_SDK_DOWNLOAD_URL --output dotnet.tar.gz \
    && mkdir -p /usr/share/dotnet \
    && tar -zxf dotnet.tar.gz -C /usr/share/dotnet \
    && rm dotnet.tar.gz \
    && ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet \
	&& mkdir /scripts

# Trigger the population of the local package cache
ENV NUGET_XMLDOC_MODE skip
RUN mkdir warmup \
    && cd warmup \
    && dotnet new \
    && cd .. \
    && rm -rf warmup \
&& rm -rf /tmp/NuGetScratch


#install powershell
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
	&& curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list | tee /etc/apt/sources.list.d/microsoft.list \
	&& apt-get update \
	&& apt-get install -y powershell

# get powershell source 
RUN mkdir -p /projects/PowerShell && git clone https://github.com/PowerShell/PowerShell.git /projects/PowerShell

WORKDIR /projects/PowerShell/docs/host-powershell/sample-dotnet1.1

ENTRYPOINT /bin/bash

This comes from Docker hub plus code to install repo and pull Powershell repo, so there is standard and ready environment for the test

Then, run docker build -t powershell_test -f Dockerfile.1.1.2_ps . & docker run -ti --rm powershell_test

You’ll end up in sample-dotnet1.1 app directory.

Next, dotnet restore ./MyApp/MyApp.csproj

This one succeeded and proper NuGet packckages are pulled, as it mentioned here: https://github.com/PowerShell/PowerShell/issues/2284

NuGet Config files used: /projects/PowerShell/docs/host-powershell/sample-dotnet1.1/NuGet.config /projects/PowerShell/nuget.config /root/.nuget/NuGet/NuGet.Config Feeds used: https://api.nuget.org/v3/index.json https://dotnet.myget.org/F/dotnet-core/api/v3/index.json https://powershell.myget.org/F/powershell-core/api/v3/index.json

Then, start application: dotnet run --project ./MyApp/MyApp.csproj

Expected behavior

Application runs and produce output:


 Get-Command Write-Output                                                                                                                                                               
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Write-Output                                       3.1.0.0    Microsoft.PowerShell.Utility

Actual behavior

appBase: /projects/PowerShell/docs/host-powershell/sample-dotnet1.1/MyApp/bin/Debug/netcoreapp1.1

Evaluating 'Get-Command Write-Output' in PS Core Runspace


Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'System.Management.Automation.Runspaces.RunspaceFactory' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'api-ms-win-eventing-provider-l1-1-0.dll': The specified module could not be found.
 (Exception from HRESULT: 0x8007007E)
   at System.Management.Automation.Tracing.EtwActivity.UnsafeNativeMethods.EventActivityIdControl(ActivityControlCode controlCode, Guid& activityId)
   at System.Management.Automation.Tracing.EtwActivity.GetActivityId()
   at System.Management.Automation.Runspaces.RunspaceFactory..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke()
   at Application.Test.Logic.Start(String[] args) in /projects/PowerShell/docs/host-powershell/sample-dotnet1.1/Logic/UseRunspace.cs:line 20
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Management.Automation.PowerShellAssemblyLoadContextInitializer.InitializeAndCallEntryMethod(String basePaths, AssemblyName entryAssemblyName, String entryTypeName, String entryMethodName, Object[] args)
   at Application.Test.Program.Main(String[] args) in /projects/PowerShell/docs/host-powershell/sample-dotnet1.1/MyApp/Program.cs:line 25

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.4
OS                             Linux 4.9.36-moby #1 SMP Wed Jul 12 15:29:07 UTC 2017
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Not being expert in .dotnet it is not quite clear what is missing here. I’ve tried to set different RID in project, “debian.8-x64” from “dotnet --info”. Tried to publish & run. Tried to set AppBase to “/opt/microsoft/powershell/6.0.0-beta.4/”

Same error. Ok, if there some Win-only assemblies, why “powershell” executable works right in the same container? It is using the same assemblies. Or, not,

I am looking for a simple and concise answer for “mere mortals”. Otherwise, I have to run a hybrid swarm with Linux and Windows node just for Powershell only. Dealing with Windows docker is pain, just pain and bugs. Even after having few successful Linux Docker deliveries

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
anmenagacommented, Sep 26, 2017

I can repro on Debian 8.

Looks like this issue is because all PowerShell Core packages we published so far contain windows assemblies only. This is blocking hosting powershell in .NET Core applications on Linux/OSX.

Essentially this is a direct result/duplicate of #3417

0reactions
pmsanfordcommented, Sep 10, 2017

Same stack trace with the example on v6.0.0-beta.6 in .netcore 2.0 (in CentOS 7):


Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.6
OS                             Linux 3.10.0-514.26.1.el7.x86_64 #1 SMP Thu Jun 29 16:05:25 UTC 2017
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to run PowerShell Core scripts from .NET Core applications
Start by creating a new .NET Core console application. Then add a package reference to the PowerShell SDK. At the time of this...
Read more >
PowerShell in .NET Core and Docker
NET Core SDK installed, you can simply run dotnet tool install --global PowerShell. It will now be available with the pwsh command.
Read more >
Use Docker to automate testing of PowerShell Core scripts
Summary: Learn the basics about Docker, and see how to use it for PowerShell Core script testing on different operating systems.
Read more >
Containerize an app with Docker tutorial - .NET
In this tutorial, you'll learn how to containerize a .NET application with Docker. Containers have many features and benefits, such as being ...
Read more >
dotnet clean command - .NET CLI
The dotnet clean command cleans the output of the previous build. It's implemented as an MSBuild target, so the project is evaluated when ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found