Exchange A PowerShell Create a custom object

Source: Internet
Author: User
Tags new set

PowerShell is an object-based Shell gives us a lot of flexibility in writing a line of programs, scripts, and functions. When generating detailed reports, we need to customize our data output from our code, or we can send it to other commands through the pipeline. We also need the ability to control and customize the output of the code so that we can combine data from multiple sources into a single object. In this section, we will learn the basics of building custom Objects

The first thing we want to do is to create a collection of mailbox objects that will be used as a data source for a new set of custom objects:

$mailboxes = Get-mailbox

You can add custom properties for subsequent entire pipeline object properties, such as Select-object and the format-table

$mailboxes |

Select-object Name,

Database,

@{name= "Title"; expression={(Get-user $_. Name). Title}},

@{name= "Dept"; expression={(Get-user $_. Name). Department}}

650) this.width=650; "title=" 01.png "style=" height:346px;width:720px; "border=" 0 "hspace=" 0 "src=" http:// S3.51cto.com/wyfs02/m02/59/33/wkiol1tkxpzd1zfcaamvslrvrim459.jpg "width=" 720 "height=" 346 "alt=" Wkiol1tkxpzd1zfcaamvslrvrim459.jpg "/>

Another simple method is to assign a hash table's attribute parameters to the New-object in

$mailboxes | %{

New-object Psobject-property @{

Name = $_. Name

Database = $_. Database

Title = (Get-user $_. Name). Title

Dept = (Get-user $_. Name). Department

}

}

650) this.width=650; "title=" 02.png "style=" height:364px;width:720px; "border=" 0 "hspace=" 0 "src=" http:// S3.51cto.com/wyfs02/m02/59/35/wkiom1tkxbuwyll2aalxf9r1nd0480.jpg "width=" 720 "height=" 364 "alt=" Wkiom1tkxbuwyll2aalxf9r1nd0480.jpg "/>

You can also use New-object to create a new custom object, and then use the Add-member command to enclose some of the custom attributes that belong to

$mailboxes | %{

$obj = New-object PsObject

$obj | Add-member noteproperty Name $_. Name

$obj | Add-member noteproperty Database $_. Database

$obj | Add-member noteproperty Title (Get-user $_. Name). Title

$obj | Add-member noteproperty Dept (Get-user $_. Name). Department

Write-output $obj

}

650) this.width=650; "title=" 03.png "style=" height:367px;width:720px; "border=" 0 "hspace=" 0 "src=" http:// S3.51cto.com/wyfs02/m00/59/33/wkiol1tkxp7r6wuyaame26cjgl8164.jpg "width=" 720 "height=" 367 "alt=" Wkiol1tkxp7r6wuyaame26cjgl8164.jpg "/>

These three code examples output the same custom object, which is used primarily in the Get-mailbox and the Get-user command.

How custom objects work

First, we build custom objects to be able to merge data from multiple data sources into a single object. the Get-mailbox command does not return the Title and Department Properties bound to a user, and The Get-user command needs to be used to retrieve this information. So when we want to generate a report, and the reported information includes get-mailbox and get-user Individual user retrieval, then we can build a custom object , to include all the information. This allows us to pass these objects through the pipeline to other commands that are used to export this information to a file.

We can modify the above code example to output the result to a CSV in the file

$mailboxes |

Select-object Name,

Database,

@{n= "Title"; e={(Get-user $_. Name). Title}},

@{n= "Dept"; e={(Get-user $_. Name). Department}} |

Export-csv–path C:\report.csv-NoType

Look at the results from the example above

650) this.width=650; "title=" 04.png "style=" height:330px;width:720px; "border=" 0 "hspace=" 0 "src=" http:// S3.51cto.com/wyfs02/m00/59/35/wkiom1tkxb2swsmnaal5r-rctx4360.jpg "width=" 720 "height=" "alt=" Wkiom1tkxb2swsmnaal5r-rctx4360.jpg "/>

Look at the results in the file.

650) this.width=650; "title=" 05.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/59/33/wKioL1TKXP_ Zqdwvaact2evw0ha711.jpg "alt=" Wkiol1tkxp_zqdwvaact2evw0ha711.jpg "/>

CSV content in the file:

650) this.width=650; "title=" 06.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/59/35/ Wkiom1tkxb7ybc6qaalzbtsowwy944.jpg "alt=" Wkiom1tkxb7ybc6qaalzbtsowwy944.jpg "/>

can you see that some of the results are not ideal??? How to solve the problem in Chinese? Export CSV Chinese garbled ...

keep in mind that even if you can use format-table command to create a computer property if you want to use the Select-object , just like the previous example can, when speaking the report information output to CSV file or HTML format, you will find a lot of useless data in the report.

when using Select-object when building a custom object, we can select an existing property in the object and then add one or more computed properties through the pipeline. This is done by using a hash table that defines a custom property name in the hash table key value and the script block as the value of the hash table. A script block is an expression that can run one or more commands to define the value of a custom property. In our previous example, you can see that we use the get-user command to get the user's Title and Department Properties, The values of these properties are then assigned to the new object.

The syntax for creating calculated properties is a bit like using names and expressions to create a hash table, you can also abbreviate these keywords, for example:

$mailboxes |

Select-object Name,

Database,

@{n= "Title"; e={(Get-user $_. Name). Title}},

@{n= "Dept"; e={(Get-user $_. Name). Department}}

650) this.width=650; "title=" 07.png "style=" height:327px;width:720px; "border=" 0 "hspace=" 0 "src=" http:// S3.51cto.com/wyfs02/m02/59/33/wkiol1tkxqhgfapjaalfvccezka199.jpg "width=" 720 "height=" 327 "alt=" Wkiol1tkxqhgfapjaalfvccezka199.jpg "/>

name shorthand for n, put expression shorthand for e n and e just for the sake of convenience. You can also use the lable l go to provide the property name.

Create a custom object that uses the New-object The command then assigns a hash table attribute parameter to be a quick and easy method. The only problem is that this method may cause the order of the returned properties to be random. This is because the . NET Framework allocates random values into the hash table, and these properties are based on these values, so the order of the display is not the order we define. If you want to export in the order you want, then you need to use the pipe command select-object to select the attribute name in order.

Create an empty custom object, and then manually pass the Add-member commands require more input, so this syntax is not widely used. But when you want to wear a script method or stir the properties, it will be very useful, this is an advanced technology, in the following example we are less useful.

more examples of custom object commands

Let's look at another way to create a custom object, where we use the Select-object command, take a look at the following example:

$mailboxes | %{

$obj = "" | Select-object name,database,title,dept

$obj. Name = $_. Name

$obj. Database = $_. Database

$obj. Title = (Get-user $_. Name). Title

$obj. Dept = (Get-user $_. Name). Department

Write-output $obj

}

650) this.width=650; "title=" 08.png "style=" height:360px;width:720px; "border=" 0 "hspace=" 0 "src=" http:// S3.51cto.com/wyfs02/m02/59/35/wkiom1tkxcdd-sbkaaoxk42lj_g106.jpg "width=" 720 "height=" "alt=" Wkiom1tkxcdd-sbkaaoxk42lj_g106.jpg "/>

Beware of concurrent pipeline errors

in the previous example, the variable $mailboxes Store The mailbox information, we are all through PowerShell using remote Session to handle more than one command. Ideally, we only need this to be true:

Get-mailbox | %{

New-object Psobject-property @{

Name = $_. Name

Database = $_. Database

Title = (Get-user $_. Name). Title

Dept = (Get-user $_. Name). Department

}

}

650) this.width=650; "title=" 09.png "style=" height:365px;width:720px; "border=" 0 "hspace=" 0 "src=" http:// S3.51cto.com/wyfs02/m00/59/33/wkiol1tkxqtw6xtnaam_b1mhnlg603.jpg "width=" 720 "height=" 365 "alt=" Wkiol1tkxqtw6xtnaam_b1mhnlg603.jpg "/>

but unfortunately, the immediate syntax is completely valid, but it doesn't always work in Exchange Management Shell the. This is because when the get-mailbox command passes the object through the pipeline to foreach-object , we are also trying to run the get-user command to create a custom object, remote PowerShell does not support executing multiple pipelines at the same time. To avoid this, we can use the previous example method, first to store some results in a variable, and then pass the variable to Foreach-objectthrough the pipeline.

This article from "Robin's Home" blog, declined reprint!

Exchange A PowerShell Create a custom 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.