iT邦幫忙

2021 iThome 鐵人賽

DAY 17
0
自我挑戰組

Powershell 入门系列 第 17

Powershell 入门之模块(补充)

今天本来想写动态参数的,但发现脚本有些内容涉及到一些模块的知识,所以今天我们就先来看一下powershell 中的模块。

Powershell 通过模块来扩展其功能,这样你可以根据自己的需要安装自己需要的模块,而不是安装所有模块。

我们可以通过查看 PSModulePath 来查看模块的路径:

PS C:\Users\Administrator> Get-Content Env:\PSModulePath
C:\Users\Administrator\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules

我们可以通过 Get-Module 查看当前会话已经加载的系统模块:

PS C:\Users\Administrator> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.0.1.0    ActiveDirectory                     {Add-ADCentralAccessPolicyMember, Add-ADComputerServiceAcc...
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...

你也可以通过 -ListAvailable 选项获取当前环境中所有可用的模块:

PS C:\Users\Administrator> Get-Module -ListAvailable


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0.1      Microsoft.PowerShell.Operation.V... {Get-OperationValidation, Invoke-OperationValidation}
Binary     1.0.0.1    PackageManagement                   {Find-Package, Get-Package, Get-PackageProvider, Get-PackageSource...}
Script     3.4.0      Pester                              {Describe, Context, It, Should...}
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Set-PSReadLineKeyHandler, Remove-PSReadLineKeyHandler, Get-PSReadLineOption...}


    Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.0.1.0    ActiveDirectory                     {Add-ADCentralAccessPolicyMember, Add-ADComputerServiceAccount, Add-ADDomainControllerPasswordReplicationP...
Manifest   1.0.0.0    ADDSDeployment                      {Add-ADDSReadOnlyDomainControllerAccount, Install-ADDSForest, Install-ADDSDomain, Install-ADDSDomainContro...
Manifest   1.0.0.0    AppBackgroundTask                   {Disable-AppBackgroundTaskDiagnosticLog, Enable-AppBackgroundTaskDiagnosticLog, Set-AppBackgroundTaskResou...
Manifest   2.0.0.0    AppLocker                           {Get-AppLockerFileInformation, Get-AppLockerPolicy, New-AppLockerPolicy, Set-AppLockerPolicy...}
Manifest   1.0.0.0    AppvClient                          {Add-AppvClientConnectionGroup, Add-AppvClientPackage, Add-AppvPublishingServer, Disable-Appv...}
Manifest   2.0.1.0    Appx                                {Add-AppxPackage, Get-AppxPackage, Get-AppxPackageManifest, Remove-AppxPackage...}
Manifest   1.0        BestPractices                       {Get-BpaModel, Get-BpaResult, Invoke-BpaModel, Set-BpaResult}
Manifest   2.0.0.0    BitsTransfer                        {Add-BitsFile, Complete-BitsTransfer, Get-BitsTransfer, Remove-BitsTransfer...}
Manifest   1.0.0.0    BranchCache                         {Add-BCDataCacheExtension, Clear-BCCache, Disable-BC, Disable-BCDowngrading...}
Manifest   1.0.0.0    CimCmdlets                          {Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance, Get-CimSession...}
Manifest   1.0        ConfigCI                            {Get-SystemDriver, New-CIPolicyRule, New-CIPolicy, Get-CIPolicy...}
... ... ... ...
... ... ... ...

你可以通过 install-module 来安装第三方模块

PS C:\Users\Administrator> Install-Module VMware.PowerCLI

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y

注意:很多功能模块是和角色相关的,有些模块安装,可能需要安装相关角色的管理工具(有时会包含图形管理工具)。

安装完成后,通过 Get-Module 验证,因为模块太多,我们可以直接通过 -name 选项,直接查看指定名称的模块。

PS C:\Users\Administrator> Get-Module -Name VMware.PowerCLI -ListAvailable


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   12.4.0.... VMware.PowerCLI

很多模块安装后,在 powershell 启动后,并没有被加载,所以需要我们通过 import-module 去加载:

PS C:\Users\Administrator> Import-Module VMware.PowerCLI

加载完成后,通过 Get-Module 去验证。

自从 Powershell 3 之后,Powershell 增加了一个自动加载的功能,对于非第三方的模块,可以实现自动加载,而不需要你通过 import-module 命令去手动加载。

对于经常使用的模块,我们想让它在 powershell 启动时就被加载,而不是等到我们使用的时候再去加载。我们可以同在 Powershell 的主目录中,添加一个 profile.ps1 文件,在里面添加 import-module <module name>,来实现自动加载。

当前计算机上的所有用户生效:
%Windir%\system32\WindowsPowerShell\v1.0\profile.ps1

当前用户
%UserProfile%\Documents\WindowsPowerShell\profile.ps1


上一篇
Powershell 入门参数属性(2)
下一篇
Powershell 入门之动态参数
系列文
Powershell 入门21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言