【问题标题】:Powershell Loading Binary ModulePowershell 加载二进制模块
【发布时间】:2019-09-08 07:31:43
【问题描述】:

我尝试将几个 powershell 脚本放入模块中。在同一个模块中,我还打算加载一个 c# dll 来生成令牌。 DLL 使用 System.Management.Automation。

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

tokengenerator 文件夹包含用于生成 OAuth2.0 令牌的 dll。如何在同一模块下加载 powershell 模块和 C# cmdlet。但是,当我尝试加载模块时,出现以下错误。

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

【问题讨论】:

  • PowershellTokenGenerator.dll 是 PowerShell 模块吗? Import-Module 仅用于导入 PowerShell 模块,不适用于任何旧的 C# 库。看看Add-Type
  • dll是二进制powershell模块
  • 它是否在您的模块之外加载?即,如果您打开控制台并直接在文件上使用Import-Module,它可以工作吗?此外,DLL 是否编译为与您正在运行的 PowerShell 版本相同的操作系统类型(例如 64 位)

标签: powershell


【解决方案1】:

用途:

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

确保找到相对于脚本模块的 (*.psm1) 位置的DLL,反映在自动变量$PSScriptRoot 中。

相比之下,如果您使用Import-Module ".\...",则会相对于当前位置 (.) 查找DLL,无论它可能是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多