PowerShell 5类:类型所需的导入模块

时间:2018-02-15 14:07:43

标签: powershell class powershell-v5.0 powershell-module import-module

我写了一篇PowerShell 5类:

class ConnectionManager
{

  # Public Properties
  static [String] $SiteUrl
  static [System.Management.Automation.PSCredential] $Credentials
  static [SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection] $Connection
  ...
}

SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection”类型来自自定义(已安装的模块),名为“SharePointPnPPowerShell2016”

我的班级在另一个名为“connection.manager.psm1”的模块/文件中。

当我加载此模块以使用此类时,它会返回以下错误:

> Import-Module connection.manager.psm1
At connection.manager.psm1:6 char:11
+   static [SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection]  ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type
[SharePointPnP.PowerShell.Commands.Base.SPOnlineConnection].

当我在加载模块之前在PowerShell会话中手动加载(PNP)模块时,它已正确加载,我可以使用它。

但我不想在使用模块之前总是先手动加载其他模块。我尝试通过添加:

直接在我的模块中导入PNP模块
Import-Module "SharePointPnPPowerShell2016"

在开始之前,在类声明之前,但它什么都没有改变,仍然出现错误“无法找到类型”。

任何想法如何正确地做到这一点?

2 个答案:

答案 0 :(得分:2)

我认为您可以使用module manifest

解决此问题

有一个"必需的模块"和#34;所需的组装"您可以使用的部分。这应该在您加载保存类的自定义模块时处理加载需求(只要它们已安装)。

答案 1 :(得分:0)

如果您已在模块中声明了一个类,则在spring.profiles.active=profile1时无法使用它;只带来cmdlet,函数和别名。相反,您的脚本应该spring.profiles.active=profile2模块;这将带来班级以及其他出口物品。

(我实际上误读了这个问题;这不能解决querent的特定问题 - 但是对于不使用其他模块的类的类,这将允许从模块中导入类。这个问题已经找到了一个已知的问题在PowerShell中;有关详细信息,请参阅注释。)