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

Newguy 123
Newguy 123 used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
MaheshArchitect
Distinguished Expert 2018

Commented:
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
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Chirag NagrekarAssociate - Sys Admin

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial