PowerShell自定义对象
前面的性能高
使用[pscustomobject][ordered]强制类型转换
[pscustomobject][ordered]@{
Name= 'Boe'
Number = 1
ID = 007
}
使用[pscustomobject]强制类型转换
[pscustomobject]@{
Name= 'Boe'
Number = 1
ID = 007
}
初始化OrderedPSObject
New-Object pscustomobject -Property([ordered]@{
Name= 'Boe'
Number = 1
ID = 007
})
初始化PSObject
New-Object PSObject -Property @{
Name = 'Boe'
Number = 1
ID = 007
}
使用Select-Object
$Object = '' | Select-Object Name, Number, ID
$Object.Name = 'Boe'
$Object.Number = 1
$Object.ID = 007
借助Add-Member
$Object = New-Object PSObject
$Object | Add-Member -MemberType NoteProperty -Name Name -Value Boe -PassThru |
Add-Member -MemberType NoteProperty -Name Number -Value 1 -PassThru |
Add-Member -MemberType NoteProperty -Name ID -Value 007 -PassThru
批量创建多个对象并放入同一个数组
$ous是包含多个OU条目的文本文件 [array]$objs = $null foreach ($ou in $ous) { $count = $ou.Split(",").length $props=@{'ou'=$ou;'count'=$count} $obj = New-Object -TypeName psobject -Property $props $objs += $obj } $objs |sort count
From:http://www.pstips.net/performance-of-custom-psobject.html
http://www.pstips.net/powershell-convert-hashtable-to-object.html hashtable转换为PS Object