PowerShell custom Output Object Format

Source: Internet
Author: User

There are many types of PowerShell objects, and different types have different output formats. These formats are defined in the dotnettypes.format.ps1xml below $pshome. This file is digitally signed and therefore cannot be modified, but we can modify it according to the copy and paste of the gourd, so that we can make different output effects for the object we define.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/08/wKioL1c1TAiTIhmcAAN9R9nT_r8908.png "title=" 0.PNG " alt= "Wkiol1c1taitihmcaan9r9nt_r8908.png"/>

For example, there is a function, as shown below

Function get-systeminfo{[cmdletbinding ()]param ([string[]] $ComputerName) Begin{}process{[email protected] () foreach ($computer in $ComputerName) {try{write-verbose "querying OS and Computer System" $os =get-wmiobject-class win32_operatingsystem-erroraction Stop $cs =get-wmiobject-class win32_computersystem-erroraction stop}catch{$computer |out-file c:\temp\error.txt-append}[ email protected]{computername= $computer; Lastboottime= $os. Converttodatetime ($os. LastBootUpTime); osversion= $os. Version; Manufacture= $cs. manufacturer; Model= $cs. Model} $obj =new-object-typename psobject-property $prop # $obj. Psobject.typenames.insert (0, ' Yuan.systeminfo ') Write-output $obj}}end {}}get-systeminfo-computername "localhost"


The default output type is Pscustomobject, so the result of the output is the following

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/80/08/wKioL1c1S3GRhXgfAACs3EPTu1k399.png "style=" float: none; "title=" 1.PNG "alt=" Wkiol1c1s3grhxgfaacs3eptu1k399.png "/>


If you add a statement

$obj. Psobject.typenames.insert (0, ' yuan.systeminfo ')


This time if you view $obj | The properties of GM, you can see that his type has become my custom yuan.systeminfo.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/0B/wKiom1c1TE_gPeNiAAFusejyR6g276.png "title=" 3.PNG " alt= "Wkiom1c1te_gpeniaafusejyr6g276.png"/>


Next we create a test.format.ps1xml file that customizes the Yuan.systeminfo format in this file. as shown below. The following basically is the format of the table, but the corresponding display of the name and attributes to the content of my own object.


<?xml version= "1.0"  encoding= "Utf-8"  ?><Configuration><ViewDefinitions>< view><name>yuan.systeminfo</name><viewselectedby><typename>yuan.systeminfo</ typename></viewselectedby><tablecontrol><tableheaders><tablecolumnheader/>< tablecolumnheader><label>manufacturer</label><width>20</width></ tablecolumnheader><tablecolumnheader><width>20</width></tablecolumnheader>< Tablecolumnheader/><tablecolumnheader><label>lastboottime</label></tablecolumnheader ></TableHeaders><TableRowEntries><TableRowEntry><TableColumnItems>< tablecolumnitem><propertyname>computername</propertyname></tablecolumnitem>< tablecolumnitem><propertyname>manufacture</propertyname></tablecolumnitem>< Tablecolumnitem><propertyname>model</propertyname></tablecolUmnitem><tablecolumnitem><propertyname>osversion</propertyname></tablecolumnitem> <tablecolumnitem><propertyname>lastboottime</propertyname></tablecolumnitem></ tablecolumnitems></tablerowentry></tablerowentries></tablecontrol></view></ Viewdefinitions></configuration>


Update the data format after the modification, and then run the program, found that the successful change!

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/0B/wKiom1c1SpLROEOzAAETh-PMWsU148.png "title=" 2.PNG " Style= "Float:none;" alt= "Wkiom1c1splroeozaaeth-pmwsu148.png"/>


Reference: "Learn PowerShell toolmaking in a month of Lunch"

This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1772977

PowerShell custom Output Object Format

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.