powershell 学习_极客学校:学习如何扩展PowerShell

powershell 学习

powershell 学习_极客学校:学习如何扩展PowerShell

PowerShell offers two ways for you to extend the shell. You can either use snapins, which are binary only and developed in a fully-fledged programming language like C#, or you can use modules, which can be binary as well as script based.

PowerShell提供了两种扩展外壳的方法。 您可以使用仅二进制的并以成熟的编程语言(如C#)开发的管理单元,也可以使用既可以二进制也可以基于脚本的模块。

Be sure to read the previous articles in the series:

确保阅读本系列中的先前文章:

And stay tuned for the rest of the series all week.

并继续关注本系列的其余部分。

管理单元 (Snapins)

Snapins are so last year. All jokes aside, snapins never really caught on among the PowerShell community because most scripters aren’t developers and you can only write snapins in a language like C#. Nevertheless there are still some products that use snapins, like Web Deploy for example. In order to see what snapins are available for you to use in the shell you use the following command:

Snapins去年是如此。 撇开所有笑话,管理单元从未真正在PowerShell社区中流行,因为大多数脚本编写者都不是开发人员,并且您只能使用C#这样的语言编写管理单元。 但是,仍然有一些使用管理单元的产品,例如Web Deploy。 为了查看在外壳程序中可以使用哪些管理单元,请使用以下命令:

Get-PSSnapin –Registered

Get-PSSnapin –已注册

powershell 学习_极客学校:学习如何扩展PowerShell

To use the commands added by a snapin, you first need to import it into your session, and you can do that like so:

要使用由管理单元添加的命令,您首先需要将其导入到会话中,您可以这样做:

Add-PSSnapin -Name WDeploySnapin3.0

Add-PSSnapin-名称WDeploySnapin3.0

At this point, you will get an error if you don’t have the Web Deploy snapin installed. If you do have it installed, like I do, then it will be imported into your session. To get a list of commands available in the snapin, you can simply use the Get-Command cmdlet:

此时,如果您未安装Web Deploy管理单元,则会收到错误消息。 如果像我一样安装了它,那么它将被导入到您的会话中。 若要获取管理单元中可用的命令列表,只需使用Get-Command cmdlet:

Get-Command –Module WDeploy*

获取命令–模块WDeploy *

Note: Technically this isn’t a module, but for some reason you still have to use the Module parameter.

注意:从技术上讲,这不是模块,但是由于某些原因,您仍然必须使用Module参数。

powershell 学习_极客学校:学习如何扩展PowerShell

模组 (Modules)

Modules are newer and are the way forward. They can be both scripted using PowerShell as well as coded in a language like C#. Most of the built-in commands are organized into modules as well. To see a list of modules on your system, you can use the following command:

模块是较新的,是前进的道路。 它们既可以使用PowerShell编写脚本,也可以使用C#等语言编写。 大多数内置命令也都组织成模块。 要查看系统上的模块列表,可以使用以下命令:

Get-Module –ListAvailable

获取模块–ListAvailable

powershell 学习_极客学校:学习如何扩展PowerShell

As products are updated, their PowerShell counterparts are being migrated to modules. For example, SQL used to have a snapin, but it is now made up of modules.

随着产品的更新,其PowerShell对应产品将被迁移到模块。 例如,SQL曾经有一个管理单元,但现在由模块组成。

powershell 学习_极客学校:学习如何扩展PowerShell

In order to use a module, you need to import it first.

为了使用模块,您需要先导入它。

Import-Module -Name SQLASCMDLETS

导入模块-名称SQLASCMDLETS

You can use the same trick we used with snapins to view all the commands that the module added to the shell.

您可以使用与管理单元相同的技巧来查看模块添加到外壳的所有命令。

powershell 学习_极客学校:学习如何扩展PowerShell

So that leaves the question: how does PowerShell know what snapins and modules you have on your system? Well, snapins are a bit of a pain and have to be installed. Part of the installation process includes creating a few registry entries that PowerShell looks at to find snapin information. Modules, on the other hand, can be registered with the shell by simply placing them in one of the locations in the PSModulePath environment variable. Alternatively, you could just add the path to the module to the environment variable.

这样就留下了一个问题:PowerShell如何知道系统上有哪些管理单元和模块? 好吧,管理单元有点麻烦,必须安装。 安装过程的一部分包括创建一些注册表项,PowerShell会查看这些注册表项以查找管理单元信息。 另一方面,只需将模块放在PSModulePath环境变量中的一个位置中,就可以向外壳注册模块。 或者,您可以只将模块的路径添加到环境变量中。

($env:PSModulePath).Split(“;”)

($ env:PSModulePath).Split(“;”)

That will spit out the contents of the variable. Notice that if you have a module like SQL installed, how it modified the variable to include the SQL module’s location.

这将吐出变量的内容。 请注意,如果您安装了类似SQL的模块,它将如何修改变量以包括SQL模块的位置。

powershell 学习_极客学校:学习如何扩展PowerShell

模块自动加载 (Module Auto Loading)

PowerShell 3 introduced an awesome new feature which goes by a few names. None of them are official, but “Module Auto Loading” is the best description of it. Basically, it allows you to use cmdlets that belong to an external module without explicitly importing the module using the Import-Module cmdlet. To see this, first remove all the modules from your shell using the following command:

PowerShell 3引入了一个很棒的新功能,其中有几个名称。 它们都不是官方的,但是“模块自动加载”是对此的最好描述。 基本上,它使您可以使用属于外部模块的cmdlet,而无需使用Import-Module cmdlet显式导入模块。 要查看此信息,请首先使用以下命令从您的外壳中删除所有模块:

Get-Module | Remove-Module

获取模块| 删除模块

You can then check that you have no modules loaded by using the following:

然后,可以使用以下方法检查是否没有加载模块:

Get-Module

获取模块

powershell 学习_极客学校:学习如何扩展PowerShell

Now use a cmdlet that isn’t in the core library. Test-Connection is a good one:

现在,使用不在核心库中的cmdlet。 测试连接是一个很好的选择:

Test-Connection localhost

测试连接本地主机

powershell 学习_极客学校:学习如何扩展PowerShell

If you check your loaded modules again, you will see that it did indeed load the module.

如果再次检查已加载的模块,您将看到它确实已加载了模块。

powershell 学习_极客学校:学习如何扩展PowerShell

That’s all for today guys, join us tomorrow for more.

今天就这些了,明天再加入我们。

翻译自: https://www.howtogeek.com/139077/geek-school-learn-how-to-extend-powershell/

powershell 学习

相关文章: