撰寫 Windows PowerShell 模組Writing a Windows PowerShell Module

本檔是針對需要封裝和散佈其 Windows PowerShell Cmdlet 的系統管理員、腳本開發人員和 Cmdlet 開發人員所撰寫。This document is written for administrators, script developers, and cmdlet developers who need to package and distribute their Windows PowerShell cmdlets. 藉由使用 Windows PowerShell 模組,您可以封裝和散發您的 Windows PowerShell 解決方案,而不需要使用已編譯的語言。By using Windows PowerShell modules, you can package and distribute your Windows PowerShell solutions without using a compiled language.

Windows PowerShell 模組可讓您將 Windows PowerShell 程式碼分割、組織及抽象化成獨立、可重複使用的單位。Windows PowerShell modules enable you to partition, organize, and abstract your Windows PowerShell code into self-contained, reusable units. 有了這些可重複使用的單位,您就可以輕鬆地直接與其他人共用您的模組。With these reusable units, you can easily share your modules directly with others. 如果您是腳本開發人員,也可以重新封裝協力廠商模組,以建立自訂的腳本架構應用程式。If you are a script developer, you can also repackage third-party modules to create custom script-based applications. 模組與其他指令碼語言(例如 Perl 和 Python)中的模組類似,可讓生產環境準備好使用可轉散發元件的腳本解決方案,還有額外的優點可讓您將多個元件重新封裝並抽象化至建立自訂解決方案。Modules, similar to modules in other scripting languages such as Perl and Python, enable production-ready scripting solutions that use reusable, redistributable components, with the added benefit of enabling you to repackage and abstract multiple components to create custom solutions.

在最基本的情況下,Windows PowerShell 會將儲存在 .psm1 檔案中的任何有效 Windows PowerShell 腳本程式碼視為模組。At their most basic, Windows PowerShell will treat any valid Windows PowerShell script code saved in a .psm1 file as a module. PowerShell 也會自動將任何二元 Cmdlet 元件視為模組。PowerShell will also automatically treat any binary cmdlet assembly as a module. 不過,您也可以使用模組(或更明確地說,模組資訊清單)將整個解決方案組合在一起。However, you can also use a module (or more specifically, a module manifest) to bundle an entire solution together. 下列案例說明 Windows PowerShell 模組的一般用法。The following scenarios describe typical uses for Windows PowerShell modules.

媒體櫃Libraries

模組可以用來封裝和散發執行一般工作的功能緊密程式庫。Modules can be used to package and distribute cohesive libraries of functions that perform common tasks. 一般而言,這些函式的名稱會共用一或多個名詞,以反映它們所用的一般工作。Typically, the names of these functions share one or more nouns that reflect the common task that they are used for. 這些函式也可以與 .NET Framework 類別類似,因為它們可以有公用和私用成員。These functions can also be similar to .NET Framework classes in that they can have public and private members. 例如,程式庫可以包含一組用於檔案傳輸的函數。For example, a library can contain a set of functions for file transfers. 在此情況下,反映一般工作的名詞可能是 "file"。In this case, the noun reflecting the common task might be "file."

設定Configuration

您可以藉由新增特定的 Cmdlet、提供者、函式和變數,使用模組來自訂您的環境。Modules can be used to customize your environment by adding specific cmdlets, providers, functions, and variables.

已編譯的程式碼開發和散發Compiled Code Development and Distribution

Cmdlet 和提供者開發人員可以使用模組來測試和散佈其已編譯的程式碼,而不需要建立嵌入式管理單元。他們可以將包含已編譯器代碼的元件匯入為模組(二進位模組),而不需要建立及註冊嵌入式管理單元。Cmdlet and provider developers can use modules to test and distribute their compiled code without needing to create snap-ins. They can import the assembly that contains the compiled code as a module (a binary module) without needing to create and register snap-ins.

另請參閱See Also

瞭解 Windows PowerShell 模組Understanding a Windows PowerShell Module

如何撰寫 PowerShell 腳本模組How to Write a PowerShell Script Module

如何撰寫 PowerShell 二進位模組How to Write a PowerShell Binary Module

如何撰寫 PowerShell 模組資訊清單How to Write a PowerShell Module Manifest

修改 PSModulePath 安裝路徑Modifying the PSModulePath Installation Path

匯入 PowerShell 模組Importing a PowerShell Module

安裝 PowerShell 模組Installing a PowerShell Module