Powershell beginners (12): compiling powershell management units and binary modules

Source: Internet
Author: User
ArticleDirectory
    • Write Management Unit
    • Use Management Unit
    • Write binary Module
    • Conclusion

From the very beginning, powershell proposed to use management units to implement custom cmdlet, providing a script-based management solution different from the UI interface for large software systems. In powershell V2, The powershell management unit can also be processed as a binary module.

Write Management Unit

Compiling powershell management units depends on system. Management. automation.ProgramSystem. Management. Automation assembly is part of the Windows SDK, so installing Windows SDK is a prerequisite for compiling powershell management units. You can download the Windows SDK from here.

As a preparation for compiling the powershell Management Unit, open vs2008 or vs2010 and create a class library project. Select 3.5 for the target. NET Framework version. After simple compilation, you can write the command class of the cmdlet and the installation class of snapin.

Compile the cmdlet class

Follow these steps to customize the cmdlet class:

1. Add system to the project. management. reference of automation assembly (Path: C: \ Program Files \ reference assemblies \ Microsoft \ windowspowershell \ V1.0 \ system. management. automation. DLL), and add the reference using system of the corresponding command space. management. automation;

2. Create a class named new-addition and add a reference to the Command Space System. Management. automation;

3. Let the new-addition class inherit from pscmdlet, add the cmdlet attribute tag to the new-addition class, and set the verb and noun;

4. Add parameter information. The parameter must be marked with the parameter attribute;

5. Reload the processrecord function to implement your own business logic;

An example of writing a cmdlet class is as follows (this example references Ivor Bright's blog ):

 Using  System;  Using  System. Management. automation;  Namespace  Pssnapindemo1 {[cmdlet (verbscommon. New,  "  Addition  "  )] Public   Class  New_addition: pscmdlet {[alias (  "  X  "  )] [Parameter (mandatory = True , Valuefrompipelinebypropertyname = True  )]  Public   Int Firstparameter { Get ; Set ;} [Alias (  "  Y  "  )] [Parameter (mandatory = True , Valuefrompipelinebypropertyname = True  )]  Public   Int Secondparameter { Get ; Set  ;}  Protected   Override  Void  Processrecord () {writeobject (  String . Format ( "  {0} + {1 }={ 2}  " , This . Firstparameter, This  . Secondparameter,  This . Firstparameter + This  . Secondparameter ));}}} 

After the compilation is complete, the custom cmdlet class is complete, and then you can compile the pssnapin installation class.

Compile the pssnapin installation class

The purpose of compiling the pssnapin installation class is to enable the class library to be installed and recognized as a powershell Management Unit by the system. The procedure is as follows:

1. Add a reference to the system. configuration. install assembly;

2. Create a class named pssnapininstaller and add references to the namespace system. componentmodel and system. Management. automation;

3. Let the pssnapininstaller class inherit from pssnapin and identify it with the runinstaller (true) attribute.

4. Reload three attributes: name, vendor, and description;

Example:

 Using  System;  Using  System. Management. automation;  Using  System. componentmodel;  Namespace  Pssnapindemo1 {[runinstaller (  True )]  Public   Class  Pssnapininstaller: pssnapin {  Public   Override   String  Description {  Get { Return   "  This is Luke's first snap in demo  "  ;}}  Public  Override   String  Name {  Get { Return   "  Pssnapindemo1  "  ;}}  Public   Override   String  Vendor {  Get { Return   " Caiju  "  ;}}}} 

After the compilation is complete, the pssnapin installation class is also completed.CodeAll the work is done. Compile the entire solution and generate the class library file.

Use Management Unit

You must register a management unit before using it. The registration process is as follows:

1. Open the name line that comes with Visual Studio and navigate to the pssnapindemo1.dll directory;

2. Run the "installutil/I pssnapindemo1.dll" command;

If the preceding command is successfully executed, the registration process is complete. If it fails, check the log file of the installation process carefully.

After the powershell Management Unit is successfully registered, you can add it to the current session. Find the registered powershell Management Unit. Run the following command:

PSC: \ Users \ Luke> Get-pssnapin-registered |Ftname psversion description------------------------Pssnapindemo12.0This is Luke's first Snap I... sqlservercmdletsnapin1002.0 this is a powershell snap-I... sqlserverprovidersnapin1002.0 SQL Server Provider

The output result of the preceding command shows the powershell management unit we just registered. To add a management unit, run the following command:

 
PSC: \ Users \ Luke> Add-pssnapin-name pssnapindemo1

After the powershell Management Unit is successfully added, you can use the commands in the same way as using the built-in cmdlet command.

Write binary Module

Compiling a binary module is basically the same as writing a management unit. You need to implement the cmdlet class, but it does not need to implement the installation class. For details, refer to the steps for compiling the cmdlet class above. It must be noted that the class library implementing the pssnapin class can still be used as a binary module. That is to say, the Assembly file of the powershell management unit can be used as an assembly of the binary module without modification.

Use binary Module

The usage of the binary module is the same as that of the script module in powershell (11): Create a folder with the same name under the search directory and copy the assembly, optional. Create a description file. Then, find and import the module. Then, the commands in the binary module can be used.

Conclusion

In addition to custom cmdlet, The powershell management unit can also write custom providers. The provider is a more advanced concept, with predefined file systems, registries, and certificates. However, aliases and variables cannot be defined in powershell management units. If you need these functions, you can use the script module.

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.