Build a PowerShell script to resolve DNS names for a domains that are placed in a csv file.

Hello Exports. I am trying to build a PS script to resolve DNS names for a few domains. I have the following script:

$domain1 = "test.com"
$notfound = "`tDNS Entry Not Found"
ForEach ($domain in $domain1)      {
      $lyncdiscoverresult = Resolve-DnsName lyncdiscover.$domain1 -Server 8.8.8.8 -ErrorAction SilentlyContinue
if ($lyncdiscoverresult) {
    Write-Host "`tlyncdiscover.$domain1 $(@(foreach ($result in $lyncdiscoverresult.namehost){ '-> '+$result }))" -ForegroundColor Yellow
} else {
    Write-Host $notfound -ForegroundColor Red
}
select-object Name | Export-Csv "C:\Users\mohsan.chaudhary\Desktop\domain5.csv"
}

I get the desired output in powershell just fine, but the exported csv file is empty. How can i populate the csv file with the results. Also, I would like to import a csv file with a list of domains, that i am placing into the first variable like this: $domain1 = import-csv "c\location\domain.csv". The PS output displays 'DNS Entry Not found' for all domains, even when i know some of the domains should be resolvable (when i run separately each domain like above, then it resolves fine, but when i import a csv file that includes the domain, then all domains are unresolvable). How can run the above script against a import-csv and export the results to a csv file?
Newguy 123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MaheshArchitectCommented:
this is because your output line is beyond loop

put output line in IF action block

i.e.

if ( some condition) {

some action and output line
} else
oBdACommented:
Try it like this.
Since you gave absolutely no information about the import csv file format, you'll need to replace the "Domain" property in line 1 with the column name that actually contains the domain name:
$domains =  Import-Csv -Path "C:\location\domain.csv" | Select-Object -ExpandProperty 'Domain'
$notfound = "`tDNS Entry Not Found"
$domains | ForEach-Object {
	$lyncDiscover = "lyncdiscover.$($_)"
	Write-Host $lyncDiscover -ForegroundColor Yellow
	$lyncDiscoverResult = Resolve-DnsName $lyncDiscover -Server 8.8.8.8 -ErrorAction SilentlyContinue | Select-Object -ExpandProperty NameHost -ErrorAction SilentlyContinue
	If ($lyncDiscoverResult) {
		$lyncdiscoverresult | ForEach-Object {
			Write-Host "`t -> $($_)" -ForegroundColor Yellow
			[PSCustomObject][ordered]@{'Domain' = $lyncDiscover; 'NameHost' = $_}
		}
	} Else {
		Write-Host $notfound -ForegroundColor Red
	}
} | Export-Csv "C:\Users\mohsan.chaudhary\Desktop\domain5.csv"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chirag NagrekarSystem AnalystCommented:
Try this simple command line. It will give you FQDN of server. Make sure you use powershell version 3 or higher.

[net.dns]::GetHostEntry($Computer).Hostname
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.