简体   繁体   English

PowerShell模块和管理单元

[英]PowerShell Modules and SnapIns

I've created several utility modules that all depend upon the Microsoft.SharePoint.PowerShell snapIn. 我创建了几个实用程序模块,它们都依赖于Microsoft.SharePoint.PowerShell snapIn。

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. 在我的计算机上运行时,所有脚本均能正常运行,我的配置文件中没有用于Powershell的任何模块或管理单元以“默认加载”,但是不能保证它们在其他计算机上的配置文件是干净的。

In my module loader .psd1 file i'm using the following 在我的模块加载器.psd1文件中,我正在使用以下内容

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

I use a ps1 file to trigger these module imports that contains the following Import Module command 我使用ps1文件触发这些模块导入,其中包含以下“导入模块”命令

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

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

I know that we can execute PowerShell commands with the -NoProfile argument but this is not a valid solution in our scenario. 我知道我们可以使用-NoProfile参数执行PowerShell命令,但这在我们的方案中不是有效的解决方案。

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

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

My ideal scenario is the following: 我的理想情况如下:

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)

I would switch away from having the PSD1 file load the snappin via NestedModules. 我将不再通过NestedModules使PSD1文件加载卡扣。 Create a CustomModules.psm1 file and set the ModuleToProcess (or RootModule in v3) to "CustomModules.psm1". 创建一个CustomModules.psm1文件,并将ModuleToProcess(或v3中的RootModule)设置为“ CustomModules.psm1”。 Remove the SharePoint dll from NestedModules. 从NestedModules中删除SharePoint dll。 Then add something like this to your CustomModules.psm1 file: 然后将以下内容添加到CustomModules.psm1文件中:

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

you can test the presence of the assembly name 您可以测试程序集名称的存在

$assemblyname = "Microsoft.SharePoint.PowerShell"
if (([appdomain]::currentdomain.getassemblies() | 
      Where {$_ -match $AssemblyName}) -eq $null )
{
  add-pssnapin Add-PSSnapin 'Microsoft.SharePoint.PowerShell'
}

or just load it anyway and ignore the error if it's already loaded: 或者无论如何都直接加载它,如果已经加载,则忽略该错误:

Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM