简体   繁体   English

Powerhsell - 需要解析 IP

[英]Powerhsell - Need to resolve IPs

I can export the file containing the original data as either a json, xml, or csv.我可以将包含原始数据的文件导出为 json、xml 或 csv。 I chose JSON.我选择了 JSON。

The JSON output looks like the below. 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
    }
}

output:输出:

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

This tells me it is replacing the resolved IPs, and keeping any original IPs that weren't resolved.这告诉我它正在替换已解析的 IP,并保留所有未解析的原始 IP。 This is exactly what I want这正是我想要的

I ended up figuring out how to resolve them through further research, trial, and error.我最终找到了如何通过进一步的研究、试验和错误来解决这些问题。

Thing 1- The posted json is not properly formatted.事情 1- 发布的 json 格式不正确。

Thing 2 -事情 2 -

Read in the json string, csv, xml file data and pass the IPA to the .Net class or DNS cmdlets to resolve them读入 json 字符串、csv、xml 文件数据并将 IPA 传递给 .Net 类或 DNS cmdlet 来解析它们

One Example (as there are other ways)...一个例子(因为还有其他方法)...

(@'
{
"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
...

or this way或者这样

(@'
{
"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