![](/img/trans.png)
[英]need help to resolve - Uncaught ReferenceError: data is not defined
[英]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.