簡體   English   中英

Powerhsell - 需要解析 IP

[英]Powerhsell - Need to resolve IPs

我可以將包含原始數據的文件導出為 json、xml 或 csv。 我選擇了 JSON。

JSON 輸出如下所示。

{
"entry":[{
"@name":"31.170.162.203",
"ip-netmask":
"31.170.162.203",
"description":
"test1"}
,
{
"@name":"37.193.217.222",
"ip-netmask":
"37.193.217.222",
"description":
"test2"}
,
{
"@name":"46.17.63.169",
"ip-netmask":
"46.17.63.169",
"description":
"test3"}
,
]
}

$input = Get-Content 'C:\Users\e\Desktop' -raw | ConvertFrom-Json

$iplist = $input.entry.'ip-netmask'


foreach ($ip in $iplist)   #for each line in the file...

{
    $hostnames = $null

    try {

        $hostnames = [System.Net.Dns]::GetHostByAddress("$ip").Hostname   #...resolve the ip

    }
     catch [System.Management.Automation.MethodInvocationException] {

          $hostnames = "Server IP cannot resolve."
    }

    catch {

        $hostnames = "unknown error."

    }

    if ($hostnames -ne "Server IP cannot resolve.") {

        $ip -replace $ip, $hostnames

    } else {

        Write-Host $ip
    }
}

輸出:

31.170.165.68
31.170.162.203
l37-193-217-222.novotelecom.ru

這告訴我它正在替換已解析的 IP,並保留所有未解析的原始 IP。 這正是我想要的

我最終找到了如何通過進一步的研究、試驗和錯誤來解決這些問題。

事情 1- 發布的 json 格式不正確。

事情 2 -

讀入 json 字符串、csv、xml 文件數據並將 IPA 傳遞給 .Net 類或 DNS cmdlet 來解析它們

一個例子(因為還有其他方法)...

(@'
{
"entry":[{
"@name":"31.170.162.203",
"ip-netmask":"31.170.162.203",
"description":"test1"}
,
{
"@name":"37.193.217.222",
"ip-netmask":"37.193.217.222",
"description":"test2"}
,
{
"@name":"46.17.63.169",
"ip-netmask":"46.17.63.169",
"description":"test3"}
]
}
'@ | 
ConvertFrom-Json | 
Select-Object -ExpandProperty entry).'ip-netmask' | 
ForEach {
    $PSItem
    [System.Net.Dns]::GetHostEntry("$PSItem").HostName
}

#Results
...
37.193.217.222
l37-193-217-222.novotelecom.ru
...

或者這樣

(@'
{
"entry":[{
"@name":"31.170.162.203",
"ip-netmask":"31.170.162.203",
"description":"test1"}
,
{
"@name":"37.193.217.222",
"ip-netmask":"37.193.217.222",
"description":"test2"}
,
{
"@name":"46.17.63.169",
"ip-netmask":"46.17.63.169",
"description":"test3"}
]
}
'@ | 
ConvertFrom-Json | 
Select-Object -ExpandProperty entry) | 
ForEach {
    $PSItem.'@name'
    $PSItem.'ip-netmask'
    $PSItem.'description'
    [System.Net.Dns]::GetHostEntry("$($PSItem.'ip-netmask')").HostName
}


# Results
...
37.193.217.222
37.193.217.222
test2
l37-193-217-222.novotelecom.ru
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM