Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I write PowerShell binary cmdlet with .NET Core?

I'm trying to create a basic PowerShell Module with a binary Cmdlet internals, cause writing things in PowerShell only doesn't look as convenient as in C#.

Following this guide, it looks like I have to:

  • add Microsoft.PowerShell.SDK to my project.json
  • mark my cmdlet class with required attributes
  • write manifest file, with RootModule, targeting my .dll
  • put that .dll nearby manifest
  • put both under PSModulePath

but, when I'm trying to Import-Module, PowerShell core complains on missing runtime:

Import-Module : Could not load file or assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system
cannot find the file specified.
At line:1 char:1 

Am I doing something wrong, or such tricky things are not supported yet as well?

like image 660
brutallord Avatar asked Oct 06 '16 20:10

brutallord


People also ask

How do I run a PowerShell script in .NET core?

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.

How do I run a cmdlet in PowerShell?

Run an old-fashioned command line (cmd.exe), type powershell and execute. Or, you can hit the PowerShell icon on the taskbar. Either way, you'll get a ready-to-use Windows PowerShell console. Use “Get-Help” cmdlet from before as a starting point for your journey.

How do PowerShell cmdlets work?

A cmdlet is a lightweight command that is used in the PowerShell environment. The PowerShell runtime invokes these cmdlets within the context of automation scripts that are provided at the command line. The PowerShell runtime also invokes them programmatically through PowerShell APIs.


2 Answers

This gets a lot easier with .NET Core 2.0 SDK and Visual Studio 2017 Update 15.3 (or higher). If you don't have VS, you can do this from the command line with the .NET Core 2.0 SDK.

The important bit is to add the PowerShellStandard.Library 3.0.0-preview-01 (or higher) NuGet package to your project file (.csproj).

Here is a simple command line example:

cd $home
dotnet new classlib --name psmodule
cd .\psmodule
dotnet add package PowerShellStandard.Library --version 3.0.0-preview-01
Remove-Item .\Class1.cs
@'
using System.Management.Automation;

namespace PSCmdletExample
{
    [Cmdlet("Get", "Foo")]
    public class GetFooCommand : PSCmdlet
    {
        [Parameter]
        public string Name { get; set; } = string.Empty;

        protected override void EndProcessing()
        {
            this.WriteObject("Foo is " + this.Name);
            base.EndProcessing();
        }
    }
}
'@ | Out-File GetFooCommand.cs -Encoding UTF8

dotnet build
cd .\bin\Debug\netstandard2.0\
ipmo .\psmodule.dll
get-foo

To get this same command to run in Windows PowerShell 5.1 requires a bit more work. You have to execute the following before the command will work:

Add-Type -Path "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0\netstandard.dll"
like image 189
Keith Hill Avatar answered Sep 28 '22 16:09

Keith Hill


For netcore, there is a new template for powershell that you can install and use then you can modify the c# code.

  • Installing the PowerShell Standard Module Template

$ dotnet new -i Microsoft.PowerShell.Standard.Module.Template
  • Creating a New Module Project in a new folder
$ dotnet new psmodule
  • Building the Module
dotnet build

For more details read doc

like image 29
M.Hassan Avatar answered Sep 28 '22 17:09

M.Hassan