cette petite fonction, à pour rôle de trier selon 3 types d'objets: [String] [Version] et [int]
Function Sort-ObjectType { <# .Synopsis Sort Object by Types .Description Sort Object by Types .Parameter Type Specifies the type of object - ToString: [System.String] - ToInteger: [System.Int32] - ToVersion: [System.Version] - ToIPAddress [System.Net.IPAddress] .Parameter CaseSensistive .Parameter Descending .Parameter Unique .Example [1]PS> $str += "10","2","5" [2]PS> $str | Sort-Object # 10 2 5 [3]PS> $str | Sort-ObjectType -Type ToInteger # 2 5 10 .Example [1]PS> '1.0.1.10','1.0.1.100','1.0.1.9' | Sort-Object -unique [2]PS> # 1.0.1.10 > 1.0.1.100 > 1.0.1.9 [3]PS> '1.0.1.10','1.0.1.100','1.0.1.9' | Sort-ObjectType -Type ToVersion [4]PS> # 1.0.1.9 > 1.0.1.10 > 1.0.1.100 .Example [1]PS> $ips="3.25.20.100","198.10.3.1","1.2.0.3","10.2.0.6" [2]PS> $ips | Sort-ObjectType -Type ToIPAddress .Outputs System.String #> #Requires -version 2.0 Param( [ValidateSet('ToString','ToInteger','ToVersion','ToIPAddress')] $Type, [Switch]$CaseSensitive, [SWitch]$Descending, [Switch]$Unique ) $input | Sort-Object -Property { $ty=$_ Switch($Type) { 'ToString' {[String]$ty} 'ToInteger' {[Int]$ty} 'ToVersion' {[Version]$ty} 'ToIPAddress' { [byte[]]$ty.split('.')|ForEach{[string]::Format("{0:000}",$_)} } default {$ty} } } -case:$CaseSensitive -desc:$Descending -uni:$Unique }
Aucun commentaire:
Enregistrer un commentaire