Reputation: 1469
I've create a PowerShell Binary module in .NET Core (TargetFramework is netstandard2.0). When I use Import-Module on a Windows machine the module imports successfully and I'm able to call the cmdlet. When I do the same on a Linux container (I'm using the official Microsoft PowerShell container image - mcr.microsoft.com/powershell:ubuntu-18.04) the module imports without error but I'm unable to call the cmdlet. If I run Get-Module there are no Exported Commands listed on the Linux container but there are on my Windows machine.
The project for the PowerShell module was initialised using dotnet new powershell
. I've tried adding a psd1 manifest file, I've also initialised a new project and repeated the steps above. The same problem occurs with the base project created with dotnet new.
I'm running dotnet verion 2.2.103. Running dotnet new powershell
creates a project with a csproj file that looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>demo_psmodule</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0-preview-06">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
The sample project has one class:
[Cmdlet(VerbsDiagnostic.Test,"SampleCmdlet")]
[OutputType(typeof(FavoriteStuff))]
public class TestSampleCmdletCommand : PSCmdlet
{
// cmdlet parameters and code
}
I expect the module to export the cmdlet on a linux container as it does on a Windows machine. What do I need to change to achieve this?
Update The problem is with the dockerfile I was using to build the container:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /src
COPY [ "demo-psmodule.csproj", "src/" ]
RUN dotnet restore "src/demo-psmodule.csproj"
COPY . .
RUN dotnet publish "src/demo-psmodule.csproj" -c Release -o /app/demo_psmodule
FROM mcr.microsoft.com/powershell:ubuntu-18.04 AS app
WORKDIR /app
COPY --from=build /app .
Upvotes: 1
Views: 168
Reputation: 1469
The dockerfile wasn't copying the csproj file to the same location as the source code files. This built successfully, but produced an empty dll. That imported into PowerShell without any errors either but obviously contained no cmdlets to export. The corrected (and simplified) dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /src
COPY demo-psmodule.csproj .
RUN dotnet restore demo-psmodule.csproj
COPY . .
RUN dotnet publish demo-psmodule.csproj -c Release -o /app/demo_psmodule
FROM mcr.microsoft.com/powershell:ubuntu-18.04 AS app
WORKDIR /app
COPY --from=build /app .
Upvotes: 1