PowerShell教程 - 模块管理(Modules Management)

更新记录
转载请注明出处。
2022年8月29日 发布。
2022年8月29日 从笔记迁移到博客。

模块管理(Modules Management)

模块和管理单元(Modules and Snap-Ins)#

Modules were introduced with the release of PowerShell version 2.0
Modules represented a significant step forward over snap-ins
Unlike snap-ins, modules do not have to be formally installed or registered for use with PowerShell

模块的类型#

A module may be binary, script, dynamic, or manifest
Binary modules: These are written in a language such as C# or VB.NET, and then compiled into a dynamic-link library (DLL)
Script modules: These are a collection of functions written in the PowerShell language. The commands typically reside in a script module file (PSM1)
Dynamic modules: These are created using the New-Module command and exists in memory only. The following command creates a very simple dynamic module that adds the Get-Number command: New-Module -Name TestModule -ScriptBlock { function Get-Number { return 1 } }
Manifest modules: These combines different items to make a single consistent module. For example, a manifest may be used to create a single module out of a DLL containing cmdlets and a script containing functions, and Microsoft.PowerShell.Utility is a manifest module that combines a binary and script module

创建模块(Building Modules)#

PowerShell扩展#

提供程序说明(PSProvider)#

可扩展性是PowerShell的一个主要优势
Exchange Server、SharePoint Server、System Center等系列都支持使用PowerShell操作
本质就是PowerShell的扩展,可以使用提供程序扩展PowerShell
大部分的其他软件程序包都存在提供程序
比如Internet Information Service(IIS)、SQL Server,甚至是活动目录
可以通过模块或者管理单元将一些提供程序添加到PowerShell中
如果启用了某些PowerShell功能,可能也会新增一个PSProvider

获得系统上已经有的提供程序#

Get-PSProvider

PowerShell扩展支持的扩展的类型#

模块 和 管理单元

PowerShell官方扩展库#

https://www.powershellgallery.com/

从Internet获取模块-PowerShellGet#

PowerShellGet http:// powershellgallery.com
PowerShellGet 模块需要 PowerShell 3.0 或更高版本
PowerShellGet随着PowerShell v5以及更新版本一起发行
如果你使用的是v5版本(检查$PSVersionTable),就拥有该模块

查找包#

Find-Module

命令冲突解决#

安装多个库之后可能会导致Cmdlet冲突
可以在Cmdlet前加上库名称即可

MyCoolPowerShellSnapin\Get-User

编写cmdlet#

https://docs.microsoft.com/zh-cn/powershell/scripting/developer/help/writing-help-for-windows-powershell-cmdlets?view=powershell-6

提供器(Providers)

Provider说明#

提供程序将各种数据操作表现为文件系统的操作命令
PowerShell中的提供程序主要用于不同数据的访问
使用统一的标准访问不同的数据,有种LINQ的感觉

常见的Provider#

filesystem, registry, certificate store, and so on

获得Provider的帮助描述

Get-Help about_Providers

PowerShell Gallery

The PowerShell Gallery is a repository and distribution platform for scripts, modules, and Desired State Configuration (DSC) resources that have been written by Microsoft or other users of PowerShell
注意:Support for the gallery is included by default in PowerShell 5

网站:https://www.powershellgallery.com

显示已经导入的包#

Get-Module

显示已经加载的包

Get-Module -ListAvailable 

模块加载位置:
Powershell从PSMDULEPATH环境变量中查找模块加载的位置

$env:PSMODULEPATH

使用分割符进行分割

$env:PSModulePath -split ';'

导入模块#

Import-Module

导入指定名称的模块(默认从$PSMODULEPATH加载)

Import-Module -Name PSWorkflow

导入模块跳过版本检查

Import-Module NetSecurity -SkipEditionCheck

导入指定文件内的模块

Import-Module -Name
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWorkflow\PSWorkflow.ps
d1

注意:默认情况下,将模块安装到 $env:ProgramFiles\WindowsPowerShell\Modules

移除模块#

Remove-Module

查找模块#

Find-Module

实例:

搜索指定名称的包

Find-Module Carbon
Find-Module -Name Carbon
Find-Module -Name Azure*

搜索指定标签的包

Find-Module -Filter IIS

安装模块#

Install-Module

实例:

安装指定模块(限定在指定用户)

Install-Module carbon -Scope CurrentUser

强制安装指定模块

Install-Module carbon -Scope CurrentUser -Force

更新模块#

Update-Module

保存模块#

Save-Module

说明:下载模块,而不安装

添加模块路径#

Add-WindowsPSModulePath

snap-ins(管理单元)

介绍(Introducing snap-ins)#

Snap-ins are only available in Windows PowerShell,not present in PowerShell Core
管理单元(snap-ins)是模块(Module)的前身
Snap-ins must be installed or registered before they can be used

可以在注册表中查看已经注册的snap-ins

HKLM:\Software\Microsoft\PowerShell\1\PowerShellSnapIns

查看已安装的snap-ins#

Get-PSSnapIn -Registered 

注意:只在Windows PowerShell可用

作者:重庆熊猫

出处:https://www.cnblogs.com/cqpanda/p/16634685.html

版权:本作品采用「不论是否商业使用都不允许转载,否则按3元1字进行收取费用」许可协议进行许可。

posted @   重庆熊猫  阅读(721)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示