best way to use the nslookup command on Windows to retrieve IP addresses for a list of hostnames.

Vini
Giga Guru

Hi Friends,

 

As part of Discovery implementation and support work, we often need to verify the correctness of device hostnames or IP addresses. To reduce dependency on the Network and Servers teams, I created a PowerShell script for validation.

Note:copy all the hostnames in hostnmaes,txt file and save it in a folder then open powershell make sure to pipeline the path where the file and execute the following code. it worked for me... 🙂

 

# Initialize the output file with headers
"Hostname,IPAddress" | Out-File -FilePath output.csv -Encoding UTF8

# Read hostnames and resolve
Get-Content hostnames.txt | ForEach-Object {
$hostname = $_.Trim()
try {
$rec = Resolve-DnsName -Name $hostname -Type A -ErrorAction Stop
foreach ($r in $rec) {
"$hostname,$($r.IPAddress)" | Out-File -FilePath output.csv -Append -Encoding UTF8
}
} catch {
"$hostname,Could not resolve" | Out-File -FilePath output.csv -Append -Encoding UTF8
}
}

 

Hope this helps for to you, you can mark this respectively.

# Discovery # CMDB # ITOM

0 REPLIES 0