question

WillPittenger avatar image
0 Votes"
WillPittenger asked WillPittenger answered

Can't import PowerShell module that depends on NuGet library

I have a C# PowerShell module coded for .NET 6.0. It depends on a C# library that started life in the same solution. In that configuration, the PowerShell code imported just fine. But then I decided people might want only the internals of my project and not the PowerShell stuff. So I split them out so the PowerShell was in one solution and the library it depends on in a second one. The second library, let's call it just Core, is now on NuGet and I was able to compile the PowerShell code against it just fine. But when I run import-module against the compiled PowerShell module, it says it can't find the Core library.

What's going on? I tried to import the module from a stand-alone PowerShell prompt outside VS. It was VS 2022 that compiled both projects. My tests are being run on a Windows 10 Pro machine.

dotnet-csharpwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WillPittenger avatar image
0 Votes"
WillPittenger answered WillPittenger published

I realized my description of the error was lousy. So let me explain. I'm getting an error that it can't find Core.dll as shown below in an except from the VS Terminal running PowerShell 7.2.3. Here, the module is named PS.

Mode                 LastWriteTime         Length Name
d-----         5-14-2022   6:38 am                PS
d-----         5-14-2022   5:17 am                runtimes
-a----        10-22-2021   6:40 pm          26224 Microsoft.Win32.SystemEvents.dll
-a----         5-14-2022   6:38 am           4911 PS.deps.json
-a----         5-14-2022   6:38 am          32768 PS.dll
-a----         5-14-2022   6:38 am          11464 PS.pdb
-a----         5-14-2022   6:38 am           9108 PS.xml
-a----         5-14-2022   3:54 am          37888 Core.dll
-a----        10-22-2021   6:49 pm         175216 System.Drawing.Common.dll
-a----          3-7-2019    3:8 pm         354304 System.Management.Automation.dll
-a----        10-22-2021   6:49 pm          25712 System.Windows.Extensions.dll

PS B:\PS\bin\Debug\net6.0-windows> dir .\PS\


    Directory: B:\PS\bin\Debug\net6.0-windows\PS


----                 -------------         ------ ----
-a----         5-14-2022   6:20 am           7386 PS.psd1
-a----        11-17-2021   7:27 pm            184 PS.psm1


PS B:\PS\bin\Debug\net6.0-windows> import-module .\PS\
import-module : The specified module '.\PS\' was not loaded because no valid module file was found in any module directory.
+ import-module .\PS\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (.\PS\:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

PS B:\PS\bin\Debug\net6.0-windows> import-module PS
import-module : The specified module 'PS' was not loaded because no valid module file was found in any module directory.
+ import-module PS
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (PS:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

PS B:\PS\bin\Debug\net6.0-windows> import-module .\PS.dll
import-module : Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
At line:1 char:1
+ import-module .\PS.dll
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand

PS B:\PS\bin\Debug\net6.0-windows> import-module .\Org.WillPittenger.Tools.Sounds.dll
import-module : Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
At line:1 char:1
+ import-module .\Core.dll
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand


Now here is the psd1 contents:

#
# Module manifest for module 'PS'
#
# Generated by: Will Pittenger
#
# Generated on: 11-17-2021 7:27:43 pm
#

@{

# Script module or binary module file associated with this manifest.
RootModule = 'PS.psm1'

# Version number of this module.
ModuleVersion = '1.0.0'

# ID used to uniquely identify this module
GUID = '9b641a82-0da7-4407-8218-b3e7a582284b'

# Author of this module
Author = 'Will Pittenger'

# Company or vendor of this module
CompanyName = ''

# Copyright statement for this module
Copyright = 'Copyright ©2022, . All rights reserved.'

# Description of the functionality provided by this module
Description = 'Plays sounds in a convenient way.'

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.1.19041.1682'

# Name of the Windows PowerShell host required by this module
#PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
#PowerShellHostVersion = ''

# Minimum version of the .NET Framework required by this module
DotNetFrameworkVersion = '6.0'

# Minimum version of the common language runtime (CLR) required by this module
CLRVersion = '4.0.30319.42000'

# Processor architecture (None, X86, Amd64) required by this module
#ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = 
@(
)

# Assemblies that must be loaded prior to importing this module
RequiredAssemblies =
@(
)

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
#ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
#TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
#FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @(
    )

# Functions to export from this module
FunctionsToExport = @(
    '*-*'
    )

# Cmdlets to export from this module
CmdletsToExport = @(
    '*-*'
    )

# Variables to export from this module
VariablesToExport = @(
    )

# Aliases to export from this module
AliasesToExport = @(
    )

# List of all modules packaged with this module.
 ModuleList = @(
    )

# List of all files packaged with this module
FileList = 
@(
  './PS.dll',
  './Core.dll'
)

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

    PSData = @{

        # Tags applied to this module. These help with module discovery in online galleries.
        # Tags = @()

        # A URL to the license for this module.
        LicenseUri = 'https://licenses.nuget.org/LGPL-3.0-or-later'

        # A URL to the main website for this project.
        # ProjectUri = ''

        # A URL to an icon representing this module.
        IconUri = ''

        # ReleaseNotes of this module
        # ReleaseNotes = ''

    } # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
#HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
DefaultCommandPrefix = 'WillPittenger'

}


And this is the psm1:

Push-Location $PSScriptRoot

$PackageRoot = $PSScriptRoot

$LoadingModule = $true

dir *.ps1 | % Name | Resolve-Path | Import-Module 

$LoadingModule = $false

Pop-Location

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello,

and you try with a Manual Package Download?

https://docs.microsoft.com/en-us/powershell/scripting/gallery/how-to/working-with-packages/manual-download?view=powershell-7.2

Bear in mind the following: Manual package download is not intended as a replacement for the Install-Module cmdlet. Downloading the package doesn't install the module or script. Dependencies aren't included in the NuGet package downloaded

Hope this helps with your query!



--If the reply is helpful, please Upvote and Accept as answer--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WillPittenger avatar image
0 Votes"
WillPittenger answered

The PowerShell module isn't on NuGet yet. The NuGet part is the library it depends on. I'm trying to get the PowerShell module to start in the VS 2022 debugger.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.