wuping2004 / doc

1 stars 0 forks source link

Publishing C# cmdlets to the PowerShell Gallery #6

Open wuping2004 opened 4 years ago

wuping2004 commented 4 years ago

Publishing C# cmdlets to the PowerShell Gallery

Publishing C# cmdlets to the PowerShell Gallery


Back to index Matthieu Maitre

PowerShellGet and the PowerShell Gallery are the equivalent of NuGet/NPM/Bower package managers for PowerShell. They come handy to share scripts and modules.

Installing a module, like this demo one for instance, is a one-liner:

Install-Module -Name PowerShellGet-Test-Binary-Module -Scope CurrentUser

Creating and publishing a new module is also relatively straightforward, although not exceptionally well documented.

A simple Write-HelloWorld cmdlet consists in a class deriving from Cmdlet that overrides BeginProcessing to output a string:

using System.Management.Automation;

[Cmdlet(VerbsCommunications.Write, "HelloWorld")]
public class HelloWorldCmdlet : Cmdlet
{
    protected override void BeginProcessing()
    {
        WriteObject("Hello, World!");
    }
}

One trick here is finding the System.Management.Automation reference. On my machine (Windows 10 x64) it could be found at c:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll.

Once compiled, the DLL that can be loaded in PowerShell using Import-Module.

To make it a publishable module requires adding a .psd1 PowerShell manifest akin to NuGet’s .nuspec XML manifest:

@{
    RootModule = 'PowerShellGet-Test-Binary-Module.dll'
    ModuleVersion = '1.0.0.3'
    CmdletsToExport = '*'
    GUID = '95ee4cf1-d508-45a8-9680-203b71453f98'
    DotNetFrameworkVersion = '4.5.1'
    Author = 'Matthieu Maitre'
    Description = 'Example of PowerShellGet Binary Module'
    CompanyName = 'None'
    Copyright = '(c) 2016 Matthieu Maitre. All rights reserved.'
    PrivateData = @{
        PSData = @{
            ProjectUri = 'https://github.com/mmaitre314/PowerShellGet-Test-Binary-Module'
            LicenseUri = 'https://github.com/mmaitre314/PowerShellGet-Test-Binary-Module/blob/master/LICENSE'
            ReleaseNotes = ''
        }
    }
}

Both the DLL and .psd1 file need to be placed in a folder whose name is the package name. Once this is done, the package gets uploaded to the gallery using

Publish-Module -Path .\PowerShellGet-Test-Binary-Module -Repository PSGallery -NuGetApiKey "xxx"

where the API Key comes from https://powershellgallery.com/account.

That is it. For the full source code and some unit-testing tricks see this repo on GitHub. https://mmaitre314.github.io/2016/03/22/publishing-csharp-cmdlets-to-the-powershell-gallery.html

wuping2004 commented 4 years ago

This is likely due to Publish-Module needing to look for a folder with the same name as the module you are looking to publish.

Can you create a new folder called Lazlo.PowerShell.Operations and move all the files thats under the netstandard2 folder and put them into this new folder?

Then once you do that you should be able to publish by running Publish-module to that Lazlo.PowerShell.Operations folder