【问题标题】:Adding a snapin from a module not working like expected从无法按预期工作的模块添加管理单元
【发布时间】:2018-09-06 20:41:19
【问题描述】:

我有以下名为 module.psm1 的 PowerShell 模块。这是一个简化的例子。我正在针对 SharePoint 2013 执行操作,因此我需要模块中的 SharePoint 管理单元

function Test() {
    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
        Write-Verbose "Adding" -Verbose
        Add-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose
    }
    else {
        Write-Verbose "Already loaed" -Verbose
    }

    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -ne $null) {
        Write-Verbose "Removing" -Verbose
        #Remove-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose
    }
    else {
        Write-Verbose "Already removed" -Verbose
    }

    Get-PSSnapin "Microsoft.SharePoint.PowerShell"

    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null) {
        Write-Verbose "Adding" -Verbose
        Add-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose
    }
    else { 
        Write-Verbose "Already loaded" -verbose
    }
}

Export-ModuleMember -Function 'Test'

在我的 moduletest.ps1 中,我调用了 Test 和相同的逻辑

Import-Module "$PSScriptRoot\module.psm1" -Force

Test

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Write-Verbose "Adding" -Verbose
    Add-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose
}
else {
    Write-Verbose "Already loaed" -Verbose
}

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -ne $null) {
    Write-Verbose "Removing" -Verbose
    Remove-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose
}
else {
    Write-Verbose "Already removed" -Verbose
}

Get-PSSnapin "Microsoft.SharePoint.PowerShell"

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null) {
    Write-Verbose "Adding" -Verbose
    Add-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose
}
else { 
    Write-Verbose "Already loaded" -verbose
}

当我从模块运行函数测试时,输出是:

VERBOSE: Adding
VERBOSE: Removing
VERBOSE: Performing the operation "Remove-PSSnapin" on target "Microsoft.SharePoint.PowerShell".
VERBOSE: Already loaded

所以在模块中,当我以某种方式移除 SNapIn 时,它并没有真正消失。 当我直接从 ps1 文件中运行代码时,我得到:

VERBOSE: Adding
VERBOSE: Removing
VERBOSE: Performing the operation "Remove-PSSnapin" on target "Microsoft.SharePoint.PowerShell".
VERBOSE: Adding

在我的 ps1 文件中,Remove 实际上完全删除了 SnapIn。这是正常行为吗?我看到其他 SnapIn 的行为相同。

我的另一个问题是:

当我从控制台导入模块并从我的模块加载管理单元并从控制台中的管理单元执行命令时,将无法识别任何 cmdlet。当我在模块中加载管理单元时,是在不同的范围或上下文中完成的吗?

【问题讨论】:

  • 您是直接在 SharePoint 服务器上执行此操作,还是通过显式或隐式远程处理?您使用的是什么编辑器/外壳。我见过加载 SharePoint 或 Exchange 模块的时候,尤其是在 ISE 中,直到您点击命令选项卡中的刷新按钮后,cmdlet 才会显示。使用控制台主机时这不是问题,我没有尝试过使用 VSCode。
  • 我在 SharePoint 服务器上以常规 PowerShell 控制台运行此表单。在模块中与直接在 ps1 中加载其他管理单元时,我看到相同的行为
  • 我观察到与 SharePoint 2016 相同的问题。加载模块,添加管理单元,调用模块中删除管理单元的函数,现在无法再添加/删除它。添加时会出现An item with the same key has already been added. 中断异常,尝试删除管理单元时会出现No Windows PowerShell snap-ins matching the pattern 'microsoft.sharepoint.powershell' were found. 删除模块内的管理单元时会弄乱它。

标签: powershell powershell-module pssnapin


【解决方案1】:

我认为你运行它的方式是你调用了.\moduletest.ps1,你描述了你在module.psm1 中运行Test 函数得到的输出,然后是你从其中的代码行中得到的输出moduletest.ps1 之后。这对描述您的问题更有帮助,以便重现该问题。起初,我不确定您是说您是在 ISA 中单独运行文件中的行,还是在命令窗口中运行行,或者两者都运行,以及哪些行在哪里。

如果您在脚本上下文中删除管理单元,如果您想在该脚本或会话中再次使用它,显然需要将其添加回同一脚本中 - 它确实会被删除。如果您在命令行执行一些命令,这些将在与运行之前调用的脚本相同的上下文/会话中完成。例如。如果您在名为mycode.ps1 的文件中包含$myVariable = "Sushi" 行,并且您在命令行中执行了.\mycode.ps1,然后您随后在命令行中执行了Write-Host $myVariable,它将打印Sushi

当您运行您的 ps1 脚本行时,它可以删除一个管理单元,而且显然它可以快速完成。当您尝试从模块中的函数中删除管理单元时(就像您在 module.psm1 中一样),它显然仍在发生,但发生速度太慢而无法重新添加(重新添加失败,因为它认为它仍然存在),因为您说在删除/重新加载后启动基于管理单元的命令时,这些命令会失败。我认为该模块确实正在删除管理单元,但只是没有向您的脚本报告它在检查时被删除,以便能够重新添加它。如果您在模块的 Test 函数期间在删除和尝试重新添加可能 100-200 毫秒之间添加延迟,它可能会成功地重新添加它:

Start-Sleep -m 200

不过,您通常甚至不需要删除 snapin - 当会话(窗口)关闭/脚本结束时,它会自行删除。如果您加载已加载的管理单元,那也没关系 - 您应该能够继续加载命令。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 2023-03-17
    • 2020-12-25
    • 1970-01-01
    • 2011-10-16
    • 2019-06-26
    • 1970-01-01
    相关资源
    最近更新 更多