PSObject&排序

时间:2014-09-17 13:01:06

标签: powershell

文件ip.txt是一个IP地址列表

function CreateObj
{
    param([string]$IP,[string]$class,[int]$number)
    $Obj=New-Object PSObject
    $Obj | Add-Member -Name IP -MemberType NoteProperty  -Value $IP  
    $Obj | Add-Member -Name CLASS -MemberType NoteProperty  -Value $Class  
    $Obj | Add-Member -Name NUMBER -MemberType NoteProperty -Value $number 
    return $Obj
}  


$allip = get-content ip.txt

$tab=@()
foreach ($item in $allip)
{
    $class = $item.substring(0,$item.lastindexof("."))
    $number = [int]($item.substring($item.lastindexof(".")+1))
    $tab += createobj $item $Class $number
}
$tab | sort |get-unique # <-- this fail problem of type i think ?

$ tab | sort | get-unique#&lt; - 这个失败......类型的问题? 感谢

1 个答案:

答案 0 :(得分:1)

您应该使用sort-object cmdlet的-unique参数: $tab | sort IP -unique