ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2022-08-29 08:01:18  阅读:247  来源: 互联网

标签:Management Get Modules Module ins 模块 PowerShell


更新记录
转载请注明出处。
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可用

标签:Management,Get,Modules,Module,ins,模块,PowerShell
来源: https://www.cnblogs.com/cqpanda/p/16634685.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有