使用Import Module Commandlet导入PowerShell模块

Powershell使用这些模块来提供不同的命令、函数、变量和别名。甚至大多数模块都是内置的,可以直接访问,有些模块应该在使用前导入。在不同的PowerShell版本上,如PowerShell 1.0、PowerShell 2.0和PowerShell 3.0以及更高版本,导入模块和用法都有一些变化。在本教程中,我们将学习如何在PowerShell中导入模块。

null

在PowerShell 1.0中导入模块

在PowerShell版本1.0中没有模块。这意味着没有与导入模块相关的特定命令。相反,这些模块在PowerShell1.0版中实现为管理单元。为了使用管理单元内容,应该注册管理单元。

在PowerShell 2.0中导入模块

在PowerShell 2.0中,开始使用模块和Commondlet Import-Module 提供。这些模块可以使用它们的名称作为参数导入到commondlet导入模块。在中搜索模块 PSModulePath 可变位置。模块可能有扩展 *.psd1 对于模块清单, *.psm1 脚本模块, *.dll 对于二进制模块文件。在下面的示例中,我们导入名为 MyCustomModule .

Import-Module MyCustomModule
Import-Module MyCustomModule
Import-Module MyCustomModule

如果模块不在PSModulePath下,我们可以通过指定其路径来导入模块。这意味着我们可以从默认情况下不搜索的自定义位置导入模块。选择 -Name 用于指定模块的路径和模块的名称。或者,选择 -Verbose 可用于打印有关加载操作的详细信息。

Import-Module -Name "C:MyModulesMyCustomModule" -Verbose
Import-Module -Name "C:MyModulesMyCustomModule" -Verbose
Import-Module -Name "C:MyModulesMyCustomModule" -Verbose

在PowerShell 3.0及更高版本中导入模块

PowerShell版本3.0为模块导入提供了一些额外的功能。如果其中一个函数、变量在PowerShell中使用,则如果模块位于PSModulePath下,则会自动导入相关模块。如果模块不在PSModulePath中,还可以使用显式指定具有选项名称(如PowerShell 2.0版)的模块来加载模块。

相关文章: 如何在Windows MS-DOS(cmd.exe)中使用ping?

列出可用模块

PowerShell模块存储在以下位置: C:Program FilesWindowsPowerShellModules C:Windowssystem32WindowsPowerShellv1.0Modules . 这些模块可供使用。可用的PowerShell模块可以与 Get-Module 命令 -ListAvailable 选项。

Get-Module -ListAvailable
Get-Module -ListAvailable
Get-Module -ListAvailable
图片[1]-使用Import Module Commandlet导入PowerShell模块-yiteyi-C++库
列出可用模块

导入可用模块

还可以使用commandlet导入模块显式导入可用模块。使用commandlet Get模块列出并重定向的可用模块。

Get-Module -ListAvailable | Import-Module
Get-Module -ListAvailable | Import-Module
Get-Module -ListAvailable | Import-Module
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享