gpt4 book ai didi

PowerShell 5 类 : Import module needed for type

转载 作者:行者123 更新时间:2023-12-03 12:40:00 26 4
gpt4 key购买 nike

我编写了一个 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 session 中手动加载 (PNP) 模块时,它已正确加载并且我可以使用它。

但我不想总是在使用我的模块之前必须先手动加载另一个模块。我尝试通过添加以下内容直接在我的模块中导入 PNP 模块:

Import-Module "SharePointPnPPowerShell2016"

一开始,在类声明之前,但没有任何改变,仍然出现“无法找到类型”的错误。

关于如何正确执行此操作的任何想法?

最佳答案

我认为您可以使用 module manifest 解决此问题

您可以使用“必需模块”和“必需程序集”部分。当您加载包含该类的自定义模块时,这应该会处理加载需求(只要它们已安装)。

关于PowerShell 5 类 : Import module needed for type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48809209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com