来自高级 (cmdlet) 功能的 PowerShell Add-PSSnapIn
我想创建一个带有 cmdlet 函数的高级模块,该函数执行一些逻辑并添加一些 pssnapins。这是代码:
function Add-DefaultSnapIns
{
[CmdletBinding()]
param()
begin {}
process {
# ...
Add-PsSnapIn SnapInName
}
end {}
}
export-module -function Add-DefaultSnapIns
如果我从任何点(例如,powershell 提示符)调用该函数,操作都会成功,但管理单元在函数范围之外不可用。该管理单元似乎已注册,但其任何功能均未导出到全局范围。我该如何解决呢?
I would like to create an advancedmodule with a cmdlet function which performs some logic and adds some pssnapins. This is the code:
function Add-DefaultSnapIns
{
[CmdletBinding()]
param()
begin {}
process {
# ...
Add-PsSnapIn SnapInName
}
end {}
}
export-module -function Add-DefaultSnapIns
If I invoke the function from any point (for instance, a powershell prompt), the operation succeeds, but the snapin is not available outside of the scope of the function. The snap-in appears registered, but none of its functions have been exported to the global scope. How could I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个想法是,模块是独立的,除了它们导出的 cmdlet、函数和别名之外,不会将太多“东西”溢出到全局会话空间中。最好自己添加管理单元作为模块初始化的一部分,然后通过 Export-ModuleMember 导出这些管理单元的 cmdlet。
Well the idea is that Modules are self-contained and don't spill too much of their "stuff" into the global session space except the cmdlets, functions and aliases they export. It might be better to add the snapins yourself as part of the module initialization and then export those snapins' cmdlets via Export-ModuleMember.