powershell 从ip2location数据库将IP号码转换为IP地址时出错?

cidc1ykv  于 2022-12-13  发布在  Shell
关注(0)|答案(1)|浏览(152)

我已经下载了csv格式的ip2location数据库,并阅读第一列数据,并使用ip2location常见问题解答中建议的以下代码将IP号码转换为IP地址。

CSV数据示例:

function Convert-NumbetToIP
{
    param(
    [Parameter(Mandatory=$true)][string]$number
    )

    try
    {    
        $w = [int]($number/16777216)%256
        $x = [int]($number/65536)%256
        $y = [int]($number/256)%256
        $z = [int]$number%256

        $ipAddress = "$w.$x.$y.$z"
        Write-Host "Actual IP Address - $ipAddress"

        Write-Host "Returning IP Address"
        return $ipAddress
    }
    catch
    {
        throw $_
    }
}

Convert-NumbetToIP -number 2147483648

无法将值“2147483648”转换为类型“System.Int32”。错误:“值对于Int32太大或太小。"**
有人能帮我得到正确的IP地址也请让我知道什么类型的数据是在第2列。

yqlxgs2m

yqlxgs2m1#

通过将[int]更改为[int64],我找到了答案

function Convert-NumbetToIP
{
    param(
    [Parameter(Mandatory=$true)][string]$number
    )

    try
    {    
        $w = [int64]($number/16777216)%256
        $x = [int64]($number/65536)%256
        $y = [int64]($number/256)%256
        $z = [int64]$number%256

        $ipAddress = "$w.$x.$y.$z"
        Write-Host "Actual IP Address - $ipAddress"

        Write-Host "Returning IP Address"
        return $ipAddress
    }
    catch
    {
        throw $_
    }
}

Convert-NumbetToIP -number 2147483648

相关问题