When PowerShell creates an object, the object's properties can be a single property, or it can include multiple objects within that property, which is nested object properties.
How to implement nested objects, see a simple example below.
First I wrote two function, which is to get disk information and service.
Function get-diskinfo {[cmdletbinding ()]param ([Parameter (mandatory= $true, Valuefrompipeline= $true)][string[]] $computername, [int] $MinimumFreePercent =10,[string] $errorfile = "C:\errors.txt ") $disks =get-wmiobject -class win32_logicaldisk -filter " drivetype=3 " -ComputerName $computername -ErrorAction SilentlyContinue -ErrorVariable err$result=foreach ( $disk in $disks) {$perFree = ($disk. freespace/$disk. Size) *100;if ($perFree -ge $MinimumFreePercent) {$OK = $True}else {$OK = $False}; $disk | select @{n= "Computer"; e={$disk. Pscomputername}},deviceid,volumename, ' @{n= ' Size '; e={' {0:n2} ' -f ($_. SIZE/1GB)}}, ' @{n= ' FreeSpace '; e={' {0:n2} ' -f ($_. FREESPACE/1GB)}}, ' @{name= ' OK; expression={$OK}}} $result if ($err -ne $null) {write-verbose "There are some errors, please check details from the log files "$err | Out-File $Errorfile}else{write-verbose " complete successfully "}}function get-computerservice {param ([string[]] $computername = "localhost") get-wmiobject -computername $computername -Class win32_service -Filter "state like ' Running '" | select @{n= "ComputerName"; e={$_.pscomputername}} , ' Name, displayname, processid, ' @{n= ' Virtual memory ";e={get-process -id $_.processid|select -expandproperty Virtualmemorysize}}, ' @{n= ' Peak page file usage (M) "; e={get-process -id $_.processid| Select @{n= "Peakpagedmemorysize (M)"; e={"{0:n2}" -f ($_. PEAKPAGEDMEMORYSIZE/1MB)}}| select -expandproperty "Peakpagedmemorysize (M)"  }, ' @{n= ' Threads count "; e={(get-process -id $_.processid|select -expand threads). count}} | ft}
Take a look
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/F0/wKioL1dFNGPxt5XRAAB048qX6xY192.png "title=" 1.PNG " alt= "Wkiol1dfngpxt5xraab048qx6xy192.png"/>
Then I create a new function that customizes an object in this function, and the Disksinfo property of the object is created by Get-diskinfo multiple objects The Services property gets multiple objects by Get-computerservice.
function get-detailedinfo{[cmdletbinding ()]param ([string[]] $ComputerNames) foreach ($computername in $computernames ) {$disks =get-diskinfo-computername $ComputerName $service=get-computerservice-computername $ComputerName [email protected]{' ComputerName ' = $computerName; ' Disksinfo ' = $disks; ' Services ' = $service} $obj =new-object-typename psobject-property $props $obj}}
Then we look at the results, and we can see that the corresponding property itself includes multiple objects.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/F0/wKioL1dFOFDRXg49AAB2fcxmOdQ402.png "title=" 2.PNG " alt= "Wkiol1dfofdrxg49aab2fcxmodq402.png"/>
In this case, creating the nested object is successful, but it looks very inconvenient. If I want to see what the corresponding Disksinfo do, you can generally view it in the following ways.
1. Select-expand This can extend the contents of an entire array object, and can be automatically converted to a string type
Get-detailedinfo-computernames Sydwsus | Select-expandproperty Disksinfo
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/F2/wKiom1dFOQjCgtWTAAA9hWsBfUA803.png "style=" float: none; "title=" 3.PNG "alt=" Wkiom1dfoqjcgtwtaaa9hwsbfua803.png "/>
2.format-custom This will show the structure of the whole object.
Get-detailedinfo-computernames Sydwsus | Format-custom *
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/80/F0/wKioL1dFOf6S5TPcAABZCvcnCpc898.png "title=" 4.PNG " Style= "Float:none;" alt= "Wkiol1dfof6s5tpcaabzcvcncpc898.png"/>
3. Manual loop expansion is also possible
Get-detailedinfo-computernames Sydwsus | Foreach{$_.disksinfo}
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/80/F2/wKiom1dFOQzBEHUzAAA40a1YXh8682.png "title=" 5.PNG " Style= "Float:none;" alt= "Wkiom1dfoqzbehuzaaa40a1yxh8682.png"/>
4. PowerShell3 later versions can also be treated as arrays directly ~
(Get-detailedinfo-computernames sydwsus). Disksinfo
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/F0/wKioL1dFOgLwcMESAAA06NG65OY744.png "title=" 6.PNG " Style= "Float:none;" alt= "Wkiol1dfoglwcmesaaa06ng65oy744.png"/>
Now you can see, how to save the whole result?
Traditional many habits are saved in CSV format, let's try
Get-detailedinfo-computername Sydwsus | Export-csv C:\temp\info.csv
Open the confirmation, what's going on?
CSV file This flat structure is not able to display multiple depth of the object, so he can only display the object type, meaning that there are multiple objects of this type need to be displayed, but I cannot display in the CSV.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/F2/wKiom1dFOQ-CAisZAAA58xKMSVA001.png "title=" 7.PNG " Style= "Float:none;" alt= "Wkiom1dfoq-caiszaaa58xkmsva001.png"/>
What should we do with it? This is a good place to use XML files.
For example
Get-detailedinfo-computername Sydwsus | Export-clixml C:\temp\info.xml
This will allow you to save the.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/80/F2/wKiom1dFORSBpIq3AAByY7VtbPc117.png "title=" 8.PNG " Style= "Float:none;" alt= "Wkiom1dforsbpiq3aabyy7vtbpc117.png"/>
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1782988
PowerShell Create, view, and save nested object properties