PowerShell 创建对象的时候,对象的属性可以是单一属性,也可以在这个属性里面包括多个对象,也就是嵌套的对象属性。
怎么实现嵌套的对象呢,下面看个简单的例子。
首先我写了两个function,分别是获取磁盘信息和服务。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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
} |
执行看看
然后我再创建一个新的function,在这个function里面自定义了一个对象,这个对象的disksinfo属性由Get-DiskInfo 创建多个对象;Services属性由Get-computerservice获取多个对象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
function Get-DetailedInfo {
[ cmdletbinding ()]
param (
[string[]] $ComputerNames
) foreach ( $computername in $computernames ){
$disks = get-diskinfo -computername $ComputerName
$service = Get-ComputerService -computername $ComputerName
$props =@{ 'ComputerName' = $computerName ; 'disksInfo' = $disks ; 'Services' = $service }
$obj = New-Object -TypeName psobject -Property $props
$obj } } |
然后我们看看结果, 可以看见对应的属性本身就包括了多个对象。
这样的话,创建嵌套的对象是成功了,但是看起来很不方便。如果我想查看对应的disksinfo怎么办,一般可以通过以下几种方式查看。
1. select -expand 这个可以把整个数组对象的内容扩展开来,同时还能自动转换成字符串类型
1
|
Get-DetailedInfo -ComputerNames sydwsus | select -ExpandProperty disksinfo
|
2.Format-custom 这个会展示整个对象的结构
1
|
Get-DetailedInfo -ComputerNames sydwsus | Format-Custom *
|
3. 手动的循环展开也是可以的
1
|
Get-DetailedInfo -ComputerNames sydwsus | foreach { $_ .disksInfo}
|
4. PowerShell3 以后的版本也可以直接当做数组处理~
1
|
( Get-DetailedInfo -ComputerNames sydwsus).disksinfo
|
现在可以查看了,如何把整个结果保存下来呢?
传统的很多习惯是保存为csv格式,我们来试试
1
|
Get-DetailedInfo -ComputerName sydwsus | Export-Csv c:\temp\info.csv
|
打开确认一下 额 这是怎么回事?
CSV文件这种平面的结构是无法显示多重深度的对象的,因此他只能显示个对象类型,意思就是这个类型有多个对象需要显示,但是我没法在CSV里面显示出来。
那应该怎么处理呢?这里适合使用xml文件。
比如说
1
|
Get-DetailedInfo -ComputerName sydwsus | Export-Clixml c:\temp\info.xml
|
这样就可以保存了。
本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1782988,如需转载请自行联系原作者