pwsh 对一组 IP 进行排序
# 将 ip 转为 int 值 
function Convert-IPv4ToInt {
    param (
        [string]$ipAddr
    )

    $ip = $ipAddr.Split(':')[0]

    $ipAddress = [System.Net.IPAddress]::Parse($ip)
    # 获取小端序整数
    $littleEndian = $ipAddress.Address
    # 手动将小端序转换为大端序
    $bigEndian = ([int64](
        (($littleEndian -band 0xFF) -shl 24) -bor
        (($littleEndian -band 0xFF00) -shl 8) -bor
        (($littleEndian -band 0xFF0000) -shr 8) -bor
        (($littleEndian -band 0xFF000000) -shr 24)
        ))

    return $bigEndian

}

$onlineDevices | % { [PSCustomObject]@{
        ip   = $_.ip
        ipId = Convert-IPv4ToInt($_.ip)
    } }  | Sort-Object -Property ipId | Format-Table -AutoSize
上一篇
下一篇