17

What I want

I want to publish number of PowerShell scripts as Nuget package to be used on build systems.

I want to use PowerShellGet to do installation work for me and version management.

I don't want those scripts to be part of any Visual Studio solution, but as standalone scripts.

Usage scenario

On any system, with configured Nuget provider user executes:

Install-Module MyModule

From that moment all exports from that module permanently available for this user. Also user can call that command again to update version of those scripts.

What I've done

You can find current state of package here: GitHub

  1. I've added and configured Nuget provider to our local Nuget server

    To do this call Get-PackageProvider -Name NuGet -ForceBootstrap and Set-PSRepository -Name My_Nuget_Repo -SourceLocation http://my-nuget/api -InstallationPolicy Trusted

  2. Created proper module, which can be imported locally by Import-Module

  3. Created and published Nuget package with that module

Problem

I can install that package by Install-Module cmdlet and I can see it later in Get-InstalledModule list.

But, no functions are available.

Also, no matter what, but Install-Module not calling any of scripts from my package:

  • Not calling ScriptsToProcess from MyModule.psd1
  • Not calling Install.ps1 from tools folder
  • Not calling Init.ps1 from tools folder
  • Cmdlets exported by module not available and module can't be imported by Import-Module

(Same package works properly when installed from Visual Studios Install-Package MyModule, scripts are called, PowerShell module is imported).

Investigation

Since PowerShellGet is based on OneGet it seems that problem is in Install-Package cmdlet (which is called inside Install-Module cmdlet).

When I'm executing Install-Package MyModule from Visual Studio Install.ps1 and Init.ps1 are called. But same command from pure PowerShell doing nothing.

Community
  • 1
  • 1
Anton
  • 10,890
  • 8
  • 45
  • 54
  • You mentioned Install.ps1 and Init.ps1 scripts. My understanding is that those are specific to NuGet as used in Visual Studio for .NET. Did you manage to get those working with PowerShellGet? – mason Feb 01 '17 at 16:28
  • @mason Unfortunately no, from this point of view it is inconsistent. But you still can probably execute your code on install, import and uninstall operations via PS module features. Let me check. – Anton Feb 01 '17 at 20:02
  • @mason it seems it is impossible to run some code on install or uninstall, according to [How to Write a PowerShell Module Manifest](https://msdn.microsoft.com/en-us/library/dd878337.aspx). You can still do something on module import, of course. – Anton Feb 02 '17 at 17:44

1 Answers1

34

After long reverse engineering I've found the root cause

Technical reason

Magical tag PSModule has to be added to <Tags> in nuspec file.

Real reason

You shouldn't create nuspec file and pack nuget package manually at all. Use Publish-Module cmdlet instead.

How to do it properly

I've updated powershellget-module GitHub with:

  • Example of minimal module which can be published
  • A way how to use local folder as Nuget feed
  • Publishing, installation and usage of that module
  • Reference script with no dependencies which does it all locally, so you can study it

Check it out.

Anton
  • 10,890
  • 8
  • 45
  • 54
  • Thanks for the working example on github, I love to begin with "Hello World" – fbehrens Feb 27 '16 at 17:41
  • @anton-purin This is great. Have you had any experience of Powershell Binary Modules? I have a Powershell Binary dll that consumes a project refference and some nuget packages. It's a bit of a beast getting all the required binaries in the right place and I haven't managed to get Install-Package working with the nuget package yet. Will look at this nuget tag now. – Craig.C Jan 08 '19 at 20:49
  • 1
    The examples seem to no longer be available in the links from the GitHub page. – Jessie Dec 08 '22 at 17:34
  • @Jessie sorry, moved it to the new profile and forgot it was mentioned here seven years ago :) New link is https://github.com/apurin/powershellget-module – Anton Dec 09 '22 at 18:03