Powershell (11): script Module

Source: Internet
Author: User
ArticleDirectory
    • Module, script Module
    • Script Module
    • Use the script Module
    • Conclusion

Now, by writing a module, you can manage large and distributed software systems in powershell command lines or scripts, just as exchange server and Sharepoint Server are doing. Do you have such a requirement?

Module, script Module

A module is a concept introduced in powershell V2 to improve the "Management Unit" proposed earlier ". After the Management Unit is also processed as a module (Binary module), powershell includes two modules: script module and binary module. So what is a module? The module contains some powershell commands and items.ProgramPackage, which is distributed, shared, and loaded as a whole.

The script module is a concept proposed together with the module in powershell V2. It is fully written by powershell syntax and environment and does not need to be switched to other compilation languages or development environments. The script module is the recommended method for compiling powershell modules.

Script Module

The script module is easy to operate, including determining the module path, creating module folders, Writing module files, and writing manifest files. The specific content is as follows:

Module installation path

Theoretically, the module can be placed anywhere on the local machine, but it is easier to manage and use if it is placed within the search range of env: \ psmodulepath. Env: \ psmodulepath is a variable with the same modeling method as the system variable % PATH %. Its variable value is a string consisting of comma-separated paths. Although the Env: psmodulepath variable contains two pre-defined directories, they may not be created and need to be created during use.

You can use the following command to obtain two paths:

 
PS c: \ Users \ Luke>$ Paths= (DIR env: \ psmodulepath). value. Split (";") PS c: \ Users \ Luke>$ PathsC: \ Users \ Luke \ Documents \ windowspowershell \ modulesc: \ windows \ system32 \ windowspowershell \ v1.0 \ modules \

Run the following command to check whether the path has been created:

 
PS c: \ Users \ Luke> test-path-Path$ Paths[0]
False

If false is returned, the path is not created. You can use the file browser, CMD command, or powershell command to create a path. The powershell command is as follows:

 
New-item-Path$ Paths [0]-Itemtype directory-Force

In this way, the path required for placing the module is created and you can proceed to the next step.

Module folder

A module folder is an integral part of a module and must have the same name as the module. The module files, description files, and other script files that may be used must be placed in this folder. For example, to create module moduledemo1 in this article, you can use the new-item command or switch to the script path and then use the MD command, that is, MD moduledemo1.

Script module File

The content of the script module File is similar to that of the general script file, except that the suffix is. psm1 rather than. PS1. The file name is the name of the module. However, in general, a module should only contain functions, aliases, variables, and other definitions, and should not perform many operations or outputs. A simple example of moduledemo1.psm1 is as follows:

 
FunctionGreet ([String] Name ){"Hello $ name"}
Module description file

The description file is also called a manifest file. The extension is. psd1, and its file name must be the same as that of the module. Its content is actually a hashtable; the difference is that the name of the key in this hashtable is pre-defined by the system. The role of the script module file is to restrict the running environment, set dependencies, and describe the script.

You can use any text editor to create and edit a module description file. However, for the first time, you are advised to use commands. The command for creating a module description file is as follows:

PS c: \ Users \ Luke \ Documents \ windowspowershell \ modules \ moduledemo1> New-modulemanifest-Path. \ moduledemo1.psd1cmdlet new-Modulemanifest at command pipeline position 1Supply valuesForThe following parameters: nestedmodules [0]: Author: Luke zhangcompanyname: caijucopyright: moduletoprocess: moduledemo1description: demo1typestoprocess [0]: Formatstoprocess [0]: Requiredassemblies [0]: Filelist [0]:

In this way, the module description file is created. For details, you can open the file and edit it.

Use the script Module

The script module can be used after being compiled. the get-module command and the import-module command are used to load and use the script module File. Execute the following command to display the module we just compiled:

Ps d: \> Get-module-Listavailablemoduletype name exportedcommands------------------------------Manifest comment {} manifest adrms {} manifest applocker {} manifest comment {} manifest bitstransfer {} manifest psdiagnostics {} manifest servermanager {} manifest comment {} manifest webadministration {}

Then import the module we just compiled:

 
Ps d: \> Import-module-name moduledemo1

After the import, the Geet function defined in the module can be used:

 
Ps d: \> greet"Luke"Hello Luke

If other aliases and variables are defined in the module, they can also be used.

Conclusion

the module is not only an efficient solution for large-scale software scripting management, but also a good way to share powershell commands. It is one of the few ways for powershell programmers to demonstrate their work achievements, and is the most professional. Therefore, play the module and show it to everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.