We help IT Professionals succeed at work.

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

157 Views
1 Endorsement
Last Modified: 2019-01-01
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

MaheshArchitect
CERTIFIED EXPERT
Distinguished Expert 2019

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
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Chirag NagrekarInfrastructure Automation Expert
CERTIFIED EXPERT

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.