Reputation: 877
I have not used powershell in some time but still know the basics. I am trying to create a script that out puts a csv file by checking a hostnames IP address. If the IP address is correct for the hostname it outputs YES. If the IP is wrong for the hostname it outputs NO. I have searched here and also on other sites with no prevail. Here is what I have. Any help would be greatly appreciated. Thank you for a great community! :
$names = Get-content "hnames.txt"
foreach ($name in $names){
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$name,up"
}
else{+
Write-Host "$name,down"
}
}
Upvotes: 0
Views: 4961
Reputation: 1096
I'd do something like this:
# example data saved in .\compinfo.csv:
hname,ip
comp1,192.168.1.10
comp2,192.168.1.11
# importing the example data
$compinfo = import-csv .\compinfo.csv
$lookupData = foreach($comp in $compinfo)
{
$nslkup = [System.Net.DNS]::GetHostEntry($comp.hname)
$ping = (Test-Connection -ComputerName $comp.hname -Count 1 -ErrorAction SilentlyContinue)
if($ping)
{
$status = "up"
}
else
{
$status = "down"
}
if($nslkup.AddressList.IPAddressToString -eq $comp.ip)
{
$ipgood = $true
}
else
{
$ipgood = $false
}
[pscustomobject]@{
computerName = $comp.hname
expectedIp = $comp.ip
status = $status
goodIp = $ipgood
dnsName = $nslkup.hostname
}
}
$lookupData | export-csv .\lookups.csv -NoTypeInformation
Upvotes: 2