2

I have few powershell script which I am trying to put into module. In the same module I also intend to load a c# dll for token generation. DLL uses System.Management.Automation.

enter image description here

#content of asr.psm1
Import-Module ".\tokengenerator\PowershellTokenGenerator.dll"
Get-ChildItem $psscriptroot\*.ps1 -Recurse | ForEach-Object { . $_.FullName }

The folder tokengenerator includes dll to generate OAuth2.0 token. How can I load powershell module and C# cmdlet under the same module. However, when I am trying to load the module I get the below error.

Import-Module D:\repo\src\aadsr\setup\asr.psm1

Import-Module : The specified module '.\tokengenerator\PowershellTokenGenerator.dll' was not loaded because no valid module file was found in any module directory. At D:\repo\src\aadsr\setup\asr.psm1:1 char:1
+ Import-Module ".\tokengenerator\PowershellTokenGenerator.dll"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (.\tokengenerato...enGenerator.dll:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
3
  • 1
    Is PowershellTokenGenerator.dll a PowerShell module? Import-Module is only for importing PowerShell modules, not any old C# library. Take a look at Add-Type. Commented Sep 7, 2019 at 15:50
  • dll is binary powershell module Commented Sep 7, 2019 at 15:52
  • Does it load outside of your module? i.e. if you open the console and just use Import-Module directly on the file, does it work? Also, is the DLL compiled for the same OS type as the version of PowerShell you are running (e.g. 64-bit) Commented Sep 7, 2019 at 15:58

1 Answer 1

4

Use:

Import-Module "$PSScriptRoot\tokengenerator\PowershellTokenGenerator.dll"

to ensure that the DLL is found relative to the script module's (*.psm1) location, reflected in automatic variable $PSScriptRoot.

By contrast, if you use Import-Module ".\...", the DLL is looked for relative to the current location (.), whatever it may be.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.