Add-PsSnapIn 和 Import-Module 有什么区别
Powershell 有两种将其他 cmdlet 导入会话的方法:Add-PsSnapIn 和 Import-Module。关于何时选择其中一种而不是另一种,文档根本不清楚。有人知道这些之间的区别吗?
Powershell has two means for importing additional cmdlets into a session, Add-PsSnapIn and Import-Module. The docs are not at all clear regarding when one would chose one over the other. Does anybody know the difference between these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
模块出现在 PowerShell V2 中。不过,如有必要,V2 仍然可以加载管理单元。最大的区别是模块可以使用 Xcopy 进行部署。无需注册任何东西。要部署管理单元,安装程序必须破解注册表,这需要提升权限。要加载模块,您只需使用 Import-Module 命令即可。
使用模块清单,模块还可以拥有更多元数据,它可以指定最终用户可能需要了解的各种有用信息,包括版本、对其他模块的依赖关系以及哪些 powershell 主机可以运行该模块。
尽管如此,如果您用 C# 或 VB 编写二进制模块,您仍然使用相同的 PSSnapin 基类。
要获取更多信息,请查看
Modules came about in PowerShell V2. V2 can still load Snapins though, if necessary. The big difference is that modules can be deployed using Xcopy. There is no need to register anything. To deploy a Snapin, an installer would have to hack the registry, which would require elevated priveleges. To load a module, you simply have to use the Import-Module command.
Modules can have a lot more metadata using a Module Manifest as well, which can specify all kinds of things that might be useful for the end user to know, including version, dependencies on other modules, and which powershell hosts can run the module.
All that being said, if you are writing a binary module in C# or VB, you still use the same PSSnapin base class.
To get more information, check out
在 v2 中,模块是组织 cmdlet、提供程序、函数、别名和您创建的其他命令的首选方式。您没有安装模块。您只需使用 Import-Module cmdlet 将模块导入 PowerShell 会话即可。
AFAIK,PowerShell 管理单元更像是 v1 方法。 MS 仍有一些团队创建管理单元而不是模块。例如,SharePoint 2010 cmdlet。 PowerShell 管理单元是实现 cmdlet 和提供程序的二进制文件 (.dll)。您需要安装管理单元,然后使用 Add-PSSnapin cmdlet 将管理单元中的 cmdlet 添加到 PowerShell 会话。
In v2, modules are the preferred way organize the cmdlets, providers, functions, aliases, and other commands that you create. You don't install a module. You simply import a module into the PowerShell session using Import-Module cmdlet.
AFAIK, a PowerShell snapin is more of a v1 approach. There are still a few teams at MS creating snapins instead of modules. For example, SharePoint 2010 cmdlets. PowerShell snapins are binaries (.dll) that implement cmdlets and providers. You need to install a snapin and then add the cmdlets in the snapin to a PowerShell session using Add-PSSnapin cmdlet.
这方面的记录很少,所以对我的回答持保留态度。查看 snapin 和模块。简而言之,管理单元“只是”一个 .Net 程序集,而模块可以包含脚本、程序集等。
This is poorly documented, so take my answer with a grain of salt. Take a look at developer documents for snapin and modules. Briefly, snapin is "just" a .Net assebly whilst module can contain scripts, asseblies and more.
PSSnapin 提供了一种通过在受保护目录中安装 DLL 来保护程序集的方法,而模块则可以通过替换文件来运行。
参考:PSSnapin 的 MSDN 链接
PSSnapin provides a way to protect your assemblies by installing DLLs in protected directory as compared to Module which can be played by just replacing files.
Ref: MSDN Link for PSSnapin
Add-PSSnapin
和Import-Module
用于在当前 PowerShell 会话中获取外部第 3 方库(脚本文件/二进制文件/dll)。模块比 PSSnapins 更容易使用。
相对于 PSSnapin 的主要优势模块是,一旦添加 PSSnapin,我们就无法从当前 PowerShell 会话中删除或卸载 PSSnapin。
但可以使用
Remove-Module
从当前 PowerShell 会话中手动删除模块注意:
PS 1.0 版中引入的 PSSnapin 概念和 PS 2.0 版中引入的模块。
参考:-
这个
Add-PSSnapin
andImport-Module
are used to take external 3rd party libraries(Script files/ binary files/ dll) in the current PowerShell session.Modules are little more easier to use than PSSnapins.
The main advantage module over PSSnapin is we can't remove or unload PSSnapin from the current PowerShell session once it is added.
But modules can be removed from the current PowerShell session manually, using
Remove-Module
NOTE:
The concept PSSnapin introduced in PS version 1.0 and module introduced in PS version 2.0.
Ref:-
This