PowerShell an introductory tutorial to write and use a script module instance _powershell

Source: Internet
Author: User
Tags aliases

You can now manage large, distributed software systems in the PowerShell command line or script by writing a module, just as Exchange Server and SharePoint server are doing. Do you have this kind of demand?

Modules, scripting modules

The module is the concept introduced in PowerShell V2 to improve the previously proposed "Snap-in". After the snap-in is also processed as a module (binary module), the PowerShell includes two modules-script modules and binary modules. So what is a module? A module is a package that contains a number of PowerShell commands, items that are distributed and shared and loaded with a whole.

The Scripting module is the concept presented with the module in PowerShell V2, which is written entirely by the PowerShell syntax and environment and does not need to be switched to other compilation languages or development environments. The Scripting module is now the recommended way to write PowerShell modules, so learn well.

Writing script Modules

Scripting modules include identifying module paths, creating module folders, writing module files, and writing manifest files, all of which are easy to operate, as follows:

Module installation path

In theory, modules can be placed anywhere on the machine, but they are easier to manage and use if they are placed within the Env:\psmodulepath search range. Env:\psmodulepath is a variable with the same modeling method as the system variable%path%, whose variable value is a string of comma-delimited paths. Although the Env:psmodulepath variable has a system-predefined two directories, they may not have been created and need to be created when they are in use.

You can obtain two paths with the following command:

Copy Code code as follows:

PS c:\users\luke> $paths = (dir env:\psmodulepath). Value.split (";")
PS c:\users\luke> $paths
C:\Users\luke\Documents\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

Check to see if the path has been created with the following command:
Copy Code code as follows:

PS c:\users\luke> Test-path-path $paths [0]
False

The above returns false to indicate that the path was not created. Create a path to use the file browser, cmd command, or PowerShell command. Here, use the PowerShell command as follows:
Copy Code code as follows:

New-item-path $paths [0]-itemtype Directory-force

The path required to place the module is created so that you can proceed to the next step.

Module folder

A module folder is an integral part of a module that has the same name as a module. module files, description files, and other possible script files that are included in the modules are placed in this folder. For example, to create a module ModuleDemo1, you can use the above New-item command or switch to a script path to use the MD command, that is, MD ModuleDemo1.

Script module file

The script module file is similar to the contents of the Generic script file, except that the suffix is. psm1 instead of. PS1, whose filename is the name of the module. However, in general, the module should contain only functions, aliases, variables, and so on, not many operations or output. A simple example of MODULEDEMO1.PSM1 is as follows:

Copy Code code as follows:

function Greet ([String] name)
{
"Hello $name"
}

Module description File

The description file is also called the manifest file, and the extension is. PSD1, and its file name is the same as the module. Its content is actually a hashtable; the name of the middle key in this hashtable is defined by the system. Script module files are mainly about restricting the running environment, setting dependencies, and describing scripts.

Creating and editing a module description file can be used with any text editor, but for the first time using the recommended use command. The command to create a module description file is as follows:

Copy Code code as follows:

PS c:\users\luke\documents\windowspowershell\modules\moduledemo1> New-modulemanifest-path. \MODULEDEMO1.PSD1

Cmdlet new-modulemanifest at command pipeline position 1
Supply values for the following parameters:
Nestedmodules[0]:
Author:luke Zhang
Companyname:caiju
Copyright:
Moduletoprocess:moduledemo1
Description:demo1
Typestoprocess[0]:
Formatstoprocess[0]:
Requiredassemblies[0]:
FILELIST[0]:

The module description file is created so that the details can be opened for file editing.

Using scripting Modules

The script module is ready to use, and loading the use of script module files mainly utilizes the Get-module command and the Import-module command. Execute the following command to show the module we just wrote:

Copy Code code as follows:

PS d:\> get-module-listavailable

Moduletype Name Exportedcommands
---------- ----                      ----------------
Manifest ModuleDemo1 {}
Manifest adrms {}
Manifest AppLocker {}
Manifest bestpractices {}
Manifest Bitstransfer {}
Manifest Psdiagnostics {}
Manifest Servermanager {}
Manifest Troubleshootingpack {}
Manifest Webadministration {}

Then import the module we just wrote:

Copy Code code as follows:

PS d:\> Import-module-name ModuleDemo1

After the import, the Geet function defined in the module can be used:
Copy Code code as follows:

PS d:\> Greet "Luke"
Hello Luke

It can also be used if other aliases, variables, etc. are also defined in the module.

Conclusion

The module is not only an efficient solution for large software scripting management, but also a good way to share PowerShell commands. It is one of the few ways that PowerShell programmers to showcase their work, and is one of the most specialized ways. So, 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.