【发布时间】:2013-01-30 21:38:55
【问题描述】:
我创建了几个实用程序模块,它们都依赖于 Microsoft.SharePoint.PowerShell 管理单元。
当在我的机器上运行时,脚本全部正确运行,我的配置文件中没有任何模块或管理单元用于 powershell “默认加载”,但是在其他机器上,它们的配置文件不能保证是干净的。
在我的模块加载器 .psd1 文件中,我正在使用以下内容
NestedModules = @( 'Microsoft.SharePoint.PowerShell',
'.\Modules\CustomModule.psd1')
我使用 ps1 文件来触发这些包含以下 Import Module 命令的模块导入
Import-Module -Name "$dir\.\CustomModules.psd1" -Global -Force -PassThru
如果我以这种方式运行,我会在配置文件包含 Microsoft.SharePoint.PowerShell 管理单元的计算机上出现名称冲突错误,因为该管理单元已被加载。
我知道我们可以使用 -NoProfile 参数执行 PowerShell 命令,但这在我们的场景中不是有效的解决方案。
我还尝试从 NestedModules 部分删除管理单元并在导入模块之前在 .ps1 文件中运行以下命令,但管理单元未显示为已加载,因此模块导入失败。
if ((Get-PSSnapin | ? {$_.Name -eq 'Microsoft.SharePoint.PowerShell'}) -eq $null)
{
Write-Host "Adding PSSnapin 'Microsoft.SharePoint.PowerShell'"
Add-PSSnapin 'Microsoft.SharePoint.PowerShell' }
如何重新设计此解决方案以确保管理单元能够正确加载,并且无论用户配置文件如何,模块都能成功导入?
我的理想情况如下:
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)
【问题讨论】:
-
当我将它添加到我的嵌套模块“Microsoft.SharePoint.PowerShell”时,我会立即收到错误消息。我有空的个人资料。这行得通吗?
标签: powershell module pssnapin