"PowerShell" "Add-member" Create a PowerShell object

Source: Internet
Author: User
Tags disk usage

by using arrays we can make a collection of many objects, and we can create a hash table with a name and a worthy index relationship, and when we manage an object with multiple properties, or even create a new object entirely, these methods are somewhat inadequate. To this end, we introduce Add-member, from the perspective of learning, you have to do is get-help, understand the use of a command, but in use, but given a change to increase the current object properties or to create a given multiple properties of the object.

First, modify the existing objects

in our daily work, often use the command to do the check, but this time if I need to attach a time attribute, let me get the check results at the same time I know what time to perform the check, how to do?


taking get-service bits as an example, the two most common methods of Add-member are introduced first:

    1. Add a Text property to an object

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/71/7B/wKioL1XSCVfxgKzUAAGIG4AlwGc554.jpg "title=" 1.png " Width= "height=" 133 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:133px; "alt=" Wkiol1xscvfxgkzuaagig4alwgc554.jpg "/>

2. adding script properties to an object

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/71/7F/wKiom1XSB0yjK405AAFzj8gKevQ351.jpg "style=" width : 700px;height:118px; "title=" 2.png "width=" "height=" 118 "border=" 0 "hspace=" 0 "vspace=" 0 "alt=" Wkiom1xsb0yjk405aafzj8gkevq351.jpg "/>


the results obtained by the above two methods are the same, when we get the running state of the BITS service, we attach the time information, but a relatively high readability, a more flexible.

we have seen Add-member in the modification of the object has a very powerful function, he can according to our needs to transform the existing objects, but also led to add-member a few common parameters:

    • MemberType Most critical properties, this property can be configured very much

    • Scriptproperty, a script or command to configure the value of the object;

    • Noteproperty, the Text property of the direct configuration object;

    • Aliasproperty, you can define an alias for an existing attribute;

    • There are many other properties, it is difficult to meet the actual is not to repeat;

    • Name, the new definition of the property's display names;

    • value, the values of the newly defined property;

    • Inputobject, the initialization object that needs to be modified, or the parameter can be omitted by passing the character;

    • Force, which overrides the object if it already contains the property.


second, the new PS Object

Add-member can be a good plump object to meet our needs, but after all it's just a modification to a single object, and with a simple combination of commands, we can have multiple objects grouped into a new object that contains multiple members in the way we define them. For example, I need to get a dynamic change in the size of a folder to confirm the status of my disk usage, what should I do?


It is perfect if you can generate a document like the one shown in the following table, which takes the path or name of the desired monitoring folder as the title, and records the folder size in time.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/71/7B/wKioL1XSCVfATn-dAAI93OzqeWA197.jpg "style=" width : 700px;height:67px; "title=" 3.png "width=" "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "alt=" Wkiol1xscvfatn-daai93ozqewa197.jpg "/>


with the ability to create objects with add-member, we can simply complete the requirements:

1. Create an empty object $temp, followed by the time, the folder you want to monitor as the property name, and assign it to $temp;

2. Define an Array object $output, because $temp can only store one object, we add $temp to $output;

3. Loop to get the value of the $temp and add it into the $output;

4. Enter $output.


The script demonstrates the following:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/7F/wKiom1XSCTHgRoddAAMGGJqJwfI448.jpg "title=" 4.jpg " Width= "height=" 452 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:900px;height:452px; "alt=" Wkiom1xscthgroddaamggjqjwfi448.jpg "/>

well, about Add-member Introduction to throw these primers, Microsoft's official Introduction to Add-member can refer to the following links:

Https://technet.microsoft.com/zh-CN/library/dd347695.aspx

The flexible use of add-member, arrays, and hash tables can make our object output very simple and beautiful.

any questions, welcome to communicate ~



This article is from the "Essence of the" blog, please be sure to keep this source http://simy88.blog.51cto.com/7370552/1685411

"PowerShell" "Add-member" Create a PowerShell object

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.