【问题标题】:Loading PowerShell module with nested module giving errors使用嵌套模块加载 PowerShell 模块会出现错误
【发布时间】:2018-09-07 10:21:27
【问题描述】:

我编写了一个自定义 PowerShell 模块。我的自定义模块使用 SharePoint CMD 让。所以在我的 psd1 文件中,我有一个嵌套的模块属性,例如:

NestedModules = @( 'Microsoft.SharePoint.PowerShell',
        '.\Modules\MyModule.psd1')

我来自:PowerShell Modules and SnapIns

当我导入这个模块时,我得到(错误数量的一小部分):

Import-Module : The following error occurred while loading the extended type data file: Error in TypeData
"Microsoft.Office.Server.Search.Administration.SearchServiceApplication": The member ServiceName is already present.
At line:1 char:1
+ Import-Module Test
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : Module_ImportModuleError,Microsoft.PowerShell.Commands.ImportModuleCommand

Import-Module : The following error occurred while loading the extended type data file: Error in TypeData
"Microsoft.Office.Server.Search.Administration.SearchService": The member ProcessIdentity is already present.
At line:1 char:1
+ Import-Module Test
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : Module_ImportModuleError,Microsoft.PowerShell.Commands.ImportModuleCommand

Import-Module : The following error occurred while loading the extended type data file: Error in TypeData
"Microsoft.Office.Server.Search.Administration.SearchService": The member ServiceName is already present.
At line:1 char:1
+ Import-Module Test
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : Module_ImportModuleError,Microsoft.PowerShell.Commands.ImportModuleCommand

Import-Module : The following error occurred while loading the extended type data file: Error in TypeData
"Microsoft.Office.Server.Search.Administration.Keyword": The member ExpiryDate is already present.
At line:1 char:1
+ Import-Module Test
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : Module_ImportModuleError,Microsoft.PowerShell.Commands.ImportModuleCommand

Import-Module : The following error occurred while loading the extended type data file: Error in TypeData

【问题讨论】:

    标签: powershell powershell-module


    【解决方案1】:

    我最终创建了 PowerShell 脚本来加载 SharePoint PSSnapin。

    Begin {
        $SPSnapin = "Microsoft.SharePoint.PowerShell"
    
        Write-Verbose "The GasuniePowerShell module will try to load the $SPSnapin snappin"  -Verbose
    
        try {        
            if (Get-PSSnapin $SPSnapin -ErrorAction SilentlyContinue) {
                Write-Verbose "Microsoft.SharePoint.PowerShell Snappin already registered and loaded" -Verbose
            }
            elseif (Get-PSSnapin $SPSnapin -Registered -ErrorAction SilentlyContinue) {
                Add-PSSnapin $SPSnapin
                Write-Verbose "$SPSnapin Snappin is registered and has been loaded for script operation" -Verbose
            }
            else {
                Write-Warning "$SPSnapin Snappin not installed on this server. SharePoint cmdlets disabled." -Verbose
                Exit                
            }
        }
        catch {
            Write-Warning "There was an issue loading the $SPSnapin Snappin. Exiting function." -Verbose
            Write-Warning $_
            Exit  
    
        }    
    }
    

    在我的 PowerShell 模块清单中,我添加了以下属性:

    ScriptsToProcess = '.\scripts\LoadSharePointPowerShell.ps1'
    

    每当导入模块时,此脚本都会运行并加载 SnapIn。模块重装时不会出现错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多