Power shell Study Notes (2) Objects

Source: Internet
Author: User
Tags mssqlserver
Processing object

Although you may not realize it at first, the. NET object is used when working in Windows powershell. As your experience increases, the object processing capability becomes more obvious, and you find that you are using objects, or even thinking with objects.

Technically speaking, the. NET object is a. Net class instance that contains data and operations associated with the data. However, objects can be treated as data entities with properties (similar to features) and methods (operations that can be performed on objects.

For example, when obtaining a service in Windows powershell, it actually gets the object that represents the service. When you view service information, you can view the properties of the service object. In addition, when the service is started (that is, when the service status attribute is changed to "started"), the method of the service object is used.

All objects of the same type have the same attributes and methods, but each instance of the object may have different attribute values. For example, each service object has the name and status attributes. However, each service can have different names and statuses.

It is easy to understand objects after they are ready. To identify the type of the object being retrieved by the cmdlet, use the pipeline operator (|) to send the result of the "get" command to the get-member command. For example, the following command sends the object retrieved by the get-service command to get-member.

Get-service | get-member

Get-member displays information about the service object, including the object type name and its attributes and methods.

To obtain information about the object class, copy and paste the type name in msdn, such as system. serviceprocess. servicecontroller. After finding the class, you can read the secondary topic of msdn to learn the attributes and methods of the class-based objects (such as objects in Windows powershell.

Annotation:

In fact, after careful observation, we can guess his handling thoughts. In fact, the information obtained by each cmdlet (get) is returned in the form of a. Net object. You can understand that we are using PS scripts to writeCode. ButProgramCompilation is not required. PS is an object-based approach. First, get the object, set the object through (property, no need to explain it), and operate the object through (method. Get-member is a very useful method. When you get an object and do not know what operations can be performed on it, try to use it. I generally cannot remember so many attributes and Methods. Most of them all think about the idea and debug the script.

To find the values of all properties of a specific object, use the pipeline operator (|) to send the results of the "get" command to the format-list or format-Table command. Use the property parameter of the format cmdlet with all values. For example, to search for all the attributes of the Schedule service on the system, type:

Get-service schedule | format-list-property *

The following shows a sample result.

Annotation:

First, get the service object of schedule, and then set the format of the output list, in the format of displaying only property (and its value ).

When learning windows powershell, you don't need to know anything about the object, but you need to be aware of this concept. You will soon be able to make full use of the object.

 

Target Audience: Get-member

Get-member is one of the most useful cmdlet that displays information about the. NET object returned by the command. This information includes the object type, attributes, and methods.

To use get-Member, use the pipeline operator (|) to send the command result to get-member. For example:

Get-service | get-member

This command shows that get-service actually returns a group of system. serviceprocess. servicecontroller objects-each service on the computer has an object.

This information looks very technical, but it is actually very practical.

· You can use the type name (such as "system. serviceprocess. servicecontroller") to know the type of. Net object returned by the cmdlet. To obtain information about objects in this. Net class, paste the type name in the search text box on msdn. The associated msdn topic includes the attributes and methods of objects in this class, including the objects returned by get-service.

· The property type indicates the properties of an object. The value of each attribute is information about the service object. For example, the servicecontroller object has the canpauseandcontinue attribute. The msdn description of this attribute indicates whether the service can be suspended or restored.

To list the attribute values of a specific service,Enter:

(Get-service <service name>). <property name>

For example:

(Get-service MSSQLServer). canpauseandcontinue

To display the name and Value List of the canpauseandcontinue attribute of the MSSQLServer service, type:

Get-service MSSQLServer | format-list-property name, canpauseandcontinue

To display a list Of all attribute values of the MSSQLServer service, type:

Get-service MSSQLServer | format-list-property *

To display the name and value table of the canpauseandcontinue attribute of all services, type:

Get-service | format-table-property name, canpauseandcontinue

· The method type indicates the method of the object, that is, the operation that can be performed on the object. For example, the servicecontroller object has the stop method, which can be used to stop the service.

To call the method of a service object, use the following format. (Must include parentheses ).

(Get-service <service name>). <Method Name> ()

(Get-service MSSQLServer). Stop ()

Likewise

(Get-service MSSQLServer). Start ()

For information about the get-member command, type:

Get-help get-member-detailed.

If you are familiar with. NET programming, it is easy to get started.

Related Article

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.