当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 导入模块 | PowerShell 导入模块的主要示例

作者:精品下载站 日期:2024-12-14 04:49:55 浏览:14 分类:玩电脑

PowerShell 导入模块 | PowerShell 导入模块的主要示例


[玩转系统] PowerShell 导入模块 | PowerShell 导入模块的主要示例

PowerShell导入模块简介

模块是作为包组合在一起的 cmdlet、变量、函数和工作流的集合。模块首次在 PowerShell 版本 2 中引入。模块通常存储在以下两个主要位置。在本主题中,我们将了解 PowerShell 导入模块。

  • %windir%\system32\WindowsPowerShell\v1.0\Modules 这是系统中任何用户都可以使用的系统范围模块的位置。
  • %USERPROFILE%\Documents\WindowsPowerShell\Modules

每个模块都有一个保存它的专用文件夹。它还包含一个名为 Module Manifest 的 psd1 文件。清单文件包含模块的设置,例如 PowerShell 版本、作者和其他设置。可以通过运行 Get-Module -ListAvailable cmdlet 来获取可用模块的列表。

导入模块

可以使用 Import-Module cmdlet 在会话中导入模块。对于要导入的模块,它必须存在于系统或远程服务器上。从 PowerShell 3.0 开始,首次运行已安装模块中的任何 cmdlet 时,会自动导入模块。 $PSModuleAutoloadingPreference 首选项变量用于启用或禁用模块的自动导入。 Import-Module cmdlet 默认导入所有成员,这可以通过使用别名或变量参数来限制。

导入模块的语法

以下是 Import-Module Cmdlet 的语法

[玩转系统] PowerShell 导入模块 | PowerShell 导入模块的主要示例

参数

有许多与 Import-Module Cmdlet 相关的参数,下面将讨论一些常用的参数。

1. 别名

这表示将从模块导入当前会话的别名列表。某些模块默认会自动导出选定的别名。它的类型是字符串,默认值是none。它不接受管道输入和通配符。它是一个可选参数。

2. 姓名

这是一个强制参数。这表示要导入的模块的名称。该名称可以表示模块或模块内的文件,例如 dll 或 .ps1 文件。始终使用模块名称是明智的做法。它的类型是字符串。默认值为无。它接受管道输入和通配符。

3.前缀

这指定将添加到导入的模块成员的名称中的前缀。这会影响当前会话中的模块成员。它的类型是字符串,默认值是none。它不接受管道输入和通配符。

4. 所需版本

它表示需要导入的模块的版本,如果指定的版本不可用,则会抛出错误。它的类型是version,默认值是none。它不接受管道输入和通配符。

5.范围

它表示必须导入模块的范围。它可以是全局的或局部的。在全局中,它可用于会话中的所有命令,而在本地中,它仅适用于当前范围。它的类型是字符串,默认值是none。它不接受管道输入和通配符。

6. 变量

它表示需要从模块导入到当前会话的变量列表。 它的类型是字符串,默认值是none。它不接受管道输入,但接受通配符。

示例

以下是下面提到的 PowerShell 导入模块的以下示例

例子#1

导入模块并获取模块中的命令

输入:

Write-Host "Welcome to Import Module Tutorial" -ForegroundColor Green
Write-Host "List of modules available in the current session are below" -ForegroundColor Green
Get-Module -All
Write-Host "Importing Diagnostics Module" -ForegroundColor Green
Import-Module -Name PSDiagnostics
Write-Host "Commands available in PSDiagnostics are follows" -ForegroundColor Green
Get-Command -Module PSDiagnostics -TotalCount 1

输出:

[玩转系统] PowerShell 导入模块 | PowerShell 导入模块的主要示例

[玩转系统] PowerShell 导入模块 | PowerShell 导入模块的主要示例

例子#2

导入模块和删除模块以及使用 verbose:

verbose 参数用于列出正在导入的模块中可用的各种脚本、函数、变量和工作流程。

输入:

Write-Host "Welcome to verbose and removal of module tutorial" -ForegroundColor Green
Write-Host "Importing Diagnostics Module with verbose parameter" -ForegroundColor Green
Import-Module -Name PSDiagnostics -Verbose
Write-Host "Removing the module from the current session" -ForegroundColor Green
Remove-Module -Name PSDiagnostics
Write-Host "Successfully remove the Module"

输出:

[玩转系统] PowerShell 导入模块 | PowerShell 导入模块的主要示例

例子#3

限制导入的成员:

如上所述,可以限制模块成员的导入。一个模块可以有 100 个命令和函数。当只使用其中少数时,不建议导入所有它们。只导入所需的成员比导入全部要好。以下示例展示了如何限制成员。

输入:

Write-Host "Welcome to the tutorial of restricting cmdlets from being imported" -ForegroundColor Green
Write-Host "The following are the available cmdlets in the PSDiagnostics Module" -ForegroundColor Green
(Get-Module PSDiagnostics).ExportedCommands
#mention only start and stop to be imported
Write-Host "Lets see how to import only start the trace and stop trace" -ForegroundColor Green
Import-Module PSDiagnostics -Function Start-Trace , Stop-Trace
Write-Host "only stop and start trace are imported" -ForegroundColor Green
Write-Host "imported cmdlets are as below" -ForegroundColor Green
Get-Command -Module PSDiagnostics

输出:

[玩转系统] PowerShell 导入模块 | PowerShell 导入模块的主要示例

例子#4

重新导入模块,添加前缀并仅检索模块的指定版本:

输入:

Write-Host "welcome to prefix tutorial" -ForegroundColor Green
Write-Host "Importing PSDiagnostics for first time" -ForegroundColor Green
Import-Module PSDiagnostics
Write-Host "Module Imported"
Write-Host "Importing PSDiagnostics for second  time with prefix" -ForegroundColor Green
Import-Module PSDiagnostics -Force -Prefix Px
Write-Host "List of commands with prefix px" -ForegroundColor Green
Get-Command -Module PSDiagnostics
Write-Host "Importing PowerShell get module with 3.0 version" -ForegroundColor Green
Import-Module -Name PowerShellGet -MinimumVersion 3.0.0
Write-Host "Module imported"

输出:

[玩转系统] PowerShell 导入模块 | PowerShell 导入模块的主要示例

下面突出显示的区域显示了带前缀的区域。

结论 - PowerShell 导入模块

因此,本文详细介绍了 Import-Module Cmdlet。它还解释了如何导入模块、导入模块的各种方式、过滤需要导入的命令和函数以及适当的示例。要深入了解 cmdlet,建议创建示例程序并享受围绕它们工作的乐趣。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯