Avatar of Newguy 123
Newguy 123
 asked on

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?
PowershellDNSActive Directory

Avatar of undefined
Last Comment
Chirag Nagrekar

8/22/2022 - Mon
Mahesh

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
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Chirag Nagrekar

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes