I've developed a binary cmdlet using C#. The project's TargetFramework is netstandard2.0 and the assembly (the dll) is published as a NuGet package. This assembly and thus the NuGet package has a few dependencies (i.e. other NuGets).
What is the best way to install my binary cmdlet locally on a computer using PowerShell, so my cmdlet becomes available on that computer to be called from PowerShell scripts. I.e., can I install the NuGet with a Powershell command such that my DLL and its dependencies become available to PowerShell?
CodePudding user response:
As of PowerShell 7.2, there is no support for direct installation of NuGet packages.
- GitHub issue #6724 asks for such support.
Your options are:
If feasible, repackage your binary cmdlet and its dependencies as a PowerShell module, which can then be installed via
Import-Module
:- A PowerShell module is typically a directory named for the module, which contains a module manifest file (
*.psd1
) with metadata about the module alongside the files that make up the implementation of the module; in the case of a module wrapping a cmdlet DLL, the manifest'sRootModule
entry references that DLL, and theRequiredAssemblies
entry references the dependencies. - When
Publish-Module
is called to publish a module, it is packaged as a NuGet package with a custom structure thatImport-Module
understands; typically, PowerShell modules are published to the PowerShell Gallery. - Perhaps this is a viable starting point: Creating a new module
- A PowerShell module is typically a directory named for the module, which contains a module manifest file (
Otherwise, use an aux. .NET SDK project to install your NuGet package, which is a nontrivial process outlined in this answer.
- This MIT-licensed Gist contains helper function
Add-NuGetType
, which automates the process and additionally supports native library dependencies in PowerShell (Core) 7 , as discussed in this answer; however, note that the latter doesn't offer strict version control, and therefore shouldn't be used in production.
- This MIT-licensed Gist contains helper function