PowerShell模块和管理单元 [英] PowerShell Modules and SnapIns

查看:112
本文介绍了PowerShell模块和管理单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了几个依赖于Microsoft.SharePoint.PowerShell snapIn的实用程序模块.

I've created several utility modules that all depend upon the Microsoft.SharePoint.PowerShell snapIn.

在我的计算机上运行时,所有脚本均能正常运行,我的配置文件中没有任何模块或管理单元以使Powershell能够默认加载",但是在其他计算机上,不能保证它们配置的文件是干净的.

When run on my machine the scripts all run correctly, I don't have any modules or snapins in my profile for powershell to "default load", however on other machines they profile cannot be guaranteed to be clean.

在我的模块加载器.psd1文件中,我正在使用以下

In my module loader .psd1 file i'm using the following

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

我使用ps1文件触发包含以下导入模块"命令的这些模块导入

I use a ps1 file to trigger these module imports that contains the following Import Module command

Import-Module -Name "$dir\.\CustomModules.psd1" -Global -Force -PassThru

如果以这种方式运行事情,由于配置文件已被加载,则在配置文件包含Microsoft.SharePoint.PowerShell管理单元的计算机上会收到名称冲突错误.

If I run things this way I get name conflict errors on the machines where the profile contains the Microsoft.SharePoint.PowerShell snapin, due to the snapin already being loaded.

我知道我们可以使用-NoProfile参数执行PowerShell命令,但这在我们的方案中不是有效的解决方案.

I know that we can execute PowerShell commands with the -NoProfile argument but this is not a valid solution in our scenario.

我还尝试了从NestedModules部分中删除管理单元,并在导入模块之前在.ps1文件中运行以下命令,但该管理单元未显示为正在加载,因此模块导入失败.

I have also tried removing the snapin from the NestedModules section and running the following, in the .ps1 file before the modules get imported but the SnapIn does not show up as being loaded and therefore the module import fails.

if ((Get-PSSnapin | ? {$_.Name -eq 'Microsoft.SharePoint.PowerShell'}) -eq $null)
{
    Write-Host "Adding PSSnapin 'Microsoft.SharePoint.PowerShell'"
    Add-PSSnapin 'Microsoft.SharePoint.PowerShell'  }

如何重新设计此解决方案,以确保管理单元能够正确加载,并且无论用户配置文件如何,模块都能成功导入?

How can I re-work this solution to ensure that the snapins will load appropriately and the modules will import successfully regardless of the user profile?

我的理想情况如下:

Task.ps1   (The custom task me or other devs are trying to achieve)
     > Calls ImportModules.ps1   (Hides the importing logic of modules and snap-ins)
           > Imports CustomModules.psd1  (Imports all modules, functions, global vars into the runspace)

推荐答案

我将放弃让PSD1文件通过NestedModules加载snappin.创建一个CustomModules.psm1文件,并将ModuleToProcess(或v3中的RootModule)设置为"CustomModules.psm1".从NestedModules中删除SharePoint dll.然后将类似这样的内容添加到您的CustomModules.psm1文件中:

I would switch away from having the PSD1 file load the snappin via NestedModules. Create a CustomModules.psm1 file and set the ModuleToProcess (or RootModule in v3) to "CustomModules.psm1". Remove the SharePoint dll from NestedModules. Then add something like this to your CustomModules.psm1 file:

function AddSnapin
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $Name
    )

    Process {
        foreach ($n in $name)
        {
            if (!(Get-PSSnapin $n -ErrorAction SilentlyContinue)) {
                Add-PSSnapin $n
            }
            else {
                Write-Verbose "Skipping $n, already loaded."
            }
        }
    }
}

AddSnapin Microsoft.SharePoint.PowerShell -Verbose

这篇关于PowerShell模块和管理单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆