Windows PowerShell Object = attribute + method _powershell

Source: Internet
Author: User

In the real world, you may already know that objects are things that you can touch. The objects in PowerShell are similar to real life. For example, to describe a knife in real life. We might be able to describe it in two ways.
Property : A knife has some special properties, such as its color, manufacturer, size, number of blades. The object is red, weighs 55 grams, has 3 blades, produced by ABC Company. So the property describes what an object is.
method : You can use this object to do something, such as cutting things, when the screws with, open beer lid. A method in which an object is capable of being part of this object.

Creating objects

You can create an object by New-object, or even create a virtual knife, but the first step is to create an empty object. A null object has nothing, and if it is invoked, it does not return anything.

Copy Code code as follows:

PS c:powershell> $pocketknife =new-object Object
PS c:powershell> $pocketknife
System.Object

Add Properties

Next, describe what this object is.

Copy Code code as follows:

PS c:powershell> add-member-inputobject $pocketknife-name color-value "Red"
-membertype Noteproperty
PS c:powershell> $pocketknife

Color
-----
Red

PS c:powershell> add-member-inputobject $pocketknife-name weight-value "55"
-membertype Noteproperty
PS c:powershell> $pocketknife | Add-member Noteproperty Blades 3
PS c:powershell> $pocketknife | Add-member Noteproperty manufacturer ABC
PS c:powershell> $pocketknife

Color Weight Blades Manufacturer
----- ------ ------ ------------
Red 3 ABC


Add Method

When you add attributes to an object, the object has a shape, but it still doesn't do anything, and if you want to do something, you have to add a way to it. Use the same add-member, but the-membertype option uses Scriptmethod.

Copy Code code as follows:

# Add a new method:
Add-member-membertype scriptmethod-in $pocketknife '
-name cut-value {"I ' m whittling Now"}
# Add a new method for specifying the parameter type:
Add-member-in $pocketknife scriptmethod Screw {"phew...it ' in!"}
#直接通过管道增加一个新方法:
$pocketknife | Add-member Scriptmethod Corkscrew {"pop! Cheers! "}

Method has been successfully added, you can call the
Copy Code code as follows:

PS c:powershell> $pocketknife. Cut ()
I ' M whittling now
PS c:powershell> $pocketknife. Screw ()
Phew...it ' s in!
PS c:powershell> $pocketknife. Corkscrew ()
pop! cheers!

If you do not use parentheses when calling a method, the method does not execute, but you can return the basic information for the method.

Copy Code code as follows:

PS c:powershell> $pocketknife. Corkscrew

Script: "pop! Cheers! "
Overloaddefinitions: {System.Object corkscrew ();}
Membertype:scriptmethod
TypeNameOfValue:System.Object
Value:System.Object Corkscrew ();
Name:corkscrew
Isinstance:true


So far a virtual knife object has been created, and an object contains data (attributes) and actions (methods).

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.